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.

Tuesday, May 24, 2011

Run a program independent of session in Windows Server 2008

One of my hobbies is wandering around in the lovely realm that is Minecraft. I am an admin of a world for a small group of my friends. I have a modest file server that runs 24/7 already, so I thought “why not?” and put Minecraft Server on there. One of the things that immediately annoyed me is that if I just run the server executable (e.g. start it in a Remote Desktop session), I would have to leave my server account logged in permanently. That doesn’t fly with me so I had to figure out a way to get the game to run without me being logged in.


After much disappointing searching on Google for things like “run program as a service” and “use system account to run program in Windows” I decided to do some sleuthing around on my own machine. I found a pretty simple way to do it as well. Keep in mind this can work for other programs, but I am using Minecraft as the example.


The first thing you want to do in Windows Server 2008 is open up the Task Scheduler program. This can be found under Start Menu > Accessories > System Tools.


The next step is to click “Create Task…” in the right-hand Action pane.


From this window you first want to click on the “Change User or Group” button, enter “System” in the text box in the dialog and then click OK.


After that is done, click on the Triggers tab at the top of the Create Task dialog and Add a new Trigger. In the drop down for “Begin the task” select “At Startup” and then click OK.


When finished with creating the Trigger, click on the Actions tab at the top of the Create Task dialog and Add a New Action. Select “Start a Program” from the Action drop down menu and in the text box for “Program/Script'”, enter the path to the Minecraft_Server.exe executable. I’d also recommend setting the “Start in” field to the same directory as the executable. Then click OK.

Finally, go ahead and click OK on the Create Task dialog and voila! You should now have the Minecraft_Server.exe start up under the System account when your machine boots. To test your new scheduled task, you can right-click on it in the Task Scheduler Library and select “Run”.


With this setup I don’t have to leave a Remote Desktop session idling or Lock the server session instead of Logging Out my Administrator account. Minecraft will keep running on the system while the server itself is powered on.