dumpring

Valheim Data Backup Script

Feb 12th, 2021 (edited)
839
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### Valheim data backup script
  2. ### Backs up the entire appdata folder including characters/world data
  3.  
  4. ## Note: If you get errors running the script, run the following:
  5. ## Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
  6.  
  7. ## Configuration
  8. # How many backups to keep before pruning older copies
  9. # 0 keeps all backups
  10. $NumToKeep = 7
  11.  
  12. # Send pruned backups to recycle bin (false means permanent delete)
  13. $UseRecycleBin = $true
  14.  
  15. # Where to save backups (default: yourdesktop\Valheim_Backups)
  16. $BackupFolderPath = "$env:USERPROFILE\Desktop\Valheim_Backups"
  17.  
  18. # Name of each unique backup folder (default yyyyMMdd-HHmm, e.g. 20210212-1238)
  19. $FolderName = Get-Date -Format "yyyyMMdd-HHmm"
  20.  
  21. ## Main Script (modify at own risk)
  22. $DestinationPath = "$BackupFolderPath\$FolderName"
  23.  
  24. # Create the backup directory
  25. if ((Test-Path $DestinationPath) -eq $false) {
  26.     New-Item `
  27.         -Path $BackupFolderPath `
  28.         -Name $FolderName `
  29.         -ItemType "directory" | Out-Null
  30. }
  31.  
  32. # Copy valheim appdata
  33. Copy-Item `
  34.     "$env:USERPROFILE\AppData\LocalLow\IronGate\Valheim\*" `
  35.     -Destination $DestinationPath `
  36.     -Force -Recurse
  37.  
  38. # Prune old backups
  39. if ($NumToKeep -gt 0) {
  40.     $ExistingBackups = Get-ChildItem -Path $BackupFolderPath -Directory | Sort-Object -Descending
  41.     for ($i = 0; $i -lt $ExistingBackups.Count; $i++) {
  42.         if ($i -gt ($NumToKeep - 1)) {
  43.             if ($UseRecycleBin) {
  44.                 Add-Type -AssemblyName Microsoft.VisualBasic
  45.                 [Microsoft.VisualBasic.FileIO.FileSystem]::Deletedirectory(
  46.                         $ExistingBackups[$i],
  47.                         'OnlyErrorDialogs',
  48.                         'SendToRecycleBin'
  49.                         )            
  50.             } else {
  51.                 Remove-Item $ExistingBackups[$i] -Recurse -Force
  52.             }
  53.         }
  54.     }
  55. }
RAW Paste Data