]> git.p6c8.net - dsmonrot.git/commitdiff
- Now all error messages are sent by mail
authorPatrick Canterino <patrick@patrick-canterino.de>
Tue, 6 Mar 2018 13:08:34 +0000 (14:08 +0100)
committerPatrick Canterino <patrick@patrick-canterino.de>
Tue, 6 Mar 2018 13:08:34 +0000 (14:08 +0100)
- Include executed Drive Snapshot command (if available) in error mail

TODO.md
dsmonrot.ps1

diff --git a/TODO.md b/TODO.md
index fad0f8489261fecf234aba2c7646ab961d8732df..b727f891ccd4f8d5c95df85d5805f190f1050b25 100644 (file)
--- a/TODO.md
+++ b/TODO.md
@@ -1,8 +1,8 @@
 # TODO
 
 # TODO
 
-- [ ] Collect error messages and send them via email
+- [x] Collect error messages and send them via email
 - [x] Check if directories are created successfully
 - [x] Check if directories are created successfully
-- [ ] Check if the network drive already exists before connecting
+- [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
 - [ ] 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
index 6a510c9caf0a1afc8120744787d6f3b68e6ca050..5732bd500eab9585398e0289b953f876d67ebd48 100644 (file)
@@ -18,8 +18,8 @@
 [Int32]$keepMonths = 2\r
 # Rotate BEFORE the beginning of a full backup (default is after a successful\r
 # full backup)\r
 [Int32]$keepMonths = 2\r
 # Rotate BEFORE the beginning of a full backup (default is after a successful\r
 # full backup)\r
-# WARNING: If this option is set to $True and the full backup fails you have\r
-# NO backup\r
+# WARNING: If this option is set to $True and the full backup fails you could\r
+# have NO backup\r
 $rotateBeforeBackup = $False\r
 # Path to Drive Snapshot\r
 [String]$dsPath = "C:\Users\Patrick\Desktop\DSMonRot\snapshot.exe"\r
 $rotateBeforeBackup = $False\r
 # Path to Drive Snapshot\r
 [String]$dsPath = "C:\Users\Patrick\Desktop\DSMonRot\snapshot.exe"\r
@@ -122,9 +122,11 @@ $errorMessages = @()
 $smbConnected = $False\r
 $doBackup = $False\r
 $success = $False\r
 $smbConnected = $False\r
 $doBackup = $False\r
 $success = $False\r
+$isDiff = $False\r
+$dsCommand = ""\r
 \r
 if($smbDrive) {\r
 \r
 if($smbDrive) {\r
-       Try {\r
+       try {\r
                Write-Host "Connecting network drive"\r
                \r
                if($smbUser -and $smbPassword) {\r
                Write-Host "Connecting network drive"\r
                \r
                if($smbUser -and $smbPassword) {\r
@@ -143,54 +145,114 @@ if($smbDrive) {
                \r
                $smbConnected = $True\r
        }\r
                \r
                $smbConnected = $True\r
        }\r
-       Catch {\r
+       catch {\r
                Write-Host "Could not connect to network drive $smbDrive`: $_.Exception.Message"\r
                Write-Host "Could not connect to network drive $smbDrive`: $_.Exception.Message"\r
-               exit\r
+               $errorMessages += "Could not connect to network drive $smbDrive`: $_.Exception.Message"\r
        }\r
 }\r
 \r
 if(!(Test-Path $backupDir)) {\r
        Write-Host "Directory $backupDir does not exist!"\r
        }\r
 }\r
 \r
 if(!(Test-Path $backupDir)) {\r
        Write-Host "Directory $backupDir does not exist!"\r
-       exit\r
+       $errorMessages += "Directory $backupDir does not exist!"\r
 }\r
 \r
 }\r
 \r
-$currMonth = Get-Date -format "yyyy-MM"\r
-$currDay = Get-Date -format "yyyy-MM-dd"\r
+if($errorMessages.Count -eq 0) {\r
+       $currMonth = Get-Date -format "yyyy-MM"\r
+       $currDay = Get-Date -format "yyyy-MM-dd"\r
 \r
 \r
-Write-Host $currMonth\r
+       Write-Host $currMonth\r
 \r
 \r
-$backupTarget = $backupDir + "\" + $currMonth\r
-$backupTargetFull = $backupTarget + "\" + "Full"\r
+       $backupTarget = $backupDir + "\" + $currMonth\r
+       $backupTargetFull = $backupTarget + "\" + "Full"\r
 \r
 \r
-$backupTargetDiff = $backupTarget + "\" + "Diff-" + $currDay\r
+       $backupTargetDiff = $backupTarget + "\" + "Diff-" + $currDay\r
 \r
 \r
-Write-Host $backupTarget\r
+       Write-Host $backupTarget\r
 \r
 \r
-$isDiff = $False\r
+       if((Test-Path $backupTarget) -and (Test-Path $backupTargetFull) -and (Test-Path "$backupTargetFull\*.hsh")) {\r
+               Write-Host "Differential backup"\r
+               \r
+               $isDiff = $True\r
+               \r
+               if(!(Test-Path $backupTargetDiff)) {\r
+                       Write-Host "Creating directory $backupTargetDiff"\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
+                               $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
+                               $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
+               }\r
+               else {\r
+                       Write-Host "Directory $backupTargetDiff already exists!"\r
+                       $errorMessages += "Directory $backupTargetDiff already exists!"\r
+               }\r
+       }\r
+       else {\r
+               Write-Host "Full backup"\r
 \r
 \r
-if((Test-Path $backupTarget) -and (Test-Path $backupTargetFull) -and (Test-Path "$backupTargetFull\*.hsh")) {\r
-       Write-Host "Differential backup"\r
-       \r
-       $isDiff = $True\r
-       \r
-       if(!(Test-Path $backupTargetDiff)) {\r
-               Write-Host "Creating directory $backupTargetDiff"\r
+               if(!(Test-Path $backupTarget)) {\r
+                       Write-Host "Creating directory $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
                \r
-               try {\r
-                       New-Item -ItemType directory -Path $backupTargetDiff -ErrorAction Stop\r
-                       $doBackup = $True\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
                }\r
-               catch {\r
-                       Write-Host "Could not create directory $backupTargetDiff`: $_.Exception.Message"\r
-                       $errorMessages += "Could not create directory $backupTargetDiff`: $_.Exception.Message"\r
+               else {\r
+                       $doBackup = $True\r
                }\r
                \r
                if($doBackup) {\r
                }\r
                \r
                if($doBackup) {\r
-                       $dsLogPath = if($dsLogFileToBackup) { "$backupTargetDiff\$dsLogFile" } else { $dsLogFile }\r
+                       if($rotateBeforeBackup) {\r
+                               Rotate-Backup\r
+                       }\r
                        \r
                        \r
-                       $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetDiff\`$disk.sna", "-h$backupTargetFull\`$disk.hsh") + $dsAdditionalArgs\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
                        Write-Host $dsPath ($dsArgs -join " ")\r
                        \r
+                       $dsCommand = "$dsPath $dsArgs"\r
+                       \r
                        & $dsPath $dsArgs\r
                        \r
                        if($LastExitCode -ne 0) {\r
                        & $dsPath $dsArgs\r
                        \r
                        if($LastExitCode -ne 0) {\r
@@ -200,66 +262,10 @@ if((Test-Path $backupTarget) -and (Test-Path $backupTargetFull) -and (Test-Path
                        else {\r
                                $success = $True\r
                        }\r
                        else {\r
                                $success = $True\r
                        }\r
-               }\r
-       }\r
-       else {\r
-               Write-Host "Directory $backupTargetDiff already exists!"\r
-               $errorMessages += "Directory $backupTargetDiff already exists!"\r
-       }\r
-}\r
-else {\r
-       Write-Host "Full backup"\r
-\r
-       if(!(Test-Path $backupTarget)) {\r
-               Write-Host "Creating directory $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
-               \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
-               }\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
+                       if($rotateBeforeBackup -eq $False -and $success -eq $True) {\r
+                               Rotate-Backup\r
+                       }\r
                }\r
        }\r
 }\r
                }\r
        }\r
 }\r
@@ -279,8 +285,9 @@ if($smbConnected) {
 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
 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 += "Differential backup: $isDiff`n"\r
-       $emailBody += "Backup successfull:  $success`n`n"\r
+       $emailBody += "Differential backup:    $isDiff`n"\r
+       $emailBody += "Backup successful:      $success`n"\r
+       $emailBody += "Drive Snapshot command: $dsCommand`n`n"\r
        $emailBody += ($errorMessages -join "`n")\r
 \r
        Send-Email ($emailBody)\r
        $emailBody += ($errorMessages -join "`n")\r
 \r
        Send-Email ($emailBody)\r

patrick-canterino.de