]> git.p6c8.net - dsmonrot.git/commitdiff
- Check if directories are created successfully and send an error message by
authorPatrick Canterino <patrick@patrick-canterino.de>
Sat, 3 Mar 2018 13:24:09 +0000 (14:24 +0100)
committerPatrick Canterino <patrick@patrick-canterino.de>
Sat, 3 Mar 2018 13:24:09 +0000 (14:24 +0100)
  mail if not
- Provide some status information in the error mail

TODO.md
dsmonrot.ps1

diff --git a/TODO.md b/TODO.md
index 2ade9b48fe1fa982c76f7ddb9d42111b1e5b4519..fad0f8489261fecf234aba2c7646ab961d8732df 100644 (file)
--- 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`)
index b07d5ef3722452bc3f0f87f366d3d710fdd26c2e..6a510c9caf0a1afc8120744787d6f3b68e6ca050 100644 (file)
@@ -114,11 +114,13 @@ function Rotate-Backup {
        }\r
 }\r
 \r
-Write-Host "Started at" (Get-Date -format "yyyy-MM-dd HH:mm:ss")\r
+$startTime = Get-Date -format "yyyy-MM-dd HH:mm:ss"\r
+Write-Host "Started at $startTime"\r
 \r
 $errorMessages = @()\r
 \r
 $smbConnected = $False\r
+$doBackup = $False\r
 $success = $False\r
 \r
 if($smbDrive) {\r
@@ -176,27 +178,28 @@ if((Test-Path $backupTarget) -and (Test-Path $backupTargetFull) -and (Test-Path
                \r
                try {\r
                        New-Item -ItemType directory -Path $backupTargetDiff -ErrorAction Stop\r
-               \r
-                       $success = $True\r
+                       $doBackup = $True\r
                }\r
                catch {\r
                        Write-Host "Could not create directory $backupTargetDiff`: $_.Exception.Message"\r
-                       exit\r
+                       $errorMessages += "Could not create directory $backupTargetDiff`: $_.Exception.Message"\r
                }\r
                \r
-               $dsLogPath = if($dsLogFileToBackup) { "$backupTargetDiff\$dsLogFile" } else { $dsLogFile }\r
-               \r
-               $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetDiff\`$disk.sna", "-h$backupTargetFull\`$disk.hsh") + $dsAdditionalArgs\r
-               Write-Host $dsPath ($dsArgs -join " ")\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
+               if($doBackup) {\r
+                       $dsLogPath = if($dsLogFileToBackup) { "$backupTargetDiff\$dsLogFile" } else { $dsLogFile }\r
+                       \r
+                       $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetDiff\`$disk.sna", "-h$backupTargetFull\`$disk.hsh") + $dsAdditionalArgs\r
+                       Write-Host $dsPath ($dsArgs -join " ")\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
        }\r
        else {\r
@@ -209,35 +212,55 @@ else {
 \r
        if(!(Test-Path $backupTarget)) {\r
                Write-Host "Creating directory $backupTarget"\r
-               New-Item -ItemType directory -Path $backupTarget\r
+               \r
+               try {\r
+                       New-Item -ItemType directory -Path $backupTarget -ErrorAction Stop\r
+               }\r
+               catch {\r
+                       Write-Host "Could not create directory $backupTarget`: $_.Exception.Message"\r
+                       $errorMessages += "Could not create directory $backupTarget`: $_.Exception.Message"\r
+               }\r
        }\r
        \r
        if(!(Test-Path $backupTargetFull)) {\r
                Write-Host "Creating directory $backupTargetFull"\r
-               New-Item -ItemType directory -Path $backupTargetFull\r
-       }\r
-       \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
-       & $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
+               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
-               $success = $True\r
+               $doBackup = $True\r
        }\r
        \r
-       if($rotateBeforeBackup -eq $False -and $success -eq $True) {\r
-               Rotate-Backup\r
+       if($doBackup) {\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
+               & $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
@@ -254,7 +277,14 @@ if($smbConnected) {
 }\r
 \r
 if($emailOnError -and $errorMessages.Count -gt 0) {\r
-       Send-Email ("Error:`n"+($errorMessages -join "`n"))\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 += "Differential backup: $isDiff`n"\r
+       $emailBody += "Backup successfull:  $success`n`n"\r
+       $emailBody += ($errorMessages -join "`n")\r
+\r
+       Send-Email ($emailBody)\r
 }\r
 \r
-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"\r
+Write-Host "Ended at $endTime"
\ No newline at end of file

patrick-canterino.de