Jump to content

Windows dedicated server backup script


Liiw

Recommended Posts

I've been running a separate Windows machine as a dedicated server. It was a pretty modified session and unfortunately the world got corrupted and since the last actual backup was from a while ago, I've decided to reassemble the pack on a newer version of the game etc. 

Because of this, I was looking for ways to automate backing up the world files. I decided to write a batch script to handle this for me. I've attached the script files to this post in case anyone else is interested in using it. 

To get it working, I will tell the Windows task scheduler to run BackupManager.bat at 04:55 (five minutes before 5AM), which will call for the other bat files in order. The server instance needs to be running for the script to work. Otherwise it will shut itself down.

BackupManager.bat has some parameters that need to be customized:

set savePath="C:\Users\Liiw\AppData\Roaming\VintagestoryData\Saves"
set backupPath="D:\VS Server backup\Saves\"
set exePath="C:\Games\Vintagestory\VintagestoryServer.exe"
set EXE=VintagestoryServer.exe
  • savePath - where the server "Saves" folder is located.
  • backupPath - where the backups will be stored.
  • exePath - where the server exe is.
  • EXE - server process name, probably don't need to change that one.

Basically the script works as follows:

  1. Searches through the tasklist for an active server process (the EXE). If it does not find it, it will display a message and shut down after 5 seconds.
  2. If it finds a server instance, it will activate it and send a message to the server console that the server will shut down in 5 minutes. This is to warn any players online.
  3. Repeat the warning for 1 minute and 10 seconds as well.
  4. Shut down the server. 
  5. After waiting 10 seconds, it will create the backup as a [DATE][TIME].zip file in the defined folder.
  6. Restart the server. There is a Y/N prompt with a ten second delay in case the user wants to cancel server start.

I've included the whole "Saves" folder because in my case the mod "XSkills" also keeps the leveling data in this folder as a separate file. The server built-in backup command would only backup the world file and not the extra data. 

I've given the backup command a ten second delay to make sure that the server instance is able to close. I've tested it a couple of times and it seemed to work ok. In certain cases, the delay might need to be longer. I've used Notepad++ for easier editing. The number on line 40 (TIMEOUT /T 10 /NOBREAK>nul) of BackupManager.bat represents the seconds.

VS BACKUP MANAGER.zip

  • Like 2
Link to comment
Share on other sites

Apparently getting this script working via Task Scheduler on a headless machine is not that straightforward. It is running properly when I am connected to the machine via Remote Desktop, but refuses to run while I'm not connected. I've tried multiple ways, but it will not cooperate. It seems to be a problem with Powershell and GUI interaction while there is no display output. I'm gonna look for alternative solutions to the backup issue. 

Link to comment
Share on other sites

Thanks for the suggestion! I have not yet used this mod, but it might work. I was thinking about making a stand-alone mod for the autobackup and this would give me the information from where to start. 

But since that will take an unforeseen amount of time (because of life and all other stuff regarding it), I believe it would be possible to use Th3Essentials in the meantime. It seems that this does not backup any additional data, like Xskills for example, but that might not be even that much of an issue. 

Back when I was running a Minecraft modded server, I used a nifty little script to run the server. This script could restart the server when it crashed or was shut down for some other reason. After I set it up in the Windows Startup section, it would even start the server after the PC restarted for whatever reason. I have changed the script a little to work with VS. Namely some echo text and the actual EXE path to be started. 

So this script would handle the restart of the server and the backup could be configured and handled by Th3Essentials mod. 

@choice /t 10 /d N /m "Cancel server start?"
@if %errorlevel% == 1 goto yes
goto start

:start
@echo Starting Vintage Story Server...


@echo (%date% %time%) Starting Server. >> restart.log

:VSStart
"C:\Games\Vintagestory\VintagestoryServer.exe"

@echo.
@echo.
@echo.

@choice /t 10 /d N /m "Stop server start?"
@if %errorlevel% == 1 goto yes
goto no

:no
@echo.
@echo (%date% %time%) Server Crash/Restart. >> restart.log
@echo.
@goto VSStart

:yes

So the line after :VSStart would need to reflect your actual server exe location. 

This script will first ask if the user wants to cancel the launch. If the server is running the script is basically paused in that spot. Once the server is stopped, the script will contiune and ask if the user wants to cancel. The default of these prompts is "no" so once the 10 second timer is up, the script loops back to where it needs to start the server. It also outputs these events into a file called "restart.log", which is just a text file. 

To make it work, one could just create an empty txt file and paste the code in there. After that the txt file would need to be saved as a .bat file (e.g. ServerStart.bat). File extensions need to be visible for it to successfully turn into a .bat file (the sheet icon would turn into a small window icon with a couple of gears in it). If file extensions are not visible, these can usually be turned on from the "View" tab on the top of the explorer window. There is a checkbox called "File name extensions" which needs to be checked. 




 

Link to comment
Share on other sites

19 hours ago, Liiw said:

Thanks for the suggestion! I have not yet used this mod, but it might work. I was thinking about making a stand-alone mod for the autobackup and this would give me the information from where to start. 

