Backing Up Your Minecraft Server Worlds

If like me you are running a Minecraft server you’ll appreciate the necessity of maintaining good solid backups of your data in case of a crash or after being attacked by griefers*. There are literally dozens of variations of server backup scripts out there based on the ones at the Minecraft wiki.

The majority of these scripts will happily backup a single world. However, if you are running either the “nether” world or other worlds alongside your main one then you may have difficulties.

There have been a few scripts that use array values fed into a loop to backup both worlds:

# Declare Worlds to be backed up
declare -a worlds=(world world_nether)

# Get number of worlds
numworlds=${#worlds[@]}

# Backup Worlds
for ((i=0;i<$numworlds;i++)); do
BFILE="${worlds[$i]}.$STAMP.tar.gz"
CMD="tar -czf $FINALDIR/$BFILE $SRCDIR/${worlds[$i]}"
done

Unfortunately this approach doesn’t seem to work too well in Debian based Linux distributions. The issues I was running up against were that only one of the worlds would be backed up and the other completely ignored for no apparent reason. In an attempt to get to the cause of the issue I discovered that if I typed the tar command with the full path in the shell it worked without a hitch. However, when entered in the bash script inside the loop it would only partially run without rhyme or reason.

After several days of scouring forums for alternative backup scripts I discovered that the issue was not to do with either the script or the tar command itself but in how it was called. A search on the eval command and its use revealed the solution… The problem with the CMD string is that the bash shell doesn’t parse it correctly, but if you add the eval command to call the function it works correctly!

# Backup Worlds
for ((i=0;i<$numworlds;i++)); do
BFILE="${worlds[$i]}.$STAMP.tar.gz"
CMD="tar -czf $FINALDIR/$BFILE $SRCDIR/${worlds[$i]}"
eval $CMD
done

So there you have it! A single line added to the loop that resolves the issue and allows for multiple worlds to be backed up separately.

* For the uninitiated the terms “griefer” and “griefing” relate to acts of deliberate vandalism of other people’s creations within Minecraft worlds. Think of a group of <insert slang phrase for your country/region> vandalising and terrorising your neighbourhood and you get the general idea.

Advertisements