Advertisement
kerbo_

Railroader-backup.ps1

Dec 2nd, 2023 (edited)
1,869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # A simple PowerShell script to backup Railroader saves
  2. # Twitter: @Kerbo_
  3. #
  4. $ErrorActionPreference = 'SilentlyContinue'
  5. $railroaderDir = $env:APPDATA + "\..\LocalLow\Giraffe Lab LLC\Railroader\Saves"
  6. $dateString = $(get-date -f yyy-MM-dd_HH-mm)
  7. $maxBackups = 10
  8. Push-Location $railroaderDir
  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 *.shortsave
  16.  
  17. foreach($save in $saves) {
  18.     $savename = $save.Name
  19.     $basename = $save.BaseName
  20.    
  21.     if($savename -Like "*_auto*") { continue }
  22.    
  23.     $save_hash = Get-FileHash -Algorithm SHA1 $save
  24.     $backup = gci "backups\$basename-*" | select -last 1
  25.     $backup_hash = Get-FileHash -Algorithm SHA1 $backup -EV Err -EA SilentlyContinue
  26.     if ( $backup_hash.Hash -eq $save_hash.Hash ) {
  27.         Write-Host "$savename matches latest backup, no backup needed" -ForegroundColor green
  28.     } else {
  29.         cp $savename "backups\$basename-$dateString.shortsave"
  30.         $RET=$?
  31.         if ($RET) {
  32.             Write-Host "Copied $basename.shortsave to backups\$basename-$dateString.shortsave" -ForegroundColor yellow
  33.         } else {
  34.             Write-Host "Error! cp returned $RET for $basename.shortsave" -ForegroundColor red
  35.         }
  36.     }
  37.     # Keep only latest $maxBackups backups
  38.     $c = Get-ChildItem "backups\$basename-*" | measure
  39.     $count = $c.Count
  40.     #Write-Host "Using $count out of $maxBackups backups for $savename"
  41.     If($count -gt $maxBackups) {
  42.         $delcount = $count - $maxBackups
  43.         Write-Host "Cleaning up $delcount old backups for $savename"
  44.         Get-ChildItem "backups\$basename-*" -Recurse | sort CreationTime -desc | select -Skip $maxBackups | Remove-Item -Force
  45.     }
  46. }
  47. Pop-Location
  48. # Sleep to show screen output before exiting
  49. Start-Sleep -Seconds 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement