Automatic Minecraft Server Backup Script

Minecraft, even though officially released, is still prone to bugs and crashes. This happened once on a server I host for my friends, with desastrous consequences. When the server crashed, the map got corrupt and unusable. We had a backup, but it was several weeks old...

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Eventually, we started all over again, on a new map, but this time, I made sure that we would always have an up-to-date backup.

Note: This will only work for Windows.

The Script

In order to never again have the fear of losing my map, I wrote a simple batch file that would create a backup of the server every ten minutes. I decided to share it with you in order to spare you the frustration of having to write your own. You can download by right-clicking the button below and selecting "Save as...."

 

Backup.bat

 

The file contains the following code:

 

SET COUNTER=0
:loop
SET /a COUNTER=%COUNTER%+1
XCOPY "Server\*" "c:\minecraft\backups\server_backup_%COUNTER%" /i /s
PING 1.1.1.1 -n 1 -w 600000 > NUL
goto loop

 

It's actually formidably simple, but less advanced users might still enjoy an explanation.

 

SET COUNTER=0

 

This first line creates a variable called COUNTER.

 

:loop

 

Next, we start a loop. A loop is simply a syntactic structure in which we tell the computer to repeat a certain task over and over again.

 

SET /a COUNTER=%COUNTER%+1

 

Now here's a more funky line. When we first created our COUNTER variable, it was equal to zero, remember? Well, now we are ready to make our first backup, so we make counter equal to itself+1. This sounds ridiculous but actually makes a lot of sense when we think about it. If COUNTER is equal to zero, then COUNTER+1 is equal to 1, so if we then make COUNTER equal to itself+1, then it will become one. Likewise, if COUNTER was equal to any other number, this line would add one to it, so 2 becomes 3, 3 becomes 4, etc.

 

XCOPY "Server\*" "c:\minecraft\backups\server_backup_%COUNTER%" /i /s

 

This is where the real work gets done. Once it gets to this line, the computer copies the "Server" folder to "C:\minecraft\backups\server_backup_" and then appends the counter number at the end of the backup, so we get something that looks like this:

 

Backups in their server

 

PING 1.1.1.1 -n 1 -w 600000 > NUL

 

This line is particular, since MS-DOS does not include a timer command, we must use this little "hack" to force the batch file to wait a predetermined time. There are a few ways to do this, but I chose the PING method. Ping is a command used to test the reachability of an IP on a network. It sends packets of information to said IP and then waits either for an answer, or until the request times out. For this trick to work, we need to ping an IP that will not answer, thus 1.1.1.1. Next, we tell the ping command to wait 600000 seconds (10 minutes) until it times out.

Basically, ping calls an IP, waits for an answer and, since 1.1.1.1 never answers, it will wait for the whole ten minutes.

 

goto loop

 

At the beginning of the file, we made a loop using the :loop label. Now, we are simply telling the computer to go back to that label, and thus the cycle starts once again!

Using the Script

Fortunately, this file is very easy to use. Simply rename put your Minecraft Server and the batch file in the same folder and then make sure the Server is actually called "Server" (alternatively, you can change the name of the folder to be copied by XCOPY in the batch file").

 

Server Folder

Now just make sure you start the script every time you start your server!

 

Conclusion

I hope you enjoy this small script and that it save you from losing all your hard work! Finally, you might also want to delete some old backups after a few hours, once; I left this script on for so long, it almost filled up my Hard Drive!

 

Happy Minecraft!

-Icosebyte