-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-backups.ps1
158 lines (141 loc) · 5.78 KB
/
process-backups.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
param(
[switch]$BackupAll
)
#
# Script Variables
#
$BackupVM_List = Get-VM -Tag "Permit-Backups"; # Tag to search for in vCenter. Set "*" to check all VM's
$Backup_Datastore = Get-Datastore -Name "HomeNAS-Cache"; # Datastore to place backups
$Backup_Location = Get-Folder -Name "Backup Machines"; # Location in vCenter to store VM topologically
$backupDatabase = @(); # Backup Database
$CloneDate = Get-Date -Format "yyyyMMdd-hhmmss"; # Timestamp for comments and names of backups
#
# Import and Set Certification Actions
#
Get-Module -ListAvailable PowerCLI* | Import-Module
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -confirm:$False
# Do not use this line with user name and password in production enviroments.
# By Removing the User and Password variables the script will prompt for cred.
Connect-VIServer -Server "0.0.0.0" -User "user@domain.local" -Password "SomeP@ssw0rd" -Force
#
# Backup Process
#
Function Process-Backup($VM) {
# Create Snapshot Name
$SnapshotName = "$($VM.Name) Snapshot - $($CloneDate)";
$BackupName = "$($CloneDate) - $($VM.Name) Backup";
Write-host "Starting VM Backup of the Following VM:" $VM.Name;
Write-Host "Creating snapshot of $($VM.Name): " -NoNewline;
# Create Snapshot before cloning.
$Snapshot_Temp = New-Snapshot -VM $VM -Name $SnapshotName -Verbose:$false;
Write-Host "Done...";
# Cloning VM
Write-Host "Creating backup of $($VM.Name): " -NoNewline;
$Backup_Temp1 = New-VM -Name "$($BackupName)-Temp" -VM $VM -VMHost $VM.VMHost -Location $Backup_Location -Datastore $Backup_Datastore -LinkedClone -ReferenceSnapshot $SnapshotName;
$Backup_Temp2 = New-VM -Name $BackupName -VM "$($BackupName)-Temp" -VMHost $VM.VMHost -Location $Backup_Location -Datastore $Backup_Datastore;
Write-Host "Done...";
# Remove VM from vCenter
Write-Host "Removing backup of $($BackupName) from vCenter: " -NoNewline;
Remove-VM -VM $Backup_Temp1 -DeletePermanently -Confirm:$false;
Remove-VM -VM $Backup_Temp2 -Confirm:$false;
Write-Host "Done...";
# Remove Snapshot after clone.
Write-Host "Deleting snapshot of $($VM.Name): " -NoNewline;
$nullString = ($Snapshot_Temp | Remove-Snapshot -Confirm:$false -Verbose:$false -RunAsync)
Write-Host "Done...";
Write-Host "Backup of $($VM.Name) complete."
Write-Host ""
}
#
# Unattended Switches
#
if($BackupAll) {
$BackupVM_List | Foreach-Object {
# Process the Backup for All VM's with the marked tag.
Process-Backup($_);
}
exit;
}
#
# Function that makes menu... Copied from stackoverflow.com (https://stackoverflow.com/questions/48691249/option-menu-in-powershell-continue-after-loop)
# Changed exit info and adjusted for menu width for the line.
#
Function MenuMaker{
param(
[parameter(Mandatory=$true)][String[]]$Selections,
[switch]$IncludeExit,
[string]$Title = $null
)
$Width = if($Title){$Length = $Title.Length;$Length2 = $Selections|%{$_.length}|Sort -Descending|Select -First 1;$Length2,$Length|Sort -Descending|Select -First 1}else{$Selections|%{$_.length}|Sort -Descending|Select -First 1}
$Buffer = if(($Width*1.5) -gt 78){[math]::floor((78-$width)/2)}else{[math]::floor($width/4)}
if($Buffer -gt 6){$Buffer = 6}
$MaxWidth = $Buffer*2+$Width+$($Selections.count).length+2
$Menu = @()
$Menu += "╔"+"═"*$maxwidth+"╗"
if($Title){
$Menu += "║"+" "*[Math]::Floor(($maxwidth-$title.Length)/2)+$Title+" "*[Math]::Ceiling(($maxwidth-$title.Length)/2)+"║"
$Menu += "╟"+"─"*$maxwidth+"╢"
}
For($i=1;$i -le $Selections.count;$i++){
$Item = "$(if ($Selections.count -gt 9 -and $i -lt 10){" "})$i`. "
$Menu += "║"+" "*$Buffer+$Item+$Selections[$i-1]+" "*($MaxWidth-$Buffer-$Item.Length-$Selections[$i-1].Length)+"║"
}
If($IncludeExit){
$Menu += "║"+" "*$MaxWidth+"║"
$Menu += "║"+" "*$Buffer+"-1 - Exit"+" "*($MaxWidth-$Buffer-9)+"║"
}
$Menu += "╚"+"═"*$maxwidth+"╝"
$menu
}
#
# Main Script
#
While($true) {
Cls;
Clear-Variable -Name "Selection" -ErrorAction SilentlyContinue;
$SelectionList = $BackupVM_List + "All";
MenuMaker -Selections $SelectionList -Title 'Choose Virtual Machine' -IncludeExit:$true
Write-Host ""
if([int]$Selection = Read-Host "Choose Virtual Machine" -ErrorAction SilentlyContinue)
{
switch -Exact ($Selection) {
-1 {
exit;
}
{($_ -ige 1) -and ($_ -ile $BackupVM_List.Length)} {
Cls;
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host "Backing up all $($BackupVM_List.Item($Selection-1).Name)"
Write-Host
Process-Backup($BackupVM_List.Item($Selection-1));
$prompt = Read-Host -Prompt "Press enter to continue"Select
continue;
}
{($BackupVM_List.Length+1)} {
Cls;
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host "Backing up all Virtual Machines"
Write-Host
$BackupVM_List | Foreach-Object {
# Process the Backup for All VM's with the "Allow-Backups" tag.
Process-Backup($_);
}
$prompt = Read-Host -Prompt "Press enter to continue"
continue;
}
default {
$prompt = Read-Host -Prompt "Only $($BackupVM_List.Length+1) to choose from. Press any key to try again or -1 to quit"
}
}
}
}
#
# Forced exit just in-case...
#
exit;