2 # Script for rotating Drive Snapshot backups monthly
4 # Author: Patrick Canterino <patrick@patrick-canterino.de>
5 # WWW: https://www.patrick-canterino.de/
6 # https://github.com/pcanterino/dsmonrot
7 # License: 2-Clause BSD License
9 # Drive Snapshot is copyright by Tom Ehlert
10 # http://www.drivesnapshot.de/
14 # Path to backup directory
15 [String]$backupDir = "Z:\"
16 # Keep backup for this amount of months (excluding the current month), -1 for indefinite
17 [Int32]$keepMonths = 2
18 # Path to Drive Snapshot
19 [String]$dsPath = "C:\Users\Patrick\Desktop\DSMonRot\snapshot.exe"
20 # Path to Drive Snapshot log file
21 [String]$dsLogFile = "C:\Users\Patrick\Desktop\DSMonRot\snapshot.log"
22 # Disks to backup, see http://www.drivesnapshot.de/en/commandline.htm
23 [String]$disksToBackup = "HD1:1"
24 # Path to DSMonRot log file
25 [String]$logFile = "C:\Users\Patrick\Desktop\DSMonRot\dsmonrot.log"
27 # Map network share to this drive letter, comment out if you don't want to use it
28 [String]$smbDrive = "Z"
29 # Path to network share
30 [String]$smbPath = "\\192.168.0.3\ds"
31 # User and password for connecting to network share, comment out if you don't want to use it
32 # (for example if you want to pass current Windows credentials)
33 [String]$smbUser = "patrick"
34 [String]$smbPassword = ""
36 # Send an email if an error occured
37 [Boolean]$emailOnError = $True
38 # From address of email notification
39 [String]$emailFromAddress = "alarm@test.local"
40 # To address of email notification
41 [String]$emailToAddress = "patrick@test.local"
42 # Subject of email notification
43 [String]$emailSubject = "DSMonRot"
45 [String]$emailMailserver = "localhost"
47 [Int32]$emailPort = 25
49 [Boolean]$emailSSL = $False
51 [Boolean]$emailAuth = $False
53 [String]$emailUser = ""
55 [String]$emailPassword = ""
59 $dsAdditionalArgs = @("--logfile:$dsLogFile", "--UseVSS")
61 # Allow SMTP with SSL and SMTP Auth
62 # see: http://petermorrissey.blogspot.de/2013/01/sending-smtp-emails-with-powershell.html
63 function Send-Email([String]$body) {
64 Write-Host "Sending email: $emailToAddress, $body"
67 $smtp = New-Object System.Net.Mail.SmtpClient($emailMailServer, $emailPort);
69 $smtp.EnableSSL = $emailSSL
72 $smtp.Credentials = New-Object System.Net.NetworkCredential($emailUser, $emailPassword);
75 $smtp.Send($emailFromAddress, $emailToAddress, $emailSubject, $body);
78 Write-Host "Could not send email: $_.Exception.Message"
84 $smbConnected = $False
89 Write-Host "Connecting network drive"
91 if($smbUser -and $smbPassword) {
92 Write-Host "With credentials"
94 $secSmbPassword = $smbPassword | ConvertTo-SecureString -asPlainText -Force
95 $smbCredential = New-Object System.Management.Automation.PSCredential($smbUser, $secSmbPassword)
97 New-PSDrive -Name $smbDrive -PSProvider "FileSystem" -Root $smbPath -Credential $smbCredential -Persist -ErrorAction Stop
100 Write-Host "Without credentials"
102 New-PSDrive -Name $smbDrive -PSProvider "FileSystem" -Root $smbPath -Persist -ErrorAction Stop
105 $smbConnected = $True
108 Write-Host "Could not connect to network drive: $_.Exception.Message"
113 if(!(Test-Path $backupDir)) {
114 Write-Host "Directory $backupDir does not exist!"
118 $currMonth = Get-Date -format "yyyy-MM"
119 $currDay = Get-Date -format "yyyy-MM-dd"
121 Write-Host $currMonth
123 $backupTarget = $backupDir + "\" + $currMonth
124 $backupTargetFull = $backupTarget + "\" + "Full"
126 $backupTargetDiff = $backupTarget + "\" + "Diff-" + $currDay
128 Write-Host $backupTarget
132 if((Test-Path $backupTarget) -and (Test-Path $backupTargetFull) -and (Test-Path "$backupTargetFull\*.hsh")) {
133 Write-Host "Differential backup"
137 if(!(Test-Path $backupTargetDiff)) {
138 Write-Host "Creating directory $backupTargetDiff"
141 New-Item -ItemType directory -Path $backupTargetDiff -ErrorAction Stop
146 Write-Host "Could not create directory $backupTargetDiff`: $_.Exception.Message"
150 $dsArgs = @($disksToBackup, "$backupTargetDiff\`$disk.sna", "-h$backupTargetFull\`$disk.hsh") + $dsAdditionalArgs
151 Write-Host $dsPath ($dsArgs -join " ")
155 if($LastExitCode -ne 0) {
156 Write-Host "Error code: $LastExitCode"
160 Write-Host "Directory $backupTargetDiff already exists!"
162 $errorMessages += "Directory $backupTargetDiff already exists!"
166 Write-Host "Full backup"
168 if(!(Test-Path $backupTarget)) {
169 Write-Host "Creating directory $backupTarget"
170 New-Item -ItemType directory -Path $backupTarget
173 if(!(Test-Path $backupTargetFull)) {
174 Write-Host "Creating directory $backupTargetFull"
175 New-Item -ItemType directory -Path $backupTargetFull
178 $dsArgs = @($disksToBackup, "$backupTargetFull\`$disk.sna") + $dsAdditionalArgs
179 Write-Host $dsPath ($dsArgs -join " ")
183 if($LastExitCode -ne 0) {
184 Write-Host "Error code: $LastExitCode"
190 if($isDiff -eq $False -and $success -eq $True -and $keepMonths -ge 0) {
191 Write-Host "Rotating"
193 $keepMonthsCount = $keepMonths
195 Get-ChildItem $backupDir -Directory | Where-Object {($_.Name -ne $currMonth) -and ($_.Name -match "^\d{4,}-\d{2}$")} | Sort-Object -Descending |
197 Write-Host $_ "=>" $_.FullName
199 if($keepMonthsCount -ge 0) {
203 Write-Host $keepMonthsCount
205 if($keepMonthsCount -eq -1) {
206 Write-Host "Deleting $_"
207 Remove-Item -Recurse -Force $_.FullName
213 Write-Host "Disconnecting network drive"
214 Remove-PSDrive $smbDrive
217 if($emailOnError -and $errorMessages.Count -gt 0) {
218 Send-Email ("Error:`n"+($errorMessages -join "`n"))