Skip to content

Instantly share code, notes, and snippets.

@branneman
Last active September 18, 2024 10:13
Show Gist options
  • Save branneman/912d75e8f84247721b5550df06bf333c to your computer and use it in GitHub Desktop.
Save branneman/912d75e8f84247721b5550df06bf333c to your computer and use it in GitHub Desktop.
$source = "C:\Program Files (x86)\World of Warcraft\_classic_era_\Screenshots"
$destination = "C:\Users\branv\Proton Drive\bran.van.der.meer\My files\70 - Backups\wow-classic\Screenshots"
$zipFolder1 = "C:\Program Files (x86)\World of Warcraft\_classic_era_\Interface"
$zipFolder2 = "C:\Program Files (x86)\World of Warcraft\_classic_era_\WTF"
$zipLocation = "C:\Users\branv\Proton Drive\bran.van.der.meer\My files\70 - Backups\wow-classic\"
$zipDate = Get-Date -Format "yyyy-MM-dd"
$zipFileName = "$zipLocation\wowclassic-backup-$zipDate.zip"
if (!(Test-Path -Path $destination)) {
New-Item -ItemType Directory -Path $destination
}
# /E copies all subdirectories, including empty ones
# /Z ensures that the copy operation can resume after an interruption
# /R:3 retries 3 times in case of failures
# /W:10 waits 10 seconds between retries
Robocopy $source $destination /E /Z /R:3 /W:10
if (Test-Path -Path $zipFileName) {
Write-Host "ZIP backup already exist for today, skipping."
} elseif (!(Test-Path -Path $zipFolder1)) {
Write-Host "Folder1 does not exist!"
} elseif (!(Test-Path -Path $zipFolder2)) {
Write-Host "Folder2 does not exist!"
} else {
Compress-Archive -Path $zipFolder1, $zipFolder2 -DestinationPath $zipFileName
Write-Host "ZIP file created: $zipFileName"
}
$zipFiles = Get-ChildItem -Path $zipLocation -Filter "wowclassic-backup-*.zip"
$sortedFiles = $zipFiles | Sort-Object {
$fileName = $_.Name
$dateString = $fileName -replace 'wowclassic-backup-|\.zip',''
[datetime]::ParseExact($dateString, 'yyyy-MM-dd', $null)
}
if ($sortedFiles.Count -gt 3) {
$filesToDelete = $sortedFiles | Select-Object -First ($sortedFiles.Count - 3)
foreach ($file in $filesToDelete) {
Remove-Item $file.FullName
Write-Host "Deleted old backup file: $($file.Name)"
}
}
Write-Host "Done."
Pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment