Advertisement
kerbo_

VintageStory-backup.ps1

Mar 21st, 2024 (edited)
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # A simple PowerShell script to backup Vintage Story saves
  2. # Twitter: @Kerbo_
  3. #
  4. $ErrorActionPreference = 'SilentlyContinue'
  5. $vintagestoryDir = $env:APPDATA + "\VintagestoryData\Saves"
  6. $dateString = $(get-date -f yyy-MM-dd_HH-mm)
  7. $maxBackups = 10
  8. Push-Location $vintagestoryDir
  9.  
  10. If(!(test-path -PathType container "backups"))
  11. {
  12.       New-Item -ItemType Directory -Path "backups" | Out-Null
  13. }
  14.  
  15. $saves = Get-ChildItem -Filter *.vcdbs
  16.  
  17. foreach($save in $saves) {
  18.     $savename = $save.Name
  19.     $basename = $save.BaseName
  20.    
  21.     Write-Host "Checking hash: $savename"
  22.     $save_hash = Get-FileHash -Algorithm SHA1 $save
  23.     $backup = Get-ChildItem "backups\$basename-*" | select -last 1
  24.     $backup_hash = Get-FileHash -Algorithm SHA1 $backup -EV Err -EA SilentlyContinue
  25.     if ( $backup_hash.Hash -eq $save_hash.Hash ) {
  26.         Write-Host "$savename matches latest backup, no backup needed" -ForegroundColor green
  27.     } else {
  28.         cp $savename "backups\$basename-$dateString.vcdbs"
  29.         $RET=$?
  30.         if ($RET) {
  31.             Write-Host "Copied $basename.vcdbs to backups\$basename-$dateString.vcdbs" -ForegroundColor yellow
  32.         } else {
  33.             Write-Host "Error! cp returned $RET for $basename.vcdbs" -ForegroundColor red
  34.         }
  35.     }
  36.     # Keep only latest $maxBackups backups
  37.     $c = Get-ChildItem "backups\$basename-*" | measure
  38.     $count = $c.Count
  39.     #Write-Host "Using $count out of $maxBackups backups for $savename"
  40.     If($count -gt $maxBackups) {
  41.         $delcount = $count - $maxBackups
  42.         Write-Host "Cleaning up $delcount old backups for $savename"
  43.         Get-ChildItem "backups\$basename-*" -Recurse | sort CreationTime -desc | select -Skip $maxBackups | Remove-Item -Force
  44.     }
  45. }
  46. Pop-Location
  47. # Sleep to show screen output before exiting
  48. Start-Sleep -Seconds 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement