Wednesday, 11 December 2013

Minecraft Backup for MultiWorld servers

Basic Server Backup

One of the main reasons we built our new Linux server was to be able to run a simple Minecraft server, but as time's gone by we've moved to a more sophisticated configuration. We started out by switching from vanilla Minecraft to (plug-in friendly) CraftBukkit and from there we've experimented with various plug-ins that improved security or added enhancements to the game.

We've always used the linux startup script from the minecraft wiki and this gives us the added benefit of easy world backups without having to shut the service down. Backups are triggered from an entry in the crontab as below.

10 23 * * * /etc/init.d/minecraft backup

Here the backup option is triggered at 23:10 every day and generates the following entries in the minecraft server log.

2013-12-01 23:10:01 [INFO] [Server ] SERVER BACKUP STARTING. Server going readonly...
2013-12-01 23:10:01 [INFO] CONSOLE: Disabled level saving..
2013-12-01 23:10:01 [INFO] CONSOLE: Forcing save..
2013-12-01 23:10:03 [INFO] CONSOLE: Save complete.
2013-12-01 23:10:19 [INFO] CONSOLE: Enabled level saving..
2013-12-01 23:10:19 [INFO] [Server ] SERVER BACKUP ENDED. Server going read-write...  

MultiSite Backup

It wasn't long before we had the Multi-World plug-in installed and a dozen new Worlds linked in. My son built a linking portal with teleports to all of the new worlds and it was great, but it became apparent that these weren't being backed up by the startup script.

Mojang's script covers just the main world which is defined in the #Settings section at the top of the file. (see example below)


OTHERWORLDS='creative hungergames spawn griefcity'

I added a new variable called 'OTHERWORLDS' and I populated it with a list of the worlds currently being missed. (Note: these are separated by a space character so make sure the worlds don't include spaces in their names)

Next I altered the backup section of the script to loop through the OTHERWORLDS list and add each world directory to the backup tar file. (see script below)

mc_backup() {
NOW=`date "+%Y-%m-%d_%Hh%M"`
echo "Backing up minecraft world…"
#as_user "cd $MCPATH && cp -r $WORLD $BACKUPPATH/${WORLD}_`date "+%Y.%m.%d_%H.%M"`" as_user "tar -C \"$MCPATH\" -cf \"$BACKUP_FILE\" $WORLD"

  for i in $OTHERWORLDS
    as_user "tar -C \"$MCPATH\" -rf \"$BACKUP_FILE\" $i"

echo "Backing up $SERVICE"
as_user "tar -C \"$MCPATH\" -rf \"$BACKUP_FILE\" $SERVICE"
#as_user "cp \"$MCPATH/$SERVICE\" \"$BACKUPPATH/minecraft_server_${NOW}.jar\""


echo "Compressing backup..."
as_user "gzip -f \"$BACKUP_FILE\""
echo "Done."

Now when the backup runs the main world is backed up to a daily tar file and then the other worlds are added to it. (It will create a TAR file named after your main world and a timestamp)

Possible Updates

The only real problem with this script is that is doesn't cater for world names containing spaces. This was initially an issue that I struggled to fix, and in the end I took the easy option and altered the world name to remove the offending character.

But as is often the case the script is good enough, so I have no plans to fix it.


An improved method can be found here