Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A simple PowerShell script to backup Vintage Story saves
- # Twitter: @Kerbo_
- #
- $ErrorActionPreference = 'SilentlyContinue'
- $vintagestoryDir = $env:APPDATA + "\VintagestoryData\Saves"
- $dateString = $(get-date -f yyy-MM-dd_HH-mm)
- $maxBackups = 10
- Push-Location $vintagestoryDir
- If(!(test-path -PathType container "backups"))
- {
- New-Item -ItemType Directory -Path "backups" | Out-Null
- }
- $saves = Get-ChildItem -Filter *.vcdbs
- foreach($save in $saves) {
- $savename = $save.Name
- $basename = $save.BaseName
- Write-Host "Checking hash: $savename"
- $save_hash = Get-FileHash -Algorithm SHA1 $save
- $backup = Get-ChildItem "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.vcdbs"
- $RET=$?
- if ($RET) {
- Write-Host "Copied $basename.vcdbs to backups\$basename-$dateString.vcdbs" -ForegroundColor yellow
- } else {
- Write-Host "Error! cp returned $RET for $basename.vcdbs" -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