But since that will take an unforeseen amount of time (because of life and all other stuff regarding it), I believe it would be possible to use Th3Essentials in the meantime. It seems that this does not backup any additional data, like Xskills for example, but that might not be even that much of an issue. 

Back when I was running a Minecraft modded server, I used a nifty little script to run the server. This script could restart the server when it crashed or was shut down for some other reason. After I set it up in the Windows Startup section, it would even start the server after the PC restarted for whatever reason. I have changed the script a little to work with VS. Namely some echo text and the actual EXE path to be started. 

So this script would handle the restart of the server and the backup could be configured and handled by Th3Essentials mod. 

@choice /t 10 /d N /m "Cancel server start?"
@if %errorlevel% == 1 goto yes
goto start

:start
@echo Starting Vintage Story Server...


@echo (%date% %time%) Starting Server. >> restart.log

:VSStart
"C:\Games\Vintagestory\VintagestoryServer.exe"

@echo.
@echo.
@echo.

@choice /t 10 /d N /m "Stop server start?"
@if %errorlevel% == 1 goto yes
goto no

:no
@echo.
@echo (%date% %time%) Server Crash/Restart. >> restart.log
@echo.
@goto VSStart

:yes

So the line after :VSStart would need to reflect your actual server exe location. 

This script will first ask if the user wants to cancel the launch. If the server is running the script is basically paused in that spot. Once the server is stopped, the script will contiune and ask if the user wants to cancel. The default of these prompts is "no" so once the 10 second timer is up, the script loops back to where it needs to start the server. It also outputs these events into a file called "restart.log", which is just a text file. 

To make it work, one could just create an empty txt file and paste the code in there. After that the txt file would need to be saved as a .bat file (e.g. ServerStart.bat). File extensions need to be visible for it to successfully turn into a .bat file (the sheet icon would turn into a small window icon with a couple of gears in it). If file extensions are not visible, these can usually be turned on from the "View" tab on the top of the explorer window. There is a checkbox called "File name extensions" which needs to be checked. 




 

I am using Th3essentials mod for auto shutdown the server and auto backup map. And Windows task scheduler to run server again.

There is string in mod config to configure that.

 "ShutdownEnabled": true,
  "BackupOnShutdown": true,
  "ShutdownTime": "12:00:00",
  "ShutdownTimes": ["12:00:00", "04:00:00"],
  "ShutdownAnnounce": [1,3,5,10,20,30],

I don´t know how change the path to output backup, that´s the downside.

 

 

 

Link to comment
Share on other sites

Getting some oddness with the script, it does the messages, shuts down and restarts server but seemingly does not do the backup itself.

set savePath="E:\VintageStoryData\Saves"
set backupPath="E:\VSBackups\"
set exePath="E:\Vintagestory\VintagestoryServer.exe"
set EXE=VintagestoryServer.exe

The VSBackups folder exists, did it with a trailing slash as thought that might have been the issue but it was not,

Screenshot of the saves location, same case sensitivity etc just incase that was somehow causing an issue
 

image.png

Link to comment
Share on other sites

@MishkaSnep

I think I know what the problem is. The "\" character is doing funky stuff in the script perhaps.

Please try the following:

  1. Remove the backshlash from the end of backupPath. So in your case it would look like "E:\VSBackups"
  2. Open up "backuphandler.bat with a text editor. On line 8 there is a set backup= command. Insert the backslash between the two percent signs of backupPath and date. So it would look like this:
set backup="%backupPath%\%date%_%time:~0,2%_%time:~3,2%.zip"

Perhaps this will work out.

Link to comment
Share on other sites

31 minutes ago, Liiw said:

@MishkaSnep

I think I know what the problem is. The "\" character is doing funky stuff in the script perhaps.

Please try the following:

  1. Remove the backshlash from the end of backupPath. So in your case it would look like "E:\VSBackups"
  2. Open up "backuphandler.bat with a text editor. On line 8 there is a set backup= command. Insert the backslash between the two percent signs of backupPath and date. So it would look like this:
set backup="%backupPath%\%date%_%time:~0,2%_%time:~3,2%.zip"

Perhaps this will work out.

Nope still the same, server reboots doing the messages all fine but just does not do the backup

Just to check, what should the file end up looking like?

If I manually dump this into powershell it does start generating a file before erroring with stream is too long (the save file is 2.88GB), even just right clicking the folder and doing send to compressed file takes a while though it does shrink it down considerably

Powershell Compress-Archive -LiteralPath "E:\VintageStoryData\Saves" -DestinationPath "E:\VSBackups\backuptest.zip"

could it be worth having a version that just copies the "Saves" folder and renames it? ie renames it to "Saves - 22.02.2024 11.55", would be faster but use more space?

Edit, changed the the backuphandler part to this and it copies the folder renaming it without issue, so think its just an issue with the compressing side, though mine is hardcoded its just for testing

powershell copy-item "E:\VintageStoryData\Saves" -destination "E:\VSBackups\Saves-$(Get-Date -format "yyyy_MM_dd_hh_mm_ss")" -recurse

I am on the Discord as "Mishka" if that might help working this out rather than back n forth on here? :)

Edited by MishkaSnep
Link to comment
Share on other sites

11 minutes ago, MishkaSnep said:

Nope still the same, server reboots doing the messages all fine but just does not do the backup

Just to check, what should the file end up looking like?

If I manually dump this into powershell it does start generating a file before erroring with stream is too long (the save file is 2.88GB), even just right clicking the folder and doing send to compressed file takes a while though it does shrink it down considerably

Powershell Compress-Archive -LiteralPath "E:\VintageStoryData\Saves" -DestinationPath "E:\VSBackups\backuptest.zip"

could it be worth having a version that just copies the "Saves" folder and renames it? ie renames it to "Saves - 22.02.2024 11.55", would be faster but use more space?

I am on the Discord as "Mishka" if that might help working this out rather than back n forth on here? :)

I am a bit busy with work at the moment so cannot come to Discord right now. 

The file should be called [Date]_[TimeH]_[TimeMin].zip. For example 22.02.2024_13_29.zip. The date format depends of your regional settings etc.

I have not considered that the file indeed could be too large for powershell to handle it. So the copying and renaming option would probably work better in this case. I will look into it later on how to handle this. Perhaps there is a workaround to be able to compress it still.

If you have the time, you could delete the "@echo off" from the start of backuphandler.bat. Then in BackupManager.bat, on line 43 replace "nul" with "output.txt" so that it would look like this: 

call backuphandler.bat %savePath% %backupPath%>output.txt

This should create a file called output.txt in the same folder where the scripts are at and this file should contain the powershell error message. Post this file here please.

Link to comment
Share on other sites

  • 1 month later...
On 2/17/2024 at 2:35 PM, Liiw said:

Apparently getting this script working via Task Scheduler on a headless machine is not that straightforward. It is running properly when I am connected to the machine via Remote Desktop, but refuses to run while I'm not connected. I've tried multiple ways, but it will not cooperate. It seems to be a problem with Powershell and GUI interaction while there is no display output. I'm gonna look for alternative solutions to the backup issue. 

I have same problem, did you solve it?

Edited by Murlok
Link to comment
Share on other sites

20 hours ago, Murlok said:

I have same problem, did you solve it?

Hello! 

Unfortunately I have not had much luck with this. It might be possible to getting it working by hooking up a monitor to the headless machine or use a HDMI dummy dongle (which some GPU miners use to circumvent the hash rate limitations). 

I ended up using Th3Essentials  mod and running the server with the script I posted earlier. The mod makes a world backup before shutdown, these can be made multiple times a day and it will also give a warning message to the players. The script will then restart the server.

 

On 2/18/2024 at 10:37 PM, Liiw said:

Thanks for the suggestion! I have not yet used this mod, but it might work. I was thinking about making a stand-alone mod for the autobackup and this would give me the information from where to start. 

But since that will take an unforeseen amount of time (because of life and all other stuff regarding it), I believe it would be possible to use Th3Essentials in the meantime. It seems that this does not backup any additional data, like Xskills for example, but that might not be even that much of an issue. 

Back when I was running a Minecraft modded server, I used a nifty little script to run the server. This script could restart the server when it crashed or was shut down for some other reason. After I set it up in the Windows Startup section, it would even start the server after the PC restarted for whatever reason. I have changed the script a little to work with VS. Namely some echo text and the actual EXE path to be started. 

So this script would handle the restart of the server and the backup could be configured and handled by Th3Essentials mod. 

@choice /t 10 /d N /m "Cancel server start?"
@if %errorlevel% == 1 goto yes
goto start

:start
@echo Starting Vintage Story Server...


@echo (%date% %time%) Starting Server. >> restart.log

:VSStart
"C:\Games\Vintagestory\VintagestoryServer.exe"

@echo.
@echo.
@echo.

@choice /t 10 /d N /m "Stop server start?"
@if %errorlevel% == 1 goto yes
goto no

:no
@echo.
@echo (%date% %time%) Server Crash/Restart. >> restart.log
@echo.
@goto VSStart

:yes

So the line after :VSStart would need to reflect your actual server exe location. 

This script will first ask if the user wants to cancel the launch. If the server is running the script is basically paused in that spot. Once the server is stopped, the script will contiune and ask if the user wants to cancel. The default of these prompts is "no" so once the 10 second timer is up, the script loops back to where it needs to start the server. It also outputs these events into a file called "restart.log", which is just a text file. 

To make it work, one could just create an empty txt file and paste the code in there. After that the txt file would need to be saved as a .bat file (e.g. ServerStart.bat). File extensions need to be visible for it to successfully turn into a .bat file (the sheet icon would turn into a small window icon with a couple of gears in it). If file extensions are not visible, these can usually be turned on from the "View" tab on the top of the explorer window. There is a checkbox called "File name extensions" which needs to be checked. 




 

This .bat file can also be configured to run at system startup in case of windows updates or other restarts. 

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.