-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVeeamFreeAutoBackup.ps1
More file actions
357 lines (345 loc) · 15.8 KB
/
VeeamFreeAutoBackup.ps1
File metadata and controls
357 lines (345 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<#
# Simple ESXi backup automation script
# for Veeam Backup Free Edition
#
# Author: inDev.be
# Version: 1.0
# Licence: GPL
#
# improvement idea:
# - handle hosts/vm types & adapt commands
# - support -BackupRepository for Start-VBRZip cmdlet
# - support -RunAsync for Start-VBRZip cmdlet
#
#####################################>
### Environement and configuration ###
## Functions
function Send-Mail ($subject, $body, $isHtml = $TRUE)
{
if($mailConfigValidated) {
try
{
# Creating message
$message = New-Object System.Net.Mail.MailMessage( $config.Mail.From, $config.Mail.To )
foreach($cc in $config.Mail.Cc)
{
$message.cc.add($cc)
}
$message.IsBodyHtml = $isHtml
$message.Subject = $subject
$message.Body = $body
# SMTP connection & send
$SMTPClient = New-Object System.Net.Mail.SmtpClient( $config.Mail.SmtpHost , $config.Mail.SmtpPort )
$SMTPClient.EnableSsl = $config.Mail.EnableSsl
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $config.Mail.SmtpUser , $config.Mail.SmtpPass );
$SMTPClient.Send( $message )
}
catch
{
$errorMessage = $_.Exception.Message
$failedItem = $_.Exception.ItemName
$message = "Email notification Failed! We failed to send a notification email.`r`n`r`n$failedItem.`r`n The error message was $errorMessage"
$title = "Email error"
Log -Title $title -Message $message -Level "warning"
}
}
}
function Log ($title, $message, $level = "error", $sendMail = $FALSE)
{
try {
switch ($level)
{
"success" { Write-Output $message }
"info" { Write-Output $message }
"warning" { Write-Warning $message }
default
{
$level = "error"
Write-Error $message
}
}
$logPath = [IO.Path]::Combine($currentDirectory, "logs.txt")
"[$level] $title" | Add-Content -Path $logPath
$message | Add-Content -Path $logPath
if($sendmail -and $config.Mail.EnableNotifications)
{
if($config.Mail.isHtml)
{
$message = $message.Replace("`r`n","<br>")
}
switch ($level)
{
"info"
{
$subject = if($mailSuccessSubjectTemplate) { $mailSuccessSubjectTemplate -f $title } else { $title }
$body = if($mailSuccessBodyTemplate) { $mailSuccessBodyTemplate -f $message } else { $message }
Send-Mail -Subject $subject -Body $body -IsHtml $config.Mail.isHtml
}
"success"
{
$subject = if($mailSuccessSubjectTemplate) { $mailSuccessSubjectTemplate -f $title } else { $title }
$body = if($mailSuccessBodyTemplate) { $mailSuccessBodyTemplate -f $message } else { $message }
Send-Mail -Subject $subject -Body $body -IsHtml $config.Mail.isHtml
}
default
{
$subject = if($mailFailureSubjectTemplate) { $mailFailureSubjectTemplate -f $title } else { $title }
$body = if($mailFailureBodyTemplate) { $mailFailureBodyTemplate -f $message } else { $message }
Send-Mail -Subject $subject -Body $body -IsHtml $config.Mail.isHtml
}
}
}
}
catch
{
$errorMessage = $_.Exception.Message
$failedItem = $_.Exception.ItemName
$message = "Error.`r`n`r`n$failedItem.`r`n The error message was $errorMessage"
Write-Error $message
}
}
## Configuration ##
$currentDirectory = [IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)
$configFile = [IO.Path]::Combine($currentDirectory, 'VeeamFreeAutoBackupConfig.json')
$config = $NULL
# Parsing JSON config
try
{
$config = Get-Content "$configFile" -Raw -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue | ConvertFrom-Json -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue
}
catch
{
$errorMessage = $_.Exception.Message
$failedItem = $_.Exception.ItemName
Write-Error "Config File Read Failed! We failed to read file.`r`n`r`n $failedItem.`r`nThe error message was $errorMessage"
Break
}
## Mails ##
$mailConfigValidated = $TRUE
# Const
$mailTemplatePath = [IO.Path]::Combine($currentDirectory, "templates\")
$mailTemplatePrefix = "Mail_"
$mailTemplateSubjectPrefix = "Subject_"
$mailTemplateBodyPrefix = "Body_"
$mailTemplateSubjectSufix = ".txt"
$mailTemplateBodySufix = if ($config.Mail.IsHtml) {".html"} Else {".txt"}
# Templates
$mailSuccessSubjectFile = [IO.Path]::Combine($mailTemplatePath, $mailTemplatePrefix + $mailTemplateSubjectPrefix + $config.Mail.Templates.Success + $mailTemplateSubjectSufix)
$mailSuccessBodyFile = [IO.Path]::Combine($mailTemplatePath, $mailTemplatePrefix + $mailTemplateBodyPrefix + $config.Mail.Templates.Success + $mailTemplateBodySufix)
$mailFailureSubjectFile = [IO.Path]::Combine($mailTemplatePath, $mailTemplatePrefix + $mailTemplateSubjectPrefix + $config.Mail.Templates.Failure + $mailTemplateSubjectSufix)
$mailFailureBodyFile = [IO.Path]::Combine($mailTemplatePath, $mailTemplatePrefix + $mailTemplateBodyPrefix + $config.Mail.Templates.Failure + $mailTemplateBodySufix)
try
{
if([IO.File]::Exists($mailSuccessSubjectFile)) { $mailSuccessSubjectTemplate = Get-Content $mailSuccessSubjectFile }
else {
$mailConfigValidated = $FALSE
$title = "Email template missing"
$message = "mailSuccessSubjectFile file is missing at $mailSuccessSubjectFile"
Log -Title $title -Message $message -Level "warning"
}
if([IO.File]::Exists($mailSuccessBodyFile)) { $mailSuccessBodyTemplate = Get-Content $mailSuccessBodyFile }
else {
$mailConfigValidated = $FALSE
$title = "Email template missing"
$message = "mailSuccessBodyFile file is missing at $mailSuccessBodyFile"
Log -Title $title -Message $message -Level "warning"
}
if([IO.File]::Exists($mailFailureSubjectFile)) { $mailFailureSubjectTemplate = Get-Content $mailFailureSubjectFile }
else {
$mailConfigValidated = $FALSE
$title = "Email template missing"
$message = "mailFailureSubjectFile file is missing at $mailFailureSubjectFile"
Log -Title $title -Message $message -Level "warning"
}
if([IO.File]::Exists($mailFailureBodyFile)) { $mailFailureBodyTemplate = Get-Content $mailFailureBodyFile }
else {
$mailConfigValidated = $FALSE
$title = "Email template missing"
$message = "mailFailureBodyFile file is missing at $mailFailureBodyFile"
Log -Title $title -Message $message -Level "warning"
}
}
catch
{
$mailConfigValidated = $FALSE # ?the variable doesn't keep the value outside the catch scope?
$errorMessage = $_.Exception.Message
$failedItem = $_.Exception.ItemName
$title = "Email templates error"
$message = "An error happened while reading mail notifications templates! Mail notifications disabled.`r`n`r`n$failedItem.`r`n The error message was`r`n $errorMessage"
Log -Title $title -Message $message -Level "error"
}
$now = [System.DateTime]::Now
$title = "VeeamFreeAutoBackup session started"
$startMsg = "[=== Beginning new VeeamFreeAutoBackup session at $now ===]"
Log -Title $title -Message $startMsg -Level "info"
### Veeam connection ###
# Add the Veeam Backup and Replication Snap-in
Add-PSSnapin VeeamPSSnapin
# Disconnect from Veeam Backup and Replication server in case it is already connected
Disconnect-VBRServer
# Connect to the Veeam Backup and Replication server (localhost)
Connect-VBRServer
# Loop through configured hosts
foreach($targetHost in $config.Hosts)
{
$srvName = $targetHost.Server
try
{
# Get the host server
$vbrServer = Get-VBRServer -Name $srvName
if(!$vbrServer)
{
$title = "Host not found"
$message = "No server named $srvName can be found, skipping this host."
Log -Title $title -Message $message -Level "warning" -SendMail $TRUE
continue
}
if($vbrServer.IsUnavailable)
{
$title = "Host unavailable"
$message = "The server named $srvName is unavailable, skipping this host."
Log -Title $title -Message $message -Level "warning" -SendMail $TRUE
continue
}
# Loop through configured host's backup sets
foreach($backupSet in $targetHost.BackupSets)
{
try
{
# Define variables to use (backup defined or global)
$backupDestination = if ($backupSet.Destination) {$backupSet.Destination} Else {$config.Default.Destination}
$backupCompressionLevel = if ($backupSet.CompressionLevel) {$backupSet.CompressionLevel} Else {$config.Default.CompressionLevel}
$backupEnableQuiescence = if ($backupSet.EnableQuiescence) {$backupSet.EnableQuiescence} Else {$config.Default.EnableQuiescence}
$backupEnableEncryption = if ($backupSet.EnableEncryption) {$backupSet.EnableEncryption} Else {$config.Default.EnableEncryption}
$backupEncryptionKey = if ($backupSet.EncryptionKey) {$backupSet.EncryptionKey} Else {$config.Default.EncryptionKey}
$backupRetention = if ($backupSet.Retention) {$backupSet.Retention} Else {$config.Default.Retention}
$backupNetCredential = if ($backupSet.NetCredential) {$backupSet.NetCredential} Else {$config.Default.NetCredential}
# Continue with backup set specific infos
$backupAlias = $backupSet.Alias
$backupVms = $backupSet.Vms
# Destination path definition (& creation if doesn't exit)
$backupDestinationPath = [IO.Path]::Combine($backupDestination, $backupAlias)
if(![IO.Directory]::Exists($backupDestinationPath))
{
New-Item -ItemType Directory -Force -Path $backupDestinationPath
}
# Get network credentials if needed
$vbrCredentials = $NULL
if($backupNetCredential)
{
$vbrCredentials = Get-VBRCredentials -Name $backupNetCredential
}
# Get encryption key
$vbrEncryptionKey = $NULL
if($backupEnableEncryption)
{
$vbrEncryptionKey = Get-VBREncryptionKey -Description $backupAlias
if(!$vbrEncryptionKey)
{
$vbrEncryptionKey = Add-VBREncryptionKey -Description $backupAlias -Password (cat $backupEncryptionKey | ConvertTo-SecureString)
}
if(!$vbrEncryptionKey)
{
$title = "Encryption key not found"
$message = "Encryption key $backupEncryptionKey cannot be found nor created for backup set $backupAlias (server $srvName). No encryption will be used for this backup set."
Log -Title $title -Message $message -Level "warning" -SendMail $TRUE
}
}
foreach($backupVm in $backupVms)
{
try
{
# Get the VMs to backup
$vbrVms = Find-VBRViEntity -Server $vbrServer -Name $backupVm
if(!$vbrVms -or $vbrVms.count -eq 0)
{
$title = "VM $backupVm not found"
$message = "The VM $backupVm could not be found on server $srvName (backup set $backupAlias). Skipping VM."
Log -Title $title -Message $message -Level "warning" -SendMail $TRUE
continue
}
# Create VeeamZip session
if($vbrEncryptionKey)
{
if($vbrCredentials)
{
$vbrZipSession = Start-VBRZip -Entity $vbrVms -Folder $backupDestinationPath -Compression $backupCompressionLevel -DisableQuiesce:(!$backupEnableQuiescence) -AutoDelete $backupRetention -EncryptionKey $vbrEncryptionKey -NetworkCredentials $vbrCredentials
}
else {
$vbrZipSession = Start-VBRZip -Entity $vbrVms -Folder $backupDestinationPath -Compression $backupCompressionLevel -DisableQuiesce:(!$backupEnableQuiescence) -AutoDelete $backupRetention -EncryptionKey $vbrEncryptionKey
}
}
else
{
if($vbrCredentials)
{
$vbrZipSession = Start-VBRZip -Entity $vbrVms -Folder $backupDestinationPath -Compression $backupCompressionLevel -DisableQuiesce:(!$backupEnableQuiescence) -AutoDelete $backupRetention -NetworkCredentials $vbrCredentials
}
else
{
$vbrZipSession = Start-VBRZip -Entity $vbrVms -Folder $backupDestinationPath -Compression $backupCompressionLevel -DisableQuiesce:(!$backupEnableQuiescence) -AutoDelete $backupRetention
}
}
if(!$vbrZipSession)
{
$title = "VeeamZIP session is empty"
$message = "VM $backupVm VeeamZIP session result is empty (server $srvName, backup set $backupAlias). Backup probably failed!"
Log -Title $title -Message $message -Level "warning" -SendMail $TRUE
continue
}
# Get tasks status
$vbrZipTaskSessionsLogs = $vbrZipSession.GetTaskSessions().logger.getlog().updatedrecords
$failedVbrZipTaskSessions = $vbrZipTaskSessionsLogs | where { $_.status -eq "EWarning" -or $_.Status -eq "EFailed" }
$message = ""
if ($failedVbrZipTaskSessions -ne $Null)
{
$title = "VM $backupVm backup failed"
$message = "The VM $backupVm backup for set $backupAlias on server $srvName failed`r`n" + ($vbrZipSession | Select-Object @{n="Name";e={($_.name).Substring(0, $_.name.LastIndexOf("("))}} ,@{n="Start Time";e={$_.CreationTime}},@{n="End Time";e={$_.EndTime}},Result,@{n="Details";e={$FailedSessions.Title}})
Log -Title $title -Message $message -Level "error" -SendMail $TRUE
}
else
{
$title = "VM $backupVm backup successful"
$message = "The VM $backupVm backup for set $backupAlias on server $srvName succeeded.`r`n" + ($vbrZipSession | Select-Object @{n="Name";e={($_.name).Substring(0, $_.name.LastIndexOf("("))}} ,@{n="Start Time";e={$_.CreationTime}},@{n="End Time";e={$_.EndTime}},Result,@{n="Details";e={($TaskSessions | sort creationtime -Descending | select -first 1).Title}})
Log -Title $title -Message $message -Level "success" -SendMail $TRUE
}
}
catch
{
$errorMessage = $_.Exception.Message
$failedItem = $_.Exception.ItemName
$title = "$VM backupVm backup failed"
$message = "The VM $backupVm backup for set $backupAlias on server $srvName failed.`r`n`r`n$failedItem`r`nThe error message was`r`n$errorMessage"
Log -Title $title -Message $message -Level "error" -SendMail $TRUE
continue
}
}
}
catch
{
$errorMessage = $_.Exception.Message
$failedItem = $_.Exception.ItemName
$alias = $backupSet.Alias
$title = "Backup set $alias failed"
$message = "Backup set $alias failed.`r`n`r`n$failedItem`r`nThe error message was`r`n$errorMessage"
Log -Title $title -Message $message -Level "error" -SendMail $TRUE
continue
}
}
}
catch
{
$errorMessage = $_.Exception.Message
$failedItem = $_.Exception.ItemName
$title = "Backup of host $srvName"
$message = "Backup of host $srvName failed`r`n`r`n$failedItem`r`nThe error message was`r`n$errorMessage"
Log -Title $title -Message $message -Level "error" -SendMail $TRUE
continue
}
}
# Disconnect from Veeam Backup and Replication server
Disconnect-VBRServer
$now = [System.DateTime]::Now
$title = "VeeamFreeAutoBackup session finished"
$endMsg = "[=== VeeamFreeAutoBackup session finished at $now ===]`r`n`r`n"
Log -Title $title -Message $endMsg -Level "info"