Advertisement
Guest User

backup7dtd.ps1

a guest
Jan 17th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Param (
  2.     [Parameter(Mandatory)][String] $World="Navezgane",
  3.     [Parameter(Mandatory)][String] $SaveName,
  4.     [Int] $Seconds=300,
  5.     [Int] $Retention=10
  6. )
  7.  
  8. Function Start-Countdown {
  9.     Param(
  10.         [Int32]$Seconds = 10,
  11.         [string]$Message = "Pausing for 10 seconds..."
  12.     )
  13.     ForEach ($Count in (1..$Seconds)) {
  14.         Write-Progress -Id 1 -Activity $Message -Status "Waiting for $Seconds seconds, $($Seconds - $Count) left" -PercentComplete (($Count / $Seconds) * 100)
  15.         Start-Sleep -Seconds 1
  16.     }
  17.     Write-Progress -Id 1 -Activity $Message -Status "Completed" -PercentComplete 100 -Completed
  18. }
  19.  
  20. $startDTM=Get-Date
  21. $jobid=Get-Date $startDTM -f "yyyyMMdd-HHmmss"
  22. $root="$($env:USERPROFILE)\AppData\Roaming\7DaysToDie"
  23.  
  24. $saves="$($root)\Saves\$($World)"
  25. $backups="$($root)\Backups\$($World)"
  26.  
  27. $source="$($saves)\$($SaveName)"
  28. $targetroot="$($backups)\$($SaveName)"
  29.  
  30. Write-Host "Backups will save in $targetroot every $Seconds seconds" -f white
  31. Write-Host "Press CRTL+C to Exit" -f yellow
  32. while(1){
  33.     $now=Get-Date
  34.     $id=Get-Date $now -f "yyyyMMdd-HHmmss"
  35.     $label=Get-Date $now -f "MM/dd/yyyy hh:mm:ss"
  36.     $target="$($targetroot)\$($SaveName)_$($id)"
  37.     Write-Host "[$($label)] Backing up to $($World)\$($SaveName)_$($id).zip ... " -NoNewLine
  38.     $null=robocopy $source $target /MIR /NFL /NDL /NJH /NJS /R:5 /W:5
  39.     Compress-Archive -Path $target\* -CompressionLevel Optimal -DestinationPath "$($target).zip"
  40.     if(Test-Path "$($target).zip") {
  41.         Write-Host "SUCCESS" -f green
  42.         Remove-Item $target -Recurse -Force
  43.     } else {
  44.         Write-Host "FAILED" -f red
  45.     }
  46.     # Clean up old archives
  47.     Get-ChildItem -Path $targetroot -Recurse -Include *.zip | sort CreationTime -desc | select -skip $Retention | Foreach-Object { del $_.FullName }
  48.     if ($Seconds -gt 0) {
  49.         Start-Countdown -Seconds $Seconds -Message "Press CRTL+C to Exit"
  50.     } else {
  51.         Exit
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement