Linux Automated Minecraft Backup
No, I’m not a Minecraft player. I got into it for a little while during the Alpha, paid for the full client to support Mojang just because I thought the idea was fantastic and loved seeing what other people do. But I just couldn’t stick with it. I do, however, have several friends that are fanatical fans of the game and some cycles on the home server to spare. So I offered to host a server for them so they could all play together instead of their own single player clients at home.
One shortcoming that I had for a while with the Linux version was a way to do automated backups. When running Minecraft from the command line in a screen session, there was no way to automatically shut the server down cleanly without possibly causing world corruption. I hadn’t really been keeping up with the small updates in the backend coding, but apparently they finally added a "
save-off" console command to the game to allow a clean backup of the world files while the game was still running. The next problem was figuring out how to send that to a screen session automatically.
Spent some time looking at the options in screen that I had never really had a need for previously so wasn’t familiar with them. The following is an example of the script I whipped up that will perform a full backup of Minecraft running in screen window 3.
rm -f minecraft-backup.tar.gz.old
mv minecraft-low.tar.gz minecraft-backup.tar.gz.old
screen -p 3 -X stuff "say Backing up world. It will now no longer save!... $world"
screen -p 3 -X stuff "save-off $world"
screen -p 3 -X stuff "save-all $world"
mv ./minecraft-low.tar.gz minecraft-$world-backup.tar.gz
mv ~/minecraft/backups/server_name/minecraft-$world-backup.tar.gz backups
screen -p 3 -X stuff "save-on $world"
screen -p 3 -X stuff "say Backup complete! World will now save. $world"
stuff command was the magic I was looking for. It sends some nice
say commands to the console to let any players online know that the backup is running and turns world-saving on and off to allow a good, clean backup to be taken. Took some hunting around the web to find a way to send a return character in a stuff command. Found several, but printf is the first one I came across that worked, so I went with it. I set this to run in my cron daily and my friends are now a little more thankful that their world is getting backed up more reliably than me just remembering to do it on occasion.