-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDattoBackupValidator.ps1
475 lines (404 loc) · 14.1 KB
/
DattoBackupValidator.ps1
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#Requires -Version 5.1
<#
.SYNOPSIS
Enterprise-grade Datto BCDR validation and automated testing toolkit.
.DESCRIPTION
Advanced Datto backup validation and testing solution providing:
- Comprehensive backup status verification
- Automated test virtualizations
- Backup integrity validation
- Multi-channel alerting (Email, Slack, Teams)
- Detailed HTML reporting with metrics
- Real-time monitoring and notifications
- Backup chain verification
- Screenshot verification integration
- Recovery point validation
- Network isolation testing
- Performance metrics collection
- Compliance reporting
- Historical trend analysis
- Error pattern detection
- Automated remediation options
- SLA compliance checking
- Capacity monitoring
- Agent health verification
- Custom threshold management
- Backup retention validation
.NOTES
Author: 13city
Compatible with: Windows Server 2012 R2, 2016, 2019, 2022
Requirements:
- PowerShell 5.1 or higher
- Datto API credentials
- Network connectivity to Datto API
- SMTP server access (for email notifications)
- Write access to log directory
- .NET Framework 4.7.2 or higher
- TLS 1.2 support
- Sufficient memory for virtualization tests
- Network bandwidth for API operations
- Access to Datto Partner Portal
- PowerShell WebRequest module
.PARAMETER DattoApiKey
Datto API authentication key
Required: Yes
Format: Base64 string
Obtain: Datto Partner Portal
Permissions: Full API access
Security: Store securely
.PARAMETER DattoSecretKey
API secret for request signing
Required: Yes
Format: Base64 string
Obtain: Datto Partner Portal
Security: Never expose
Rotation: 90 days recommended
.PARAMETER NotificationMethod
Alert delivery mechanism
Required: Yes
Options: "Email", "Slack"
Default: None
Multiple: Not supported
.PARAMETER EmailTo
Notification recipient(s)
Required: If Email method
Format: RFC 5322 email
Multiple: Comma-separated
Example: "admin@company.com"
.PARAMETER EmailFrom
Notification sender address
Required: If Email method
Format: RFC 5322 email
Must be: Authorized sender
Example: "datto@company.com"
.PARAMETER SmtpServer
Email relay server
Required: If Email method
Format: FQDN or IP
Ports: 25, 465, 587
Auth: If required
.PARAMETER SlackWebhook
Slack integration URL
Required: If Slack method
Format: HTTPS URL
Obtain: Slack workspace
Security: Keep private
.PARAMETER TestVirtualizationEnabled
Controls VM testing
Default: True
Type: Boolean
Impact: Performance
Resources: High
.PARAMETER ValidationDepth
Test thoroughness level
Optional: Yes
Values: Basic, Standard, Deep
Default: Standard
Impact: Runtime
.PARAMETER ReportFormat
Output report type
Optional: Yes
Values: HTML, PDF, JSON
Default: HTML
Multiple: Allowed
.PARAMETER RetentionDays
Report/log retention
Optional: Yes
Range: 1-365
Default: 30
Unit: Days
.EXAMPLE
.\DattoBackupValidator.ps1 -DattoApiKey "your-api-key" -DattoSecretKey "your-secret-key" -NotificationMethod Email -EmailTo "admin@company.com" -EmailFrom "datto@company.com" -SmtpServer "smtp.company.com"
Standard validation run:
- Email notifications
- Default testing depth
- HTML reporting
- Standard retention
- All agents checked
.EXAMPLE
.\DattoBackupValidator.ps1 -DattoApiKey "your-api-key" -DattoSecretKey "your-secret-key" -NotificationMethod Slack -SlackWebhook "https://hooks.slack.com/services/xxx" -ValidationDepth Deep
Comprehensive validation:
- Slack notifications
- Deep testing
- Extended verification
- Performance metrics
- Detailed reporting
.EXAMPLE
.\DattoBackupValidator.ps1 -DattoApiKey "your-api-key" -DattoSecretKey "your-secret-key" -NotificationMethod Email -EmailTo "admin@company.com" -TestVirtualizationEnabled $false -ReportFormat "JSON,HTML"
Quick validation run:
- Skip virtualization
- Multiple report formats
- Basic verification
- Email notifications
- Faster completion
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$DattoApiKey,
[Parameter(Mandatory = $true)]
[string]$DattoSecretKey,
[Parameter(Mandatory = $true)]
[ValidateSet("Email", "Slack")]
[string]$NotificationMethod,
[Parameter(Mandatory = $false)]
[string]$EmailTo,
[Parameter(Mandatory = $false)]
[string]$EmailFrom,
[Parameter(Mandatory = $false)]
[string]$SmtpServer,
[Parameter(Mandatory = $false)]
[string]$SlackWebhook,
[Parameter(Mandatory = $false)]
[bool]$TestVirtualizationEnabled = $true
)
# Initialize logging
$LogPath = "C:\Logs\DattoValidation"
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
$LogFile = Join-Path $LogPath "DattoValidation_$timestamp.log"
if (-not (Test-Path $LogPath)) {
New-Item -ItemType Directory -Path $LogPath -Force | Out-Null
}
function Write-Log {
param([string]$Message, [string]$Level = "INFO")
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logEntry = "[$timestamp] [$Level] $Message"
Add-Content -Path $LogFile -Value $logEntry
switch ($Level) {
"ERROR" { Write-Host $logEntry -ForegroundColor Red }
"WARNING" { Write-Host $logEntry -ForegroundColor Yellow }
default { Write-Host $logEntry }
}
}
function Send-Notification {
param(
[string]$Subject,
[string]$Message,
[string]$Priority = "Normal"
)
try {
switch ($NotificationMethod) {
"Email" {
Send-MailMessage -To $EmailTo -From $EmailFrom `
-Subject "Datto Backup Alert: $Subject" `
-Body $Message -SmtpServer $SmtpServer `
-Priority $Priority
}
"Slack" {
$payload = @{
text = "*Datto Backup Alert*`n*$Subject*`n$Message"
}
Invoke-RestMethod -Uri $SlackWebhook -Method Post -Body ($payload | ConvertTo-Json)
}
}
Write-Log "Notification sent: $Subject"
}
catch {
Write-Log "Failed to send notification: $_" -Level "ERROR"
}
}
function Get-DattoAuthHeader {
$timestamp = [Math]::Floor([decimal](Get-Date(Get-Date).ToUniversalTime()-uformat "%s"))
$message = "$DattoApiKey:$timestamp"
$hmacsha256 = New-Object System.Security.Cryptography.HMACSHA256
$hmacsha256.Key = [Text.Encoding]::ASCII.GetBytes($DattoSecretKey)
$signature = [Convert]::ToBase64String($hmacsha256.ComputeHash([Text.Encoding]::ASCII.GetBytes($message)))
return @{
'X-API-KEY' = $DattoApiKey
'X-API-TIMESTAMP' = $timestamp
'X-API-SIGNATURE' = $signature
'Content-Type' = 'application/json'
}
}
function Get-DattoDevices {
try {
$headers = Get-DattoAuthHeader
$response = Invoke-RestMethod -Uri "https://api.datto.com/v1/bcdr/devices" `
-Headers $headers -Method Get
return $response.devices
}
catch {
Write-Log "Failed to get Datto devices: $_" -Level "ERROR"
return $null
}
}
function Get-BackupStatus {
param([string]$DeviceId, [string]$AgentId)
try {
$headers = Get-DattoAuthHeader
$response = Invoke-RestMethod -Uri "https://api.datto.com/v1/bcdr/devices/$DeviceId/agents/$AgentId/backups" `
-Headers $headers -Method Get
return $response.backups | Select-Object -First 1
}
catch {
Write-Log "Failed to get backup status for agent $AgentId : $_" -Level "ERROR"
return $null
}
}
function Start-TestVirtualization {
param(
[string]$DeviceId,
[string]$AgentId,
[string]$SnapshotId
)
try {
$headers = Get-DattoAuthHeader
# Start virtualization
$body = @{
snapshotId = $SnapshotId
networkType = "isolated"
} | ConvertTo-Json
$response = Invoke-RestMethod -Uri "https://api.datto.com/v1/bcdr/devices/$DeviceId/agents/$AgentId/virtualizations" `
-Headers $headers -Method Post -Body $body
$virtualizationId = $response.virtualizationId
Write-Log "Started test virtualization for agent $AgentId (ID: $virtualizationId)"
# Wait for virtualization to be ready
$ready = $false
$attempts = 0
$maxAttempts = 20
while (-not $ready -and $attempts -lt $maxAttempts) {
Start-Sleep -Seconds 30
$status = Invoke-RestMethod -Uri "https://api.datto.com/v1/bcdr/devices/$DeviceId/agents/$AgentId/virtualizations/$virtualizationId" `
-Headers $headers -Method Get
if ($status.state -eq "running") {
$ready = $true
Write-Log "Test virtualization is running successfully"
}
elseif ($status.state -eq "failed") {
throw "Virtualization failed: $($status.errorMessage)"
}
$attempts++
}
if (-not $ready) {
throw "Virtualization did not become ready within timeout period"
}
# Cleanup
Write-Log "Cleaning up test virtualization"
Invoke-RestMethod -Uri "https://api.datto.com/v1/bcdr/devices/$DeviceId/agents/$AgentId/virtualizations/$virtualizationId" `
-Headers $headers -Method Delete
return $true
}
catch {
Write-Log "Test virtualization failed: $_" -Level "ERROR"
return $false
}
}
function Export-ValidationReport {
param([array]$Results)
try {
$reportPath = Join-Path $LogPath "DattoValidation_$timestamp.html"
$html = @"
<!DOCTYPE html>
<html>
<head>
<title>Datto Backup Validation Report</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
table { border-collapse: collapse; width: 100%; margin-top: 20px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #4CAF50; color: white; }
tr:nth-child(even) { background-color: #f2f2f2; }
.success { color: green; }
.warning { color: orange; }
.error { color: red; }
</style>
</head>
<body>
<h1>Datto Backup Validation Report</h1>
<p>Generated: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")</p>
<table>
<tr>
<th>Device Name</th>
<th>Agent Name</th>
<th>Last Backup</th>
<th>Status</th>
<th>Virtualization Test</th>
</tr>
"@
foreach ($result in $Results) {
$statusClass = switch ($result.Status) {
"success" { "success" }
"warning" { "warning" }
default { "error" }
}
$html += @"
<tr>
<td>$($result.DeviceName)</td>
<td>$($result.AgentName)</td>
<td>$($result.LastBackup)</td>
<td class="$statusClass">$($result.Status)</td>
<td>$($result.VirtualizationTest)</td>
</tr>
"@
}
$html += @"
</table>
</body>
</html>
"@
$html | Out-File -FilePath $reportPath -Encoding UTF8
Write-Log "Validation report exported to: $reportPath"
return $reportPath
}
catch {
Write-Log "Failed to export validation report: $_" -Level "ERROR"
return $null
}
}
# Main execution block
try {
Write-Log "=== Starting Datto Backup Validation ==="
# Validate parameters
if ($NotificationMethod -eq "Email" -and (-not $EmailTo -or -not $EmailFrom -or -not $SmtpServer)) {
throw "Email parameters are required when using Email notification method"
}
if ($NotificationMethod -eq "Slack" -and -not $SlackWebhook) {
throw "Slack webhook URL is required when using Slack notification method"
}
# Get all devices
$devices = Get-DattoDevices
if (-not $devices) {
throw "No Datto devices found"
}
$results = @()
foreach ($device in $devices) {
Write-Log "Processing device: $($device.name)"
foreach ($agent in $device.agents) {
Write-Log "Checking agent: $($agent.name)"
# Get latest backup status
$backup = Get-BackupStatus -DeviceId $device.id -AgentId $agent.id
$result = [PSCustomObject]@{
DeviceName = $device.name
AgentName = $agent.name
LastBackup = $backup.timestamp
Status = $backup.status
VirtualizationTest = "Not Performed"
}
# Check backup age
if ($backup.timestamp -lt (Get-Date).AddHours(-24)) {
Write-Log "Outdated backup detected for $($agent.name)" -Level "WARNING"
Send-Notification -Subject "Outdated Backup" `
-Message "Backup for $($agent.name) is outdated. Last backup: $($backup.timestamp)" `
-Priority "High"
}
# Perform test virtualization if enabled
if ($TestVirtualizationEnabled -and $backup.status -eq "success") {
$testResult = Start-TestVirtualization -DeviceId $device.id -AgentId $agent.id -SnapshotId $backup.snapshotId
$result.VirtualizationTest = if ($testResult) { "Success" } else { "Failed" }
}
$results += $result
}
}
# Generate and export report
$reportPath = Export-ValidationReport -Results $results
if ($reportPath) {
Send-Notification -Subject "Validation Report Available" `
-Message "Datto backup validation completed. Report available at: $reportPath"
}
Write-Log "=== Datto Backup Validation Completed ==="
}
catch {
Write-Log "Script execution failed: $_" -Level "ERROR"
Send-Notification -Subject "Script Execution Failed" -Message $_.Exception.Message -Priority "High"
exit 1
}