Wdrussll1

Valheim Backup Script - Windows

Feb 15th, 2021 (edited)
1,495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### Valheim data backup script
  2. #Created by Wdrussell1/Demon1337/Casey Barrett
  3. ### Backs up the entire appdata folder including characters/world data
  4.  
  5. ## Note: If you get errors running the script, run the following:
  6. ## Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
  7. #**************************************************************************
  8. ## **************************CONFIGURATION ITEMS***************************
  9. #**************************************************************************
  10.  
  11. # How many backups to keep before pruning older copies
  12. $NumToKeep = 10
  13.  
  14. # Where to save backups (Default is C:\Users\(USERNAME)\Appdata\LocalLow\IronGate\ValheimBackups)
  15. $BackupFolderPath = "$env:USERPROFILE\AppData\LocalLow\IronGate\ValheimBackups"
  16.  
  17. # Name of each unique backup file (This gets today's date and appends the file)
  18. $BackupName = Get-Date -Format "yyyyMMdd-HHmm"
  19.  
  20. # Server world file location (Default is C:\Users\(USERNAME)\Appdata\LocalLow\IronGate\Valheim)
  21. $Worldsavelocation = "$env:USERPROFILE\AppData\LocalLow\IronGate\Valheim"
  22.  
  23. #**************************************************************************
  24. ## ***********************END OF CONFIGURATION ITEMS***********************
  25. #**************************************************************************
  26. #**************************************************************************
  27. #*******************DO NOT MODIFY ANYTHING BEYOND THIS POINT***************
  28. #**************************************************************************
  29. #**************************************************************************
  30. #**************************************************************************
  31.  
  32. ## Main Script
  33.  
  34.  
  35. # Create the backup directory
  36. If(!(test-path $BackupFolderPath))
  37. {
  38.       New-Item -ItemType Directory -Force -Path $BackupFolderPath
  39. }
  40.  
  41. ## Copy/Compress Valheim folder
  42. # Copy/Compress Worlds
  43. foreach ($file in Get-ChildItem -Recurse "$Worldsavelocation/worlds")
  44. {
  45.   if ((get-date).AddMinutes(-5) -gt $file.CreationTime)
  46.   {
  47.   write-host Creating Archive at "$BackupFolderPath"
  48. Compress-Archive -path "$Worldsavelocation/worlds" -destinationpath $BackupFolderPath\$BackupName.zip -Update
  49. break
  50.   }
  51.   else
  52.   {
  53.   write-host Server recently saved, waiting 5 minutes and taking backup.
  54.   start-sleep -Seconds 300
  55.   write-host Creating Archive at "$BackupFolderPath"
  56. Compress-Archive -path "$Worldsavelocation/worlds" -destinationpath $BackupFolderPath\$BackupName.zip -Update
  57. break
  58. }
  59.  
  60. }
  61. # Copy/Compress Characters adding them to the archive
  62. foreach ($file in Get-ChildItem -Recurse "$Worldsavelocation/characters")
  63. {
  64.   if ((get-date).AddMinutes(-5) -gt $file.CreationTime)
  65.   {
  66.   write-host Creating Archive at "$BackupFolderPath"
  67. Compress-Archive -path "$Worldsavelocation/characters" -destinationpath $BackupFolderPath\$BackupName.zip -Update
  68. break
  69.   }
  70.   else
  71.   {
  72.   write-host Server recently saved, waiting 5 minutes and taking backup.
  73.   start-sleep -Seconds 300
  74.   write-host Creating Archive at "$BackupFolderPath"
  75. Compress-Archive -path "$Worldsavelocation/characters" -destinationpath $BackupFolderPath\$BackupName.zip -Update
  76. break
  77. }
  78.  
  79. }
  80.  
  81. Write-host Backup complete you will find your backup compressed at "$BackupFolderPath" named $BackupName.zip
  82.  
  83. #Prune Backups
  84. write-host pruning backups. You are keeping $NumToKeep  backups
  85.  
  86. Get-ChildItem "$BackupFolderPath" -Recurse| where{-not $_.PsIsContainer}| sort CreationTime -desc| select -Skip "$NumToKeep"| Remove-Item -Force
  87.  
  88. write-host pruning complete
  89.  
Add Comment
Please, Sign In to add comment