From: Patrick Canterino Date: Tue, 6 Mar 2018 13:19:47 +0000 (+0100) Subject: - Avoid trying to create the "Full" directory when creating month's X-Git-Url: https://git.p6c8.net/dsmonrot.git/commitdiff_plain/1393228635062138984a33279aadb8f5e77dbed6?ds=sidebyside;hp=dc31e36391307b87cd67ab26cb1c5a69d83d5e41 - 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 --- 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"