Keith's Ramblings…

"Fixing a complex system sounds pretty easy when you're an idiot."

Linux Automated Minecraft Backup

with 8 comments

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.

The 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.

Written by Keith

January 12th, 2012 at 12:35 am

Posted in Uncategorized

Tagged with , ,

  • Anonymous

    Thanks, this really helped me out and it’s exactly what I was looking for; I didn’t know you could send commands to screen. Thanks again!

    • Keith

      No problem. Glad someone out there has found this useful 🙂

  • tar is telling me that “file changed as we read it.” Is that a problem?

    • keithf4

      That means something is still changing the file as tar is trying to add it to the resulting tarball file. I would think that shouldn’t be happening with save-off set and I’ve never seen it do that myself. There’s an option to tar to ignore those errors if you know the resulting tarball will be fine (–ignore-failed-read). I would check with someone in the Minecraft community about it. Also test your backup to make sure you can restore it.

  • This looks like it could come in handy especially since it looks as though it backs up the whole folder including mods and texture packs.

    • keithf4

      Yup, all it’s doing is just basic shell commands to zip up the entire game folder. You can add as many other commands in there as needed if you had things in multiple places.

  • Dan

    The -p bit on screen wasn’t working for me. I had to change the script to use screen -x SCREENAME -X stuff “blah” and it works great, thanks 😀

  • Brett

    To go a step further I’m going to be running my server with a small APC 1500 UPS at home. I’d like to configure a script in such a way that if power went out and the UPS became active, that a backup would execute to an “emergency_backup” folder (or some other unique backup folder name). Then after a certain amount of time if the server is still running on the ups, power off. I’m going to tinker with this for the next few days, maybe longer depending how much time I can allocate to it but will post back to this thread. If anyone has any thoughts or suggestions I’d like to hear them. It’s always good to get feedback from others that may (or may not) have attempted something like this possibly other than for Minecraft.