-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLabBackup.ps1
More file actions
296 lines (233 loc) · 9.88 KB
/
Copy pathLabBackup.ps1
File metadata and controls
296 lines (233 loc) · 9.88 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
param (
[Parameter(Mandatory = $true)]
[String] $DBFilePath,
[Parameter(Mandatory = $false)]
[String[]] $excludeVMList,
[Parameter(Mandatory = $true)]
[ValidateSet("Install", "Backup", "BackupNewOnly")]
[String] $Operation
)
Begin {
$ErrorActionPreference = "Stop"
$global:db = $null
# First check and load required modules
if (!(Get-Module -ListAvailable -Name "Hyper-V")) {
throw "Hyper-V not detected"
exit 1;
}
else {
try {
import-module "Hyper-V"
}
catch {
throw "Unable to load Hyper-V module"
exit 1;
}
}
if (!(Get-Module -ListAvailable -Name xHyper-VBackup.cmdlets)) {
try {
import-module .\xHyper-VBackup.cmdlets.psm1
}
catch {
throw "Unable to load xHyper-VBackup module"
exit 1;
}
}
function DoFullOrDiffBackup {
param (
# Parameter help description
[Parameter(Mandatory = $true)]
[Object[]]
$vms
)
# For each VM determine if Full or Differential is needed
# Sequentially for now
$vms | ForEach-Object {
$vm = $_
#Full path for backup
$vmFullBackupPath = "$($global:db.BackupFolderPath)\$($vm.Name)\Full"
$vmDiffBackupPath = "$($global:db.BackupFolderPath)\$($vm.Name)\Diff_$(Get-Date -UFormat "%Y-%m-%d_%H%M%S")"
# Determine if VM is in our DB
if ($global:db.VMs.VMId -contains $vm.Id) {
#Grab our VM Config from DB
$obj = $global:db.VMs | where-object { $_.VMId -eq $vm.Id }
if ($null -eq $obj.ReferencePoint) {
throw "Invalid Configuration, not ReferencePoint found";
}
try {
# Update the status of our DB item
$obj.LastBackup = [DateTime]::Now
$obj.LastStatus = "In-Progress"
# Create and remember backup snapshot of the VM
Write-Output "Creating checkpoint for $($vm.Name)"
$checkpoint = New-VmBackupCheckpoint -VmName $vm.Name -ConsistencyLevel CrashConsistent
# Find all reference points for VM and use our last saved as differential
Write-Output "Getting reference points for $($vm.Name)"
$referencePoints = Get-VMReferencePoints -VMName $vm.Name
# Match the reference points or throw an error
$lastRP = $referencePoints | where-object { $_.InstanceID -eq $obj.ReferencePoint }
if ($null -eq $lastRP) {
throw "Unable to match up Reference Points, expected $($obj.ReferencePoint), got $lastRP"
}
# Exports differential backup of the machine
Write-Output "Diff backup of $($vm.Name) to $vmDiffBackupPath"
Export-VMBackupCheckpoint -VmName $vm.Name -DestinationPath $vmDiffBackupPath -BackupCheckpoint $checkpoint -ReferencePoint $lastRP
# Removes backup snapshot and converts it as reference point for future incremental backups
Write-Output "Saving ReferencePoint"
$rp = Convert-VmBackupCheckpoint -BackupCheckpoint $checkpoint
$obj.ReferencePoint = $rp.InstanceID
$obj.LastStatus = "Success"
}
catch {
Write-Error "Operation Failed! => $_"
$obj.LastStatus = "Failed"
}
}
else {
Write-Output "VM not in DB, performing full backup"
$obj = New-Object PSObject -Property @{
"VMName" = $vm.Name;
"VMId" = $vm.Id;
"FirstBackup" = [DateTime]::Now;
"LastBackup" = [DateTime]::Now;
"LastStatus" = "In-Progress";
"ReferencePoint" = $null;
}
try {
# Perform full first backup
# Create and remember backup snapshot of the VM
Write-Output "Creating checkpoint for $($vm.Name)"
$checkpoint = New-VmBackupCheckpoint -VmName $vm.Name -ConsistencyLevel CrashConsistent
# Exports that snapshot to dedicated folder
Write-Output "Performing full backup of $($vm.Name) to $vmFullBackupPath"
Export-VMBackupCheckpoint -VmName $vm.Name -DestinationPath $vmFullBackupPath -BackupCheckpoint $checkpoint
# Removes backup snapshot and converts it as reference point for future incremental backups
Write-Output "Saving ReferencePoint"
$rp = Convert-VmBackupCheckpoint -BackupCheckpoint $checkpoint
Write-Output "Updating DB"
$obj.ReferencePoint = $rp.InstanceID
$obj.LastStatus = "Success"
$global:db.VMs += $obj
# Update / SaveDB
SaveDB -DBFilePath $DBFilePath -Data $global:db
}
catch {
Write-Error "Operation Failed! => $_"
$obj.LastStatus = "Failed"
$global:db.VMs += $obj
# Update / SaveDB
SaveDB -DBFilePath $DBFilePath -Data $global:db
}
}
}
}
function LoadDB {
param (
[Parameter(Mandatory = $true)]
[String] $DBFilePath
)
if (!(Test-Path $DBFilePath)) {
throw "Unable to find database"
exit 1;
}
try {
$obj = (get-content -Path $DBFilePath | convertfrom-json)
}
catch {
throw "Unable to load database"
}
return $obj
}
function SaveDB {
param (
[String] $DBFilePath,
[PSObject] $Data
)
$Data | ConvertTo-Json | Out-File -FilePath $DBFilePath
Write-Output "Database saved...";
}
function DoInstall {
param (
[Parameter(Mandatory = $true)]
[String] $BackupFolderPath,
[Parameter(Mandatory = $false)]
[System.Collections.Generic.List[string]] $excludeVMList
)
# Check if folder exists
if (!(Test-Path $BackupFolderPath)) {
$tmp = [System.IO.DirectoryInfo]::new($BackupFolderPath)
# Check if root exists
if (!(Test-Path $tmp.Root)) {
throw "Invalid Path $BackupFolderPath"
exit 1;
}
else {
new-item -ItemType Directory -Path $BackupFolderPath | Out-Null
}
}
#Validate the DB does not already exist
if (Test-Path -Path "$BackupFolderPath\db.json")
{
throw "DB already exists! Please delete to create new one"
exit 1;
}
# Create initial tracking DB
$obj = new-object PSObject -Property @{
"BackupFolderPath" = $BackupFolderPath;
"ExcludeVM" = $excludeVMList;
"VMs" = new-object 'System.Collections.Generic.List[PSObject]';
}
$obj | convertto-json | Out-File -FilePath "$BackupFolderPath\db.json"
}
}
Process {
switch ($Operation) {
"Install" {
if ($null -ne $excludeVMList) {
DoInstall -BackupFolderPath $DBFilePath -excludeVMList $excludeVMList
}
else {
DoInstall -BackupFolderPath $DBFilePath
}
}
"Backup" {
# Load Database
$global:db = LoadDB -DBFilePath $DBFilePath
# Fix - Type-cast the obj in case it's empty Load Backup List if any
if ($null -eq $global:db.VMs) {
$global:db.VMs = new-object 'System.Collections.Generic.List[PSObject]'
}
# Get all VMs
$vms = get-vm
# Filter unwanted VMs
# TODO figure out how to handle vms that were backed up and fall into this list
$vms = $vms | Where-Object { $global:db.ExcludeVM -notcontains $_.Name }
#Perform the backup
DoFullOrDiffBackup -vms $vms
# Save Database
SaveDB -DBFilePath $DBFilePath -Data $global:db
}
"BackupNewOnly" {
# Load Database
$global:db = LoadDB -DBFilePath $DBFilePath
# Fix - Type-cast the obj in case it's empty Load Backup List if any
if ($null -eq $global:db.VMs) {
$global:db.VMs = new-object 'System.Collections.Generic.List[PSObject]'
}
# Get all VMs
$vms = get-vm
# Filter unwanted VMs
# TODO figure out how to handle vms that were backed up and fall into this list
$vms = $vms | Where-Object { $global:db.ExcludeVM -notcontains $_.Name }
#Filter to unbacked up VMs only & those with unsuccessful status
$list1 = $vms | Where-Object { $gloal:db.VMs | Where-Object { $_.LastStatus -ne "Success" } }
$list2 = $vms | where-object { ($global:db.VMs | ForEach-Object { $_.VMName }) -notcontains $_.Name }
$vms = ($list1 + $list2)
DoFullOrDiffBackup -vms $vms
}
}
}
End {
Write-Output "Finished..."
}
# http://www.systanddeploy.com/2018/12/create-your-own-powershell.html