# Keep backup for this amount of months (excluding the current month),\r
# -1 for indefinite\r
[Int32]$keepMonths = 2\r
+# Rotate BEFORE the beginning of a full backup (default is after a successful\r
+# full backup)\r
+# WARNING: If this option is set to $True and the full backup fails you have\r
+# NO backup\r
+$rotateBeforeBackup = $False\r
# Path to Drive Snapshot\r
[String]$dsPath = "C:\Users\Patrick\Desktop\DSMonRot\snapshot.exe"\r
# Path to Drive Snapshot log file (specify only the file name if you set\r
}\r
}\r
\r
+function Rotate-Backup {\r
+ if($keepMonths -lt 0) {\r
+ return\r
+ }\r
+\r
+ Write-Host "Rotating"\r
+ \r
+ $keepMonthsCount = $keepMonths\r
+ \r
+ Get-ChildItem $backupDir -Directory | Where-Object {($_.Name -ne $currMonth) -and ($_.Name -match "^\d{4,}-\d{2}$")} | Sort-Object -Descending |\r
+ Foreach-Object {\r
+ Write-Host $_ "=>" $_.FullName\r
+ \r
+ if($keepMonthsCount -ge 0) {\r
+ $keepMonthsCount--\r
+ }\r
+ \r
+ if($keepMonthsCount -eq -1) {\r
+ Write-Host "Deleting $_"\r
+ Remove-Item -Recurse -Force $_.FullName\r
+ }\r
+ }\r
+}\r
+\r
Write-Host "Started at" (Get-Date -format "yyyy-MM-dd HH:mm:ss")\r
\r
$errorMessages = @()\r
}\r
else {\r
Write-Host "Full backup"\r
- \r
+\r
if(!(Test-Path $backupTarget)) {\r
Write-Host "Creating directory $backupTarget"\r
New-Item -ItemType directory -Path $backupTarget\r
New-Item -ItemType directory -Path $backupTargetFull\r
}\r
\r
+ if($rotateBeforeBackup) {\r
+ Rotate-Backup\r
+ }\r
+ \r
$dsLogPath = if($dsLogFileToBackup) { "$backupTargetFull\$dsLogFile" } else { $dsLogFile }\r
\r
$dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetFull\`$disk.sna") + $dsAdditionalArgs\r
else {\r
$success = $True\r
}\r
-}\r
-\r
-if(!$isDiff -and $success -eq $True -and $keepMonths -ge 0) {\r
- Write-Host "Rotating"\r
- \r
- $keepMonthsCount = $keepMonths\r
\r
- Get-ChildItem $backupDir -Directory | Where-Object {($_.Name -ne $currMonth) -and ($_.Name -match "^\d{4,}-\d{2}$")} | Sort-Object -Descending |\r
- Foreach-Object {\r
- Write-Host $_ "=>" $_.FullName\r
- \r
- if($keepMonthsCount -ge 0) {\r
- $keepMonthsCount--\r
- }\r
- \r
- if($keepMonthsCount -eq -1) {\r
- Write-Host "Deleting $_"\r
- Remove-Item -Recurse -Force $_.FullName\r
- }\r
+ if($rotateBeforeBackup -eq $False -and $success -eq $True) {\r
+ Rotate-Backup\r
}\r
}\r
\r