Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A simple PowerShell script to backup Railroader saves
- # Twitter: @Kerbo_
- #
- $ErrorActionPreference = 'SilentlyContinue'
- $railroaderDir = $env:APPDATA + "\..\LocalLow\Giraffe Lab LLC\Railroader\Saves"
- $dateString = $(get-date -f yyy-MM-dd_HH-mm)
- $maxBackups = 10
- Push-Location $railroaderDir
- If(!(test-path -PathType container "backups"))
- {
- New-Item -ItemType Directory -Path "backups" | Out-Null
- }
- $saves = Get-ChildItem -Filter *.shortsave
- foreach($save in $saves) {
- $savename = $save.Name
- $basename = $save.BaseName
- if($savename -Like "*_auto*") { continue }
- $save_hash = Get-FileHash -Algorithm SHA1 $save
- $backup = gci "backups\$basename-*" | select -last 1
- $backup_hash = Get-FileHash -Algorithm SHA1 $backup -EV Err -EA SilentlyContinue
- if ( $backup_hash.Hash -eq $save_hash.Hash ) {
- Write-Host "$savename matches latest backup, no backup needed" -ForegroundColor green
- } else {
- cp $savename "backups\$basename-$dateString.shortsave"
- $RET=$?
- if ($RET) {
- Write-Host "Copied $basename.shortsave to backups\$basename-$dateString.shortsave" -ForegroundColor yellow
- } else {
- Write-Host "Error! cp returned $RET for $basename.shortsave" -ForegroundColor red
- }
- }
- # Keep only latest $maxBackups backups
- $c = Get-ChildItem "backups\$basename-*" | measure
- $count = $c.Count
- #Write-Host "Using $count out of $maxBackups backups for $savename"
- If($count -gt $maxBackups) {
- $delcount = $count - $maxBackups
- Write-Host "Cleaning up $delcount old backups for $savename"
- Get-ChildItem "backups\$basename-*" -Recurse | sort CreationTime -desc | select -Skip $maxBackups | Remove-Item -Force
- }
- }
- Pop-Location
- # Sleep to show screen output before exiting
- Start-Sleep -Seconds 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement