Wednesday, June 1, 2011

How to regularly backup a Minecraft Server

As I have mentioned previously, I am the server administrator for a Minecraft world for several of my friends and myself. The other week a worst-case scenario occurred: the world files somehow corrupted and Minecraft refused to load the world. I don’t know if it was the recent power outages to my server that caused it or the frequent Minecraft updates, but the fact was that some progress was lost. Thankfully I had manually backed up the world a few weeks prior so only those few weeks of play were lost.

After that happened I was determined to figure out how to regularly back up the world so that I would lose at most a day’s worth of activity. The solution I came up with requires the following:


The first task was to figure out how I can back up the world files. I found 7-Zip was more than adequate to take on this task because: it’s free; it’s open source; it has command line support. Here is the batch file I created:

for /F "tokens=1-4 delims=/ " %%i IN ('date /t') DO (set DT_DAY=%%iset DT_MM=%%jset DT_DD=%%kSET DT_YYYY=%%l) "c:\program files\7-Zip\7z.exe" a "c:\minecraft\backups\world_%DT_YYYY%_%DT_MM%_%DT_DD%.7z" "c:\minecraft\world" -ssw
Edit: Thanks to Stanchez's comment below, I found out about the "-ssw" switch in 7-zip. This allows 7-zip to archive files that are currently locked by other programs, meaning you don't have to shut down Minecraft to perform a backup. Thanks for the advice!

I’ll go ahead and break down this batch script line-by-line. The first five lines are for creating a date stamp in YYYY_MM_DD format for the archive file name. For example, if this were to run for June 1st, 2011, the date stamp would read “2011_06_01”. The line with “c:\program files\7-Zip\7z.exe” in it is the actual command to archive the world folder. The syntax of the command is as follows:

“c:\path\to\7-zip\executable” a “c:\path\to\folder\to\hold\backups\backupname_%DT_YYYY%_%DT_MM%_%DT_DD%.7z "c:\path\to\minecraft\world\folder" -ssw

The second task was to create a batch script that will clean up older backups in order to save disk space. Each backup of my world is clocking in around 100MB so I don’t want more than a week’s worth sitting on the server. This is where the forfiles command comes in handy. With this command I can build a collection of files to run a specific command against. In our case here, I want to build a collection of files with the extension *.7z in my backup folder that are older than 7 days so I can delete them. The following command takes care of exactly that:
forfiles /P "c:\minecraft\backups" /M *.7z /D -7 /C "cmd /C del @PATH"
  • The /P flag specifies the directory to look in for files.
  • The /M flag specifies a mask to use. In our case, 7-zip files.
  • The /D flag specifies a timeframe for last-modified. Here we are looking for files older than 7 days.
  • The /C flag specifies what command to run against the resulting set of files. Here I am asking to delete each files that meets the requirements above. The @PATH variable provides the full path of each file that meets requirements.

To put this all together, I set up both of these as batch files and added them as scheduled tasks in Task Scheduler. I set the first one to run nightly at 2AM, immediately followed by the second one. The whole process takes about 3-4 minutes on my server.