From c262146f000806d3bed5aa8cd07fa291fafbd0c9 Mon Sep 17 00:00:00 2001 From: Patrick Canterino Date: Sat, 3 Mar 2018 13:51:12 +0100 Subject: [PATCH 1/1] Allow to rotate BEFORE a full backup --- dsmonrot.ps1 | 55 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/dsmonrot.ps1 b/dsmonrot.ps1 index 82b5014..b07d5ef 100644 --- a/dsmonrot.ps1 +++ b/dsmonrot.ps1 @@ -16,6 +16,11 @@ # Keep backup for this amount of months (excluding the current month), # -1 for indefinite [Int32]$keepMonths = 2 +# Rotate BEFORE the beginning of a full backup (default is after a successful +# full backup) +# WARNING: If this option is set to $True and the full backup fails you have +# NO backup +$rotateBeforeBackup = $False # Path to Drive Snapshot [String]$dsPath = "C:\Users\Patrick\Desktop\DSMonRot\snapshot.exe" # Path to Drive Snapshot log file (specify only the file name if you set @@ -85,6 +90,30 @@ function Send-Email([String]$body) { } } +function Rotate-Backup { + if($keepMonths -lt 0) { + return + } + + Write-Host "Rotating" + + $keepMonthsCount = $keepMonths + + Get-ChildItem $backupDir -Directory | Where-Object {($_.Name -ne $currMonth) -and ($_.Name -match "^\d{4,}-\d{2}$")} | Sort-Object -Descending | + Foreach-Object { + Write-Host $_ "=>" $_.FullName + + if($keepMonthsCount -ge 0) { + $keepMonthsCount-- + } + + if($keepMonthsCount -eq -1) { + Write-Host "Deleting $_" + Remove-Item -Recurse -Force $_.FullName + } + } +} + Write-Host "Started at" (Get-Date -format "yyyy-MM-dd HH:mm:ss") $errorMessages = @() @@ -177,7 +206,7 @@ if((Test-Path $backupTarget) -and (Test-Path $backupTargetFull) -and (Test-Path } else { Write-Host "Full backup" - + if(!(Test-Path $backupTarget)) { Write-Host "Creating directory $backupTarget" New-Item -ItemType directory -Path $backupTarget @@ -188,6 +217,10 @@ else { New-Item -ItemType directory -Path $backupTargetFull } + if($rotateBeforeBackup) { + Rotate-Backup + } + $dsLogPath = if($dsLogFileToBackup) { "$backupTargetFull\$dsLogFile" } else { $dsLogFile } $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetFull\`$disk.sna") + $dsAdditionalArgs @@ -202,25 +235,9 @@ else { else { $success = $True } -} - -if(!$isDiff -and $success -eq $True -and $keepMonths -ge 0) { - Write-Host "Rotating" - - $keepMonthsCount = $keepMonths - Get-ChildItem $backupDir -Directory | Where-Object {($_.Name -ne $currMonth) -and ($_.Name -match "^\d{4,}-\d{2}$")} | Sort-Object -Descending | - Foreach-Object { - Write-Host $_ "=>" $_.FullName - - if($keepMonthsCount -ge 0) { - $keepMonthsCount-- - } - - if($keepMonthsCount -eq -1) { - Write-Host "Deleting $_" - Remove-Item -Recurse -Force $_.FullName - } + if($rotateBeforeBackup -eq $False -and $success -eq $True) { + Rotate-Backup } } -- 2.34.1