]> git.p6c8.net - dsmonrot.git/blob - dsmonrot.ps1
353e0dbaae7cd1321f12961481a4d3bda0d0253d
[dsmonrot.git] / dsmonrot.ps1
1 # DSMonRot
2 # Script for rotating Drive Snapshot backups monthly
3 #
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
8 #
9 # Drive Snapshot is copyright by Tom Ehlert
10 # http://www.drivesnapshot.de/
11
12 # Config
13
14 # Path to backup directory
15 [String]$backupDir = "Z:\"
16 # Keep backup for this amount of months (excluding the current month),
17 # -1 for indefinite
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"
32
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 = ""
41
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"
50 # Mail server
51 [String]$emailMailserver = "localhost"
52 # SMTP Port
53 [Int32]$emailPort = 25
54 # Use SSL?
55 [Boolean]$emailSSL = $False
56 # Use SMTP Auth?
57 [Boolean]$emailAuth = $False
58 # SMTP Auth User
59 [String]$emailUser = ""
60 # SMTP Auth Password
61 [String]$emailPassword = ""
62
63 # End of config
64
65 $dsAdditionalArgs = @("--UseVSS")
66
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"
71
72 try {
73 $smtp = New-Object System.Net.Mail.SmtpClient($emailMailServer, $emailPort);
74
75 $smtp.EnableSSL = $emailSSL
76
77 if($emailAuth) {
78 $smtp.Credentials = New-Object System.Net.NetworkCredential($emailUser, $emailPassword);
79 }
80
81 $smtp.Send($emailFromAddress, $emailToAddress, $emailSubject, $body);
82 }
83 catch {
84 Write-Host "Could not send email: $_.Exception.Message"
85 }
86 }
87
88 $errorMessages = @()
89
90 $smbConnected = $False
91 $success = $False
92
93 if($smbDrive) {
94 Try {
95 Write-Host "Connecting network drive"
96
97 if($smbUser -and $smbPassword) {
98 Write-Host "With credentials"
99
100 $secSmbPassword = $smbPassword | ConvertTo-SecureString -asPlainText -Force
101 $smbCredential = New-Object System.Management.Automation.PSCredential($smbUser, $secSmbPassword)
102
103 New-PSDrive -Name $smbDrive -PSProvider "FileSystem" -Root $smbPath -Credential $smbCredential -Persist -ErrorAction Stop
104 }
105 else {
106 Write-Host "Without credentials"
107
108 New-PSDrive -Name $smbDrive -PSProvider "FileSystem" -Root $smbPath -Persist -ErrorAction Stop
109 }
110
111 $smbConnected = $True
112 }
113 Catch {
114 Write-Host "Could not connect to network drive: $_.Exception.Message"
115 exit
116 }
117 }
118
119 if(!(Test-Path $backupDir)) {
120 Write-Host "Directory $backupDir does not exist!"
121 exit
122 }
123
124 $currMonth = Get-Date -format "yyyy-MM"
125 $currDay = Get-Date -format "yyyy-MM-dd"
126
127 Write-Host $currMonth
128
129 $backupTarget = $backupDir + "\" + $currMonth
130 $backupTargetFull = $backupTarget + "\" + "Full"
131
132 $backupTargetDiff = $backupTarget + "\" + "Diff-" + $currDay
133
134 Write-Host $backupTarget
135
136 $isDiff = $False
137
138 if((Test-Path $backupTarget) -and (Test-Path $backupTargetFull) -and (Test-Path "$backupTargetFull\*.hsh")) {
139 Write-Host "Differential backup"
140
141 $isDiff = $True
142
143 if(!(Test-Path $backupTargetDiff)) {
144 Write-Host "Creating directory $backupTargetDiff"
145
146 try {
147 New-Item -ItemType directory -Path $backupTargetDiff -ErrorAction Stop
148
149 $success = $True
150 }
151 catch {
152 Write-Host "Could not create directory $backupTargetDiff`: $_.Exception.Message"
153 exit
154 }
155
156 $dsLogPath = if($dsLogFileToBackup) { "$backupTargetDiff\$dsLogFile" } else { $dsLogFile }
157
158 $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetDiff\`$disk.sna", "-h$backupTargetFull\`$disk.hsh") + $dsAdditionalArgs
159 Write-Host $dsPath ($dsArgs -join " ")
160
161 & $dsPath $dsArgs
162
163 if($LastExitCode -ne 0) {
164 Write-Host "Error code: $LastExitCode"
165 }
166 }
167 else {
168 Write-Host "Directory $backupTargetDiff already exists!"
169
170 $errorMessages += "Directory $backupTargetDiff already exists!"
171 }
172 }
173 else {
174 Write-Host "Full backup"
175
176 if(!(Test-Path $backupTarget)) {
177 Write-Host "Creating directory $backupTarget"
178 New-Item -ItemType directory -Path $backupTarget
179 }
180
181 if(!(Test-Path $backupTargetFull)) {
182 Write-Host "Creating directory $backupTargetFull"
183 New-Item -ItemType directory -Path $backupTargetFull
184 }
185
186 $dsLogPath = if($dsLogFileToBackup) { "$backupTargetFull\$dsLogFile" } else { $dsLogFile }
187
188 $dsArgs = @($disksToBackup, "--logfile:$dsLogPath", "$backupTargetFull\`$disk.sna") + $dsAdditionalArgs
189 Write-Host $dsPath ($dsArgs -join " ")
190
191 & $dsPath $dsArgs
192
193 if($LastExitCode -ne 0) {
194 Write-Host "Error code: $LastExitCode"
195 }
196
197 $success = $False
198 }
199
200 if($isDiff -eq $False -and $success -eq $True -and $keepMonths -ge 0) {
201 Write-Host "Rotating"
202
203 $keepMonthsCount = $keepMonths
204
205 Get-ChildItem $backupDir -Directory | Where-Object {($_.Name -ne $currMonth) -and ($_.Name -match "^\d{4,}-\d{2}$")} | Sort-Object -Descending |
206 Foreach-Object {
207 Write-Host $_ "=>" $_.FullName
208
209 if($keepMonthsCount -ge 0) {
210 $keepMonthsCount--
211 }
212
213 Write-Host $keepMonthsCount
214
215 if($keepMonthsCount -eq -1) {
216 Write-Host "Deleting $_"
217 Remove-Item -Recurse -Force $_.FullName
218 }
219 }
220 }
221
222 if($smbConnected) {
223 Write-Host "Disconnecting network drive"
224 Remove-PSDrive $smbDrive
225 }
226
227 if($emailOnError -and $errorMessages.Count -gt 0) {
228 Send-Email ("Error:`n"+($errorMessages -join "`n"))
229 }

patrick-canterino.de