From 1393228635062138984a33279aadb8f5e77dbed6 Mon Sep 17 00:00:00 2001 From: Patrick Canterino Date: Tue, 6 Mar 2018 14:19:47 +0100 Subject: [PATCH] - Avoid trying to create the "Full" directory when creating month's directory failed - Got rid of $doBackup variable, instead check if there are any error messages recorded --- TODO.md | 2 +- dsmonrot.ps1 | 77 +++++++++++++++++++++++++--------------------------- 2 files changed, 38 insertions(+), 41 deletions(-) diff --git a/TODO.md b/TODO.md index b727f89..84b50f2 100644 --- a/TODO.md +++ b/TODO.md @@ -2,7 +2,7 @@ - [x] Collect error messages and send them via email - [x] Check if directories are created successfully -- [x] ~~Check if the network drive already exists before connecting~~ `New-PSDrive` throws an exception if drive is already connected +- [x] ~~Check if the network drive already exists before connecting~~ ⇒ `New-PSDrive` throws an exception if drive is already connected - [ ] Create a log file for the script - [ ] Clean up the messages sent to the console or send them to the debug or error streams (e.g. `Write-Debug` or `Write-Error`) - [ ] Suppress output of some commands \ No newline at end of file diff --git a/dsmonrot.ps1 b/dsmonrot.ps1 index 5732bd5..0323afb 100644 --- a/dsmonrot.ps1 +++ b/dsmonrot.ps1 @@ -120,7 +120,6 @@ Write-Host "Started at $startTime" $errorMessages = @() $smbConnected = $False -$doBackup = $False $success = $False $isDiff = $False $dsCommand = "" @@ -179,14 +178,13 @@ if($errorMessages.Count -eq 0) { try { New-Item -ItemType directory -Path $backupTargetDiff -ErrorAction Stop - $doBackup = $True } catch { Write-Host "Could not create directory $backupTargetDiff`: $_.Exception.Message" $errorMessages += "Could not create directory $backupTargetDiff`: $_.Exception.Message" } - if($doBackup) { + if($errorMessages.Count -eq 0) { $dsLogPath = if($dsLogFileToBackup) { "$backupTargetDiff\$dsLogFile" } else { $dsLogFile } $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetDiff\`$disk.sna", "-h$backupTargetFull\`$disk.hsh") + $dsAdditionalArgs @@ -225,46 +223,44 @@ if($errorMessages.Count -eq 0) { } } - if(!(Test-Path $backupTargetFull)) { - Write-Host "Creating directory $backupTargetFull" - - try { - New-Item -ItemType directory -Path $backupTargetFull -ErrorAction Stop - $doBackup = $True - } - catch { - Write-Host "Could not create directory $backupTargetFull`: $_.Exception.Message" - $errorMessages += "Could not create directory $backupTargetFull`: $_.Exception.Message" - } - } - else { - $doBackup = $True - } - - if($doBackup) { - if($rotateBeforeBackup) { - Rotate-Backup + if($errorMessages.Count -eq 0) { + if(!(Test-Path $backupTargetFull)) { + Write-Host "Creating directory $backupTargetFull" + + try { + New-Item -ItemType directory -Path $backupTargetFull -ErrorAction Stop + } + catch { + Write-Host "Could not create directory $backupTargetFull`: $_.Exception.Message" + $errorMessages += "Could not create directory $backupTargetFull`: $_.Exception.Message" + } } - $dsLogPath = if($dsLogFileToBackup) { "$backupTargetFull\$dsLogFile" } else { $dsLogFile } + if($errorMessages.Count -eq 0) { + if($rotateBeforeBackup) { + Rotate-Backup + } + + $dsLogPath = if($dsLogFileToBackup) { "$backupTargetFull\$dsLogFile" } else { $dsLogFile } - $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetFull\`$disk.sna") + $dsAdditionalArgs - Write-Host $dsPath ($dsArgs -join " ") - - $dsCommand = "$dsPath $dsArgs" - - & $dsPath $dsArgs - - if($LastExitCode -ne 0) { - Write-Host "Drive Snapshot failed to backup! Exit code: $LastExitCode" - $errorMessages += "Drive Snapshot failed to backup! Exit code: $LastExitCode" - } - else { - $success = $True - } - - if($rotateBeforeBackup -eq $False -and $success -eq $True) { - Rotate-Backup + $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetFull\`$disk.sna") + $dsAdditionalArgs + Write-Host $dsPath ($dsArgs -join " ") + + $dsCommand = "$dsPath $dsArgs" + + & $dsPath $dsArgs + + if($LastExitCode -ne 0) { + Write-Host "Drive Snapshot failed to backup! Exit code: $LastExitCode" + $errorMessages += "Drive Snapshot failed to backup! Exit code: $LastExitCode" + } + else { + $success = $True + } + + if($rotateBeforeBackup -eq $False -and $success -eq $True) { + Rotate-Backup + } } } } @@ -285,6 +281,7 @@ if($smbConnected) { if($emailOnError -and $errorMessages.Count -gt 0) { $emailBody = "This is DSMonRot on $env:computername, started at $startTime.`n" $emailBody += "An error occured while performing a backup. Below are the error messages and some status information.`n`n" + $emailBody += "Backup directory: $backupDir`n" $emailBody += "Differential backup: $isDiff`n" $emailBody += "Backup successful: $success`n" $emailBody += "Drive Snapshot command: $dsCommand`n`n" -- 2.34.1