X-Git-Url: https://git.p6c8.net/dsmonrot.git/blobdiff_plain/7671200561ed4b6c2ee57d1a602b1db476083920..c262146f000806d3bed5aa8cd07fa291fafbd0c9:/dsmonrot.ps1 diff --git a/dsmonrot.ps1 b/dsmonrot.ps1 index 353e0db..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 @@ -46,7 +51,7 @@ # To address of email notification [String]$emailToAddress = "patrick@test.local" # Subject of email notification -[String]$emailSubject = "DSMonRot" +[String]$emailSubject = "DSMonRot on $env:computername" # Mail server [String]$emailMailserver = "localhost" # SMTP Port @@ -70,21 +75,47 @@ function Send-Email([String]$body) { Write-Host "Sending email: $emailToAddress, $body" try { - $smtp = New-Object System.Net.Mail.SmtpClient($emailMailServer, $emailPort); + $smtp = New-Object System.Net.Mail.SmtpClient($emailMailServer, $emailPort) $smtp.EnableSSL = $emailSSL if($emailAuth) { - $smtp.Credentials = New-Object System.Net.NetworkCredential($emailUser, $emailPassword); + $smtp.Credentials = New-Object System.Net.NetworkCredential($emailUser, $emailPassword) } - $smtp.Send($emailFromAddress, $emailToAddress, $emailSubject, $body); + $smtp.Send($emailFromAddress, $emailToAddress, $emailSubject, $body) } catch { Write-Host "Could not send email: $_.Exception.Message" } } +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 = @() $smbConnected = $False @@ -111,7 +142,7 @@ if($smbDrive) { $smbConnected = $True } Catch { - Write-Host "Could not connect to network drive: $_.Exception.Message" + Write-Host "Could not connect to network drive $smbDrive`: $_.Exception.Message" exit } } @@ -161,18 +192,21 @@ if((Test-Path $backupTarget) -and (Test-Path $backupTargetFull) -and (Test-Path & $dsPath $dsArgs if($LastExitCode -ne 0) { - Write-Host "Error code: $LastExitCode" + Write-Host "Drive Snapshot failed to backup! Exit code: $LastExitCode" + $errorMessages += "Drive Snapshot failed to backup! Exit code: $LastExitCode" + } + else { + $success = $True } } else { Write-Host "Directory $backupTargetDiff already exists!" - $errorMessages += "Directory $backupTargetDiff already exists!" } } else { Write-Host "Full backup" - + if(!(Test-Path $backupTarget)) { Write-Host "Creating directory $backupTarget" New-Item -ItemType directory -Path $backupTarget @@ -183,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 @@ -191,39 +229,32 @@ else { & $dsPath $dsArgs if($LastExitCode -ne 0) { - Write-Host "Error code: $LastExitCode" + Write-Host "Drive Snapshot failed to backup! Exit code: $LastExitCode" + $errorMessages += "Drive Snapshot failed to backup! Exit code: $LastExitCode" + } + else { + $success = $True } - $success = $False -} - -if($isDiff -eq $False -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-- - } - - Write-Host $keepMonthsCount - - if($keepMonthsCount -eq -1) { - Write-Host "Deleting $_" - Remove-Item -Recurse -Force $_.FullName - } + if($rotateBeforeBackup -eq $False -and $success -eq $True) { + Rotate-Backup } } if($smbConnected) { Write-Host "Disconnecting network drive" - Remove-PSDrive $smbDrive + + try { + Remove-PSDrive $smbDrive -ErrorAction Stop + } + catch { + Write-Host "Could not disconnect network drive $smbDrive`: $_.Exception.Message" + $errorMessages += "Could not disconnect network drive $smbDrive`: $_.Exception.Message" + } } if($emailOnError -and $errorMessages.Count -gt 0) { Send-Email ("Error:`n"+($errorMessages -join "`n")) -} \ No newline at end of file +} + +Write-Host "Ended at" (Get-Date -format "yyyy-MM-dd HH:mm:ss") \ No newline at end of file