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),
18 [Int32]$keepMonths = 2
19 # Path to Drive Snapshot
20 [String]$dsPath = "C:\Users\Patrick\Desktop\DSMonRot\snapshot.exe"
21 # Path to Drive Snapshot log file (specify only the file name if you set
22 # $dsLogFileToBackup to $True)
23 #[String]$dsLogFile = "C:\Users\Patrick\Desktop\DSMonRot\snapshot.log"
24 [String]$dsLogFile = "snapshot.log"
25 # Set to $True if you want to put the log file of Drive Snapshot into the same
26 # directory as the backup
27 [Boolean]$dsLogFileToBackup = $True
28 # Disks to backup, see http://www.drivesnapshot.de/en/commandline.htm
29 [String]$disksToBackup = "HD1:1"
30 # Path to DSMonRot log file
31 [String]$logFile = "C:\Users\Patrick\Desktop\DSMonRot\dsmonrot.log"
33 # Map network share to this drive letter, comment out if you don't want to use it
34 [String]$smbDrive = "Z"
35 # Path to network share
36 [String]$smbPath = "\\192.168.0.3\ds"
37 # User and password for connecting to network share, comment out if you don't want to use it
38 # (for example if you want to pass current Windows credentials)
39 [String]$smbUser = "patrick"
40 [String]$smbPassword = ""
42 # Send an email if an error occured
43 [Boolean]$emailOnError = $True
44 # From address of email notification
45 [String]$emailFromAddress = "alarm@test.local"
46 # To address of email notification
47 [String]$emailToAddress = "patrick@test.local"
48 # Subject of email notification
49 [String]$emailSubject = "DSMonRot"
51 [String]$emailMailserver = "localhost"
53 [Int32]$emailPort = 25
55 [Boolean]$emailSSL = $False
57 [Boolean]$emailAuth = $False
59 [String]$emailUser = ""
61 [String]$emailPassword = ""
65 $dsAdditionalArgs = @("--UseVSS")
67 # Allow SMTP with SSL and SMTP Auth
68 # see: http://petermorrissey.blogspot.de/2013/01/sending-smtp-emails-with-powershell.html
69 function Send-Email([String]$body) {
70 Write-Host "Sending email: $emailToAddress, $body"
73 $smtp = New-Object System.Net.Mail.SmtpClient($emailMailServer, $emailPort);
75 $smtp.EnableSSL = $emailSSL
78 $smtp.Credentials = New-Object System.Net.NetworkCredential($emailUser, $emailPassword);
81 $smtp.Send($emailFromAddress, $emailToAddress, $emailSubject, $body);
84 Write-Host "Could not send email: $_.Exception.Message"
90 $smbConnected = $False
95 Write-Host "Connecting network drive"
97 if($smbUser -and $smbPassword) {
98 Write-Host "With credentials"
100 $secSmbPassword = $smbPassword | ConvertTo-SecureString -asPlainText -Force
101 $smbCredential = New-Object System.Management.Automation.PSCredential($smbUser, $secSmbPassword)
103 New-PSDrive -Name $smbDrive -PSProvider "FileSystem" -Root $smbPath -Credential $smbCredential -Persist -ErrorAction Stop
106 Write-Host "Without credentials"
108 New-PSDrive -Name $smbDrive -PSProvider "FileSystem" -Root $smbPath -Persist -ErrorAction Stop
111 $smbConnected = $True
114 Write-Host "Could not connect to network drive: $_.Exception.Message"
119 if(!(Test-Path $backupDir)) {
120 Write-Host "Directory $backupDir does not exist!"
124 $currMonth = Get-Date -format "yyyy-MM"
125 $currDay = Get-Date -format "yyyy-MM-dd"
127 Write-Host $currMonth
129 $backupTarget = $backupDir + "\" + $currMonth
130 $backupTargetFull = $backupTarget + "\" + "Full"
132 $backupTargetDiff = $backupTarget + "\" + "Diff-" + $currDay
134 Write-Host $backupTarget
138 if((Test-Path $backupTarget) -and (Test-Path $backupTargetFull) -and (Test-Path "$backupTargetFull\*.hsh")) {
139 Write-Host "Differential backup"
143 if(!(Test-Path $backupTargetDiff)) {
144 Write-Host "Creating directory $backupTargetDiff"
147 New-Item -ItemType directory -Path $backupTargetDiff -ErrorAction Stop
152 Write-Host "Could not create directory $backupTargetDiff`: $_.Exception.Message"
156 $dsLogPath = if($dsLogFileToBackup) { "$backupTargetDiff\$dsLogFile" } else { $dsLogFile }
158 $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetDiff\`$disk.sna", "-h$backupTargetFull\`$disk.hsh") + $dsAdditionalArgs
159 Write-Host $dsPath ($dsArgs -join " ")
163 if($LastExitCode -ne 0) {
164 Write-Host "Error code: $LastExitCode"
168 Write-Host "Directory $backupTargetDiff already exists!"
170 $errorMessages += "Directory $backupTargetDiff already exists!"
174 Write-Host "Full backup"
176 if(!(Test-Path $backupTarget)) {
177 Write-Host "Creating directory $backupTarget"
178 New-Item -ItemType directory -Path $backupTarget
181 if(!(Test-Path $backupTargetFull)) {
182 Write-Host "Creating directory $backupTargetFull"
183 New-Item -ItemType directory -Path $backupTargetFull
186 $dsLogPath = if($dsLogFileToBackup) { "$backupTargetFull\$dsLogFile" } else { $dsLogFile }
188 $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetFull\`$disk.sna") + $dsAdditionalArgs
189 Write-Host $dsPath ($dsArgs -join " ")
193 if($LastExitCode -ne 0) {
194 Write-Host "Error code: $LastExitCode"
200 if($isDiff -eq $False -and $success -eq $True -and $keepMonths -ge 0) {
201 Write-Host "Rotating"
203 $keepMonthsCount = $keepMonths
205 Get-ChildItem $backupDir -Directory | Where-Object {($_.Name -ne $currMonth) -and ($_.Name -match "^\d{4,}-\d{2}$")} | Sort-Object -Descending |
207 Write-Host $_ "=>" $_.FullName
209 if($keepMonthsCount -ge 0) {
213 Write-Host $keepMonthsCount
215 if($keepMonthsCount -eq -1) {
216 Write-Host "Deleting $_"
217 Remove-Item -Recurse -Force $_.FullName
223 Write-Host "Disconnecting network drive"
224 Remove-PSDrive $smbDrive
227 if($emailOnError -and $errorMessages.Count -gt 0) {
228 Send-Email ("Error:`n"+($errorMessages -join "`n"))