Minecraft Server Stop, Backup, Restart

Mar 26

Minecraft Server Stop, Backup, Restart

Running a Minecraft server isn’t that robust, it just runs the server and nothing else. If it stops while chunks are being changed you’re in for a world of pain trying to restore any sort of useable functionality to your map.

This happened to me a few days after I got a fully modded server up and running, and because of the loss I didn’t really feel like fixing anything and starting over just for it to happen again…

Long story short, I’m motivated now and want to get this working with daily backups so we can’t have a catastrophic failure like in the past. This time I needed a way to GRACEFULLY stop the server, run a backup command, and restart the server every morning at 04:00AM EST. Sounds easy enough, but it’s been a headache getting the server to stop gracefully, so I just coded it in AutoHotkey.


#WinActivateForce

IfWinExist, taskeng.exe
{
WinActivate taskeng.exe
send say THE SERVER WILL BE RESTARTING IN ONE MINUTE.
send {Enter}
send say THE SERVER WILL BE UNAVAILABLE FOR 15 MINUTES.
send {Enter}
send say PLEASE TRY CONNECTING AGAIN AFTER 15 MINUTES.
send {Enter}
sleep 60000
send stop
send {Enter}
sleep 10000
return
}

IfWinExist, C:\Windows\system32\cmd.exe
{
WinActivate C:\Windows\system32\cmd.exe
send say THE SERVER WILL BE RESTARTING IN ONE MINUTE.
send {Enter}
send say THE SERVER WILL BE UNAVAILABLE FOR 15 MINUTES.
send {Enter}
send say PLEASE TRY CONNECTING AGAIN AFTER 15 MINUTES.
send {Enter}
sleep 60000
send stop
send {Enter}
sleep 10000
return
}

Basically it looks to see HOW the sever was started, either manually or via a scheduled task/time, and selects the window and issues the commands to do a graceful shutdown.

Now I paired that with SyncBack Pro, which I have set to run 7 different profiles, one for every day of the week. They kick off at 04:00AM EST, but before they run, they issue a command to run the .exe with the code above to stop the server. After the server is stopped, it backs up our entire Minecraft directory into a .zip file with the name of the weekday to a specific location. Then we use Windows task scheduler to automatically start the server again at 04:15AM EST every day.

Everything will be put to the test in the following days, but I’m pretty sure I worked out all the kinks this morning.


17 comments

  1. I really appreciate you creating and publishing this script. I’ve been meaning to do something like this but I am horrible at coding for the Windows command line. Thanks

    • No problem, it was a learning experience for me as well, so I’m glad somebody can at least take a look at it and benefit from it somehow.

  2. Minecraft-what’s it about? Aim of the game?

  3. James Stroud /

    Cory, a million thanks. Here is exactly what I did based upon your info, I have a slimmed down but not as functional autokey script to stop the minecraft server, mine is
    SetTitleMatchMode 2
    ; http://www.autohotkey.com/docs/commands/SetTitleMatchMode.htm
    ; idea of script from http://corey.degrandchamp.com/2011/03/26/minecraft-server-stop-backup-restart/

    ; IfWinExist, C:\Windows\system32\cmd.exe
    ; this is why I have the the “title run-minecraft” in the .bat file that starts minecraft. I also named the bat file run-minecraft.bat.
    ; Because the line below is looking for “run-minecraft” in the “title” of the window
    IfWinExist, run-minecraft
    {
    ; WinActivate C:\Windows\system32\cmd.exe
    WinActivate run-minecraft.bat
    send say “Save your creations NOW!!!!!!” THE Minecraft SERVER WILL BE RESTARTING IN 10 Seconds.
    send say This is from DADDY!!!
    send {Enter}
    send say THE SERVER WILL BE UNAVAILABLE FOR 1 MINUTE.
    send {Enter}
    send say PLEASE TRY CONNECTING AGAIN AFTER 1 MINUTE.
    send {Enter}
    sleep, 10000
    send stop
    send {Enter}
    return
    }

    I bought backup for all professional to run the above stop server script, then back-up the minecraft server folder, and the I use the following minecraft start up script (bat file) to start minecraft after the backup. This start-up script took forever for me to get to start automatically via another program like a back-up software program. So Here is my run-minecraft.bat file

    title run-minecraft
    path=%PATH%;%C:\”Program Files”\Java\jre6\bin
    D:
    cd minecraft
    java.exe -Xms1048M -Xmx2048M -jar minecraft_server.jar nogui

    The issues were around paths and java and directories etc. for it to start correctly from the back-up software app. I also posted the start-up script here too
    http://www.minecraftwiki.net/wiki/Tutorials/Setting_up_a_server

  4. James Stroud /

    Folks a small typo in my reply, replace the line
    WinActivate run-minecraft.bat
    with
    WinActivate run-minecraft

    Also in the In the above bat file that starts the minecraft server on Windows, in this example the minecraft directory is located at D:\minecraft (so minecraft_server.jar is located at D:\minecraft\minecraft_server.jar), the operating system is Windows 7 64 bit, Sun’s java.exe is installed at C:\Program Files\Java\jre6\bin

    This java happens to be 64 bit and the PC I’m running has plenty of RAM, 16 GB in my case, so I can allocate the 2GB via the Xmx2048M of maximum memory to run minecraft. The D: line below tells the bat file to go the D: drive. Also note the double quotes around “Program Files” below, needed because of the space between the words Programs and Files . I also chose not to use the @echo off The title run-minecraft in the 1st line is to just have the MS DOS window that is used to run minecraft display “run-minecraft” in the window title bar.

  5. Jaladin /

    Nice script. thanks!
    Why do you stop the server for backup?
    Shouldn’t /save-off, /save-all, then backup, /save-on also work (without down time)?Any bad experience with it?

  6. I view something really special in this internet site.

  7. Do you have a date for a native linux version? or will i have to use WINE for a while?

  8. It’s appropriate time to make a few plans for the longer term and it is time to be happy. I’ve read this publish and if I may I want to suggest you few interesting issues or suggestions. Maybe you can write subsequent articles regarding this article. I want to read more issues about it!

  9. Important facts. Fortunate enough us I uncovered your web site by chance, and I am astonished why this particular twist of fate did not developed before! I saved as a favorite this.

  10. Thanks for the info. Looks like autohotkey is the only app that can do that.

    I suggest Freefilesync for backup, sync you can create a batch file that does the backup and close.

    http://sourceforge.net/projects/freefilesync/

    I tried a lot of those app. Some looks great at first but are full of bug and delete your file. This one is perfect.

    I’m also running, xampp, terraria server, starbound server, teamspeak server, and a vanilla minecraft server. (MCXON is down right now, I’m waiting for 1.7.4 craftbukkit and mods to be updated)

  11. Have you got unsolicited mail difficulty here; I additionally i’m some sort of digg, and that i has been wishing to determine your needs; many people are suffering from a few wonderful strategies and we are seeking to change techniques with other sites, be sure to capture myself a contact if interested.

  12. Write more, thats all I have to say. Literally, it seems as though you relied
    on the video to make your point. You definitely know what youre talking about, why waste your intelligence on just posting videos to your blog when you could be
    giving us something informative to read?

    My page telecharger subway surfers (blogspot.com)

  13. Hello There. I found your blog using msn. This is
    a very well written article. I will make sure to bookmark it and return to read more of your useful
    info. Thanks for the post. I’ll definitely comeback.

    Here is my website: walk of shame full movie

  14. I have created a similar script and a video tutorial.

    Check it out. I create scripts for any type of minecraft servers you might have.

    http://youtu.be/WL2RE4s2O6s

    Enjoy,

    Minecraft Super Admin

  15. Я думаю, Вы найдёте верное решение.
    Не отчаивайтесь.

    Мой личный сайт :: заработок в интернете

  16. I suck at coding where do I paste all of it?

Leave a Reply