Nocturnalverse

Valheim Backup Batch File

Feb 23rd, 2021 (edited)
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 4.54 KB | None | 0 0
  1. @echo off
  2. :: By using this batch file you acknowledge that it's not my fault if you manage to screw something up.
  3. :: You should read the comments in this file.
  4. :: Copy this entire thing to a text editor and save it as the type "all files" and add a .bat extension on the end.
  5. :: Example: ValheimBackup.bat
  6. :: You'll have to manually delete excess backups.
  7.  
  8. :: For the zip function to work you will need to install 7zip. It's safe. It's good. It's free. It's open source.
  9. :: https://www.7-zip.org/
  10.  
  11.  
  12.  
  13. :: You need to set three variables, _sourceFolder, _backupFolder, and _7zPath. Then you are done.
  14. :: The variables represent the source directory of the save files,
  15. :: the destination directory for the zip file,
  16. :: and the location of your 7zip installation.
  17.  
  18. :: _sourceFolder probably just needs a user name change.
  19. :: It will have a Valheim Folder inside it.
  20. :: You can probably just change the user name part of the path to whatever your directory is called.
  21. set _sourceFolder=C:\Users\FakeUserName\AppData\LocalLow\IronGate
  22.  
  23. :: _backupFolder is where the settings and save data will be copied to and, from there, moved into a zip file.
  24. :: You can probably just change the user name part of the path here as well.
  25. set _backupFolder=C:\Libraries\FakeUserName\Documents\Save Backups\Valheim Backup
  26.  
  27. :: _7zPath is where you installed 7zip.
  28. :: The default installation directory is: C:\Program Files\7-Zip
  29. set _7zPath=C:\Programs\7-Zip
  30.  
  31. :: STOP. No further changes needed.
  32.  
  33. :: EXIT potentially. Rudimentary path and previous attempt validation.
  34. :: If the temporary directory this batch file creates already exists in backupFolder, warn the user and exit.
  35. :: Remove this section if you remove the 7zip section.
  36. IF EXIST "%_backupFolder%\TempSavesBackup" (
  37. echo ##################################################################
  38. echo ERROR: The folder TempSavesBackup is in your backup directory.
  39. echo        This means that the folder was not deleted as it should have been on the previous execution.
  40. echo        You should probably figure out what went wrong and save or delete the folder at your descretion.
  41. echo ##################################################################
  42. echo:
  43. echo Source      : %_sourceFolder%
  44. echo Destination : %_backupFolder%
  45. echo:
  46. pause
  47. exit
  48. )
  49.  
  50. :: Date and time formatting: YYYY_MM_DD_HH_MM
  51. set hour=%time:~0,2%
  52. if "%hour:~0,1%" == " " set hour=0%time:~1,1%
  53. set _filename=%date:~10,4%_%date:~4,2%_%date:~7,2%_%hour%_%time:~3,2%_%time:~6,2%_Saves.zip
  54.  
  55. :: Display source and destination
  56. echo Backing up Valheim save...
  57. echo:
  58. echo Source Folder: %_sourceFolder%
  59. echo Destination: %_backupFolder%\%_filename%
  60. echo:
  61.  
  62. :: This preserves any Read Only status and copies COTW's saves and settings folders and contents to backupFolder\TempSavesBackup
  63. :: Also verifies that they are identical to the source as they are copied.
  64. :: Forces exit if the copy fails.
  65. @Xcopy /i /s /e /v /k "%_sourceFolder%" "%_backupFolder%\TempSavesBackup"
  66. ::@Xcopy /i /s /e /v /k "%_sourceFolder%" "%_backupFolder%\TempSavesBackup"
  67. echo:
  68. if "%ERRORLEVEL%" == "0" (
  69. echo         Copy Succeeded!
  70. ) else (
  71. echo ##########################
  72. echo ##### Copy FAILED! #######
  73. echo ##########################
  74. echo:
  75. pause
  76. exit
  77. )
  78.  
  79. :: Any section below here can be deleted if you don't want it.
  80. :: If you do delete this 7zip portion, go back up to the top and delete that validation section.
  81. :: You will still end up with a TempSavesBackup folder in your backupFolder directory to do with as you please.
  82. :: This moves the newly created folder's contents into a zip file and then removes the folder.
  83. @"%_7zPath%\7z" a -sdel "%_backupFolder%\%_filename%" "%_backupFolder%\TempSavesBackup\*"
  84. RD "%_backupFolder%\TempSavesBackup"
  85. echo:
  86. if "%ERRORLEVEL%" == "0" (
  87. echo         Zip Succeeded!
  88. ) else (
  89. echo ##########################
  90. echo ##### Zip FAILED! ########
  91. echo ##########################
  92. )
  93. echo:
  94. IF EXIST "%_backupFolder%\TempSavesBackup" (
  95. echo ##################################################################
  96. echo ERROR: The folder TempSavesBackup is still in your backup directory.
  97. echo        This means that the folder was not deleted as it should have been.
  98. echo        Files might still be there that should have been moved by 7zip.
  99. echo        You should probably figure out what went wrong, and save or delete the folder at your discretion.
  100. echo ##################################################################
  101. ) else (
  102. echo         TempSavesBackup was removed properly!
  103. )
  104. echo:
  105. :: Require a key press to close the command window.
  106. pause
Add Comment
Please, Sign In to add comment