- [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
$errorMessages = @()\r
\r
$smbConnected = $False\r
-$doBackup = $False\r
$success = $False\r
$isDiff = $False\r
$dsCommand = ""\r
\r
try {\r
New-Item -ItemType directory -Path $backupTargetDiff -ErrorAction Stop\r
- $doBackup = $True\r
}\r
catch {\r
Write-Host "Could not create directory $backupTargetDiff`: $_.Exception.Message"\r
$errorMessages += "Could not create directory $backupTargetDiff`: $_.Exception.Message"\r
}\r
\r
- if($doBackup) {\r
+ if($errorMessages.Count -eq 0) {\r
$dsLogPath = if($dsLogFileToBackup) { "$backupTargetDiff\$dsLogFile" } else { $dsLogFile }\r
\r
$dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetDiff\`$disk.sna", "-h$backupTargetFull\`$disk.hsh") + $dsAdditionalArgs\r
}\r
}\r
\r
- if(!(Test-Path $backupTargetFull)) {\r
- Write-Host "Creating directory $backupTargetFull"\r
- \r
- try {\r
- New-Item -ItemType directory -Path $backupTargetFull -ErrorAction Stop\r
- $doBackup = $True\r
- }\r
- catch {\r
- Write-Host "Could not create directory $backupTargetFull`: $_.Exception.Message"\r
- $errorMessages += "Could not create directory $backupTargetFull`: $_.Exception.Message"\r
- }\r
- }\r
- else {\r
- $doBackup = $True\r
- }\r
- \r
- if($doBackup) {\r
- if($rotateBeforeBackup) {\r
- Rotate-Backup\r
+ if($errorMessages.Count -eq 0) {\r
+ if(!(Test-Path $backupTargetFull)) {\r
+ Write-Host "Creating directory $backupTargetFull"\r
+ \r
+ try {\r
+ New-Item -ItemType directory -Path $backupTargetFull -ErrorAction Stop\r
+ }\r
+ catch {\r
+ Write-Host "Could not create directory $backupTargetFull`: $_.Exception.Message"\r
+ $errorMessages += "Could not create directory $backupTargetFull`: $_.Exception.Message"\r
+ }\r
}\r
\r
- $dsLogPath = if($dsLogFileToBackup) { "$backupTargetFull\$dsLogFile" } else { $dsLogFile }\r
+ if($errorMessages.Count -eq 0) {\r
+ if($rotateBeforeBackup) {\r
+ Rotate-Backup\r
+ }\r
+ \r
+ $dsLogPath = if($dsLogFileToBackup) { "$backupTargetFull\$dsLogFile" } else { $dsLogFile }\r
\r
- $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetFull\`$disk.sna") + $dsAdditionalArgs\r
- Write-Host $dsPath ($dsArgs -join " ")\r
- \r
- $dsCommand = "$dsPath $dsArgs"\r
- \r
- & $dsPath $dsArgs\r
- \r
- if($LastExitCode -ne 0) {\r
- Write-Host "Drive Snapshot failed to backup! Exit code: $LastExitCode"\r
- $errorMessages += "Drive Snapshot failed to backup! Exit code: $LastExitCode"\r
- }\r
- else {\r
- $success = $True\r
- }\r
- \r
- if($rotateBeforeBackup -eq $False -and $success -eq $True) {\r
- Rotate-Backup\r
+ $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetFull\`$disk.sna") + $dsAdditionalArgs\r
+ Write-Host $dsPath ($dsArgs -join " ")\r
+ \r
+ $dsCommand = "$dsPath $dsArgs"\r
+ \r
+ & $dsPath $dsArgs\r
+ \r
+ if($LastExitCode -ne 0) {\r
+ Write-Host "Drive Snapshot failed to backup! Exit code: $LastExitCode"\r
+ $errorMessages += "Drive Snapshot failed to backup! Exit code: $LastExitCode"\r
+ }\r
+ else {\r
+ $success = $True\r
+ }\r
+ \r
+ if($rotateBeforeBackup -eq $False -and $success -eq $True) {\r
+ Rotate-Backup\r
+ }\r
}\r
}\r
}\r
if($emailOnError -and $errorMessages.Count -gt 0) {\r
$emailBody = "This is DSMonRot on $env:computername, started at $startTime.`n"\r
$emailBody += "An error occured while performing a backup. Below are the error messages and some status information.`n`n"\r
+ $emailBody += "Backup directory: $backupDir`n"\r
$emailBody += "Differential backup: $isDiff`n"\r
$emailBody += "Backup successful: $success`n"\r
$emailBody += "Drive Snapshot command: $dsCommand`n`n"\r