Advertisement
kerbo_

Archean-backup.ps1

Aug 27th, 2024
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # A simple PowerShell script to backup Archean save files
  2. # Twitter: @Kerbo_
  3. #
  4.  
  5. # EDIT TO SUIT YOUR INSTALL AND PREFERRED BACKUP LOCATION
  6. $archeanDir = "C:\Program Files (x86)\Steam\steamapps\common\Archean\Archean-data\server\worlds"
  7. $archeanBackupDir = "C:\savegames\Archean"
  8. # Keep this many backups
  9. $maxBackups = 10
  10.  
  11. $dateString = $(get-date -f yyy-MM-dd_HH-mm)
  12.  
  13. # Create backup directory if it doesn't exist
  14. New-Item -ItemType Directory -Force -Path $archeanBackupDir | Out-Null
  15.  
  16. Push-Location $archeanDir
  17. $saves = Get-ChildItem -Directory
  18. foreach($save in $saves) {
  19.     # Examine latest backup
  20.     Push-Location $archeanBackupDir
  21.     $backup = gci "$save-*" | select -last 1
  22.     if (-not ([string]::IsNullOrEmpty($backup))) {
  23.         # New temp dir
  24.         $parent = [System.IO.Path]::GetTempPath()
  25.         [string] $name = [System.Guid]::NewGuid()
  26.         $tempDir = New-Item -ItemType Directory -Path (Join-Path $parent $name)
  27.         # Extract and get hash
  28.         Expand-Archive -Path $backup -DestinationPath $tempDir
  29.         $backup_hash = Get-FileHash $tempDir\state.worldstate
  30.         # Remove temp dir
  31.         Remove-Item -LiteralPath $tempDir -Force -Recurse
  32.     }
  33.     Pop-Location
  34.  
  35.     if(Test-Path -Path "$save\state.worldstate" -PathType Leaf) {
  36.         $current_hash = Get-FileHash "$save\state.worldstate"
  37.         if ( $backup_hash.Hash -eq $current_hash.Hash ) {
  38.             Write-Host "$save matches latest backup, no backup needed"
  39.         } else {
  40.             Write-Host "Backing up to $archeanBackupDir\$save-$dateString.zip"
  41.             Compress-Archive -Path $save\* -DestinationPath "$archeanBackupDir\$save-$dateString.zip"
  42.         }
  43.     }
  44.    
  45.     # Keep only latest $maxBackups backups
  46.     Push-Location $archeanBackupDir
  47.     $c = Get-ChildItem "$save-*" | measure
  48.     $count = $c.Count
  49.     Write-Host "Using $count out of $maxBackups backups for $save"
  50.     If($count -gt $maxBackups) {
  51.         $delcount = $count - $maxBackups
  52.         Write-Host "Cleaning up $delcount old backups for $save"
  53.         Get-ChildItem "$save-*" -Recurse | sort CreationTime -desc | select -Skip $maxBackups | Remove-Item -Force
  54.     }
  55.     Pop-Location
  56. }
  57. Pop-Location
  58. Write-Host "Done"
  59. Start-Sleep -Seconds 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement