From: Patrick Canterino Date: Sat, 3 Mar 2018 13:24:09 +0000 (+0100) Subject: - Check if directories are created successfully and send an error message by X-Git-Url: https://git.p6c8.net/dsmonrot.git/commitdiff_plain/0fc1b183684feb6ab0f9cebb949233088e5c575a?ds=inline - Check if directories are created successfully and send an error message by mail if not - Provide some status information in the error mail --- diff --git a/TODO.md b/TODO.md index 2ade9b4..fad0f84 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,7 @@ # TODO - [ ] Collect error messages and send them via email -- [ ] Check if directories are created successfully +- [x] Check if directories are created successfully - [ ] Check if the network drive already exists before connecting - [ ] 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`) diff --git a/dsmonrot.ps1 b/dsmonrot.ps1 index b07d5ef..6a510c9 100644 --- a/dsmonrot.ps1 +++ b/dsmonrot.ps1 @@ -114,11 +114,13 @@ function Rotate-Backup { } } -Write-Host "Started at" (Get-Date -format "yyyy-MM-dd HH:mm:ss") +$startTime = Get-Date -format "yyyy-MM-dd HH:mm:ss" +Write-Host "Started at $startTime" $errorMessages = @() $smbConnected = $False +$doBackup = $False $success = $False if($smbDrive) { @@ -176,27 +178,28 @@ if((Test-Path $backupTarget) -and (Test-Path $backupTargetFull) -and (Test-Path try { New-Item -ItemType directory -Path $backupTargetDiff -ErrorAction Stop - - $success = $True + $doBackup = $True } catch { Write-Host "Could not create directory $backupTargetDiff`: $_.Exception.Message" - exit + $errorMessages += "Could not create directory $backupTargetDiff`: $_.Exception.Message" } - $dsLogPath = if($dsLogFileToBackup) { "$backupTargetDiff\$dsLogFile" } else { $dsLogFile } - - $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetDiff\`$disk.sna", "-h$backupTargetFull\`$disk.hsh") + $dsAdditionalArgs - Write-Host $dsPath ($dsArgs -join " ") - - & $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($doBackup) { + $dsLogPath = if($dsLogFileToBackup) { "$backupTargetDiff\$dsLogFile" } else { $dsLogFile } + + $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetDiff\`$disk.sna", "-h$backupTargetFull\`$disk.hsh") + $dsAdditionalArgs + Write-Host $dsPath ($dsArgs -join " ") + + & $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 + } } } else { @@ -209,35 +212,55 @@ else { if(!(Test-Path $backupTarget)) { Write-Host "Creating directory $backupTarget" - New-Item -ItemType directory -Path $backupTarget + + try { + New-Item -ItemType directory -Path $backupTarget -ErrorAction Stop + } + catch { + Write-Host "Could not create directory $backupTarget`: $_.Exception.Message" + $errorMessages += "Could not create directory $backupTarget`: $_.Exception.Message" + } } if(!(Test-Path $backupTargetFull)) { Write-Host "Creating directory $backupTargetFull" - 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 - Write-Host $dsPath ($dsArgs -join " ") - - & $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" + + 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 { - $success = $True + $doBackup = $True } - if($rotateBeforeBackup -eq $False -and $success -eq $True) { - Rotate-Backup + if($doBackup) { + if($rotateBeforeBackup) { + Rotate-Backup + } + + $dsLogPath = if($dsLogFileToBackup) { "$backupTargetFull\$dsLogFile" } else { $dsLogFile } + + $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetFull\`$disk.sna") + $dsAdditionalArgs + Write-Host $dsPath ($dsArgs -join " ") + + & $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 + } } } @@ -254,7 +277,14 @@ if($smbConnected) { } if($emailOnError -and $errorMessages.Count -gt 0) { - Send-Email ("Error:`n"+($errorMessages -join "`n")) + $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 += "Differential backup: $isDiff`n" + $emailBody += "Backup successfull: $success`n`n" + $emailBody += ($errorMessages -join "`n") + + Send-Email ($emailBody) } -Write-Host "Ended at" (Get-Date -format "yyyy-MM-dd HH:mm:ss") \ No newline at end of file +$endTime = Get-Date -format "yyyy-MM-dd HH:mm:ss" +Write-Host "Ended at $endTime" \ No newline at end of file