Backing up Minecraft

I run an in-house server for myself and some friends and I got tired of manually running backups, by logging in, stopping the server, making the tar.gz file and then moving it around, so I threw together this simple script.  It relies upon an active screen session for the server and a named window called Server to be present, but other than that it should be pretty straight forward how to works.

#!/bin/sh

# change the argument for -S to whatever screen -ls says is the right value
# this allows us to target the running screen sessions so if there is a server
# reboot then this script must be updated (until I work out the code to make
# all that happen automatically

screen -S 436.minecraft -p Server -X stuff '/say Preparing for Server Backup
/say please get to a safe area soon (2 minutes)
'
sleep 2m # this should be 2m when testing is done
screen -S 436.minecraft -p Server -X stuff '/say commencing backup process now
/say this should take about 15 minutes max
/save-all
/say server is going down NOW!
/stop
'

cd mc-server
tar -cf "mc-backup-"`date +%Y_%m_%d`".tar" world*
gzip "mc-backup-"`date +%Y_%m_%d`".tar"
mv "mc-backup-"`date +%Y_%m_%d`".tar.gz" ~/backups

screen -S 436.minecraft -p Server -X stuff './start.sh
'

It wouldn’t be hard to make the script scp the file to a different server or possibly push to a cloud provider like Dropbox. The key here is that it also brings the server back up so you could very easily make this a completely automatic task if so inclined.

Leave a Comment

Close Bitnami banner
Bitnami