-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-AzurevmBackup.ps1
154 lines (130 loc) · 6.33 KB
/
Get-AzurevmBackup.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
<#
.Synopsis
Given a VM OS disk backed up by the Backup-AzureVM.ps1 script, list all of the backups.
.DESCRIPTION
List all of the backups on the blob store of the given VM, using the naming convention.
Please note the results are not sorted, and to discover the order of the backups.
The backups are as copies of the backing disk blob, with the name convention:
<Name of the backing blob of the OS disk, without the .vhd extension>_v_<serviceName>-<vmName>_b_<date in yyyy-mm-dd format>-<backup number of the day>.vhd"
.EXAMPLE
Get the backups of an existing VM, querying the same storage account the VMs disk is kept.
.\Get-AzurevmBackup.ps1 -ServiceName AService -Name vmName
Get the backups of an existing VM, querying the specified storage account.
.\Get-AzurevmBackup.ps1 -FindInStorage -ServiceName AService -Name vmName -StorageAccountName storageaccount
.INPUTS
None
.OUTPUTS
Hashtable of compressed backup number in the form of yyyymmdd<dailybackupnumber>
#>
Param
(
# Service the VM is running on
[Parameter(Mandatory=$true, ParameterSetName="FindInStorage")]
[switch]
$FindInStorage,
# Service the VM is running on
[Parameter(Mandatory=$true, ParameterSetName="UseVMsStorage")]
[Parameter(ParameterSetName="FindInStorage")]
[String]
$ServiceName,
# Name of the VM
[Parameter(Mandatory=$true, ParameterSetName="UseVMsStorage")]
[Parameter(ParameterSetName="FindInStorage")]
[String]
$Name,
# Name of the storage account for the backup blobs
[Parameter(Mandatory=$true, ParameterSetName="FindInStorage")]
[String]
$StorageAccountName,
# Name of the storage account container for the backup blobs
[Parameter(Mandatory=$false, ParameterSetName="FindInStorage")]
[String]
$StorageAccountContainer = "vhds"
)
$currentAzureSubscription = Get-AzureSubscription -Current
$currentStorageAccountName = $currentAzureSubscription.CurrentStorageAccount
if ($PSCmdlet.ParameterSetName -ne "FindInStorage")
{
$vm = Get-AzureVM -ServiceName $ServiceName -Name $Name -ErrorAction SilentlyContinue
$containerName = ""
$diskBlobName = ""
if ($vm -eq $null)
{
Write-Warning "A virtual machine with name $Name on $ServiceName does not exist, current storage account on the subscription will be used."
$StorageAccountName = $currentStorageAccountName
}
else
{
$osDiskMediaLinkUri = [System.Uri]$vm.VM.OSVirtualHardDisk.MediaLink
if ($osDiskMediaLinkUri.Segments.Count -gt 3)
{
throw "Disk containers only one level deep supported"
}
# If it is a 3 part segment, first part willbe / second will be the container name, and third part will be the blob name
$StorageAccountContainer = $osDiskMediaLinkUri.Segments[1].Replace("/","")
$diskBlobName = $osDiskMediaLinkUri.Segments[2]
$StorageAccountName = $osDiskMediaLinkUri.Host.Split(".")[0]
# Change the current storage account
if ($StorageAccountName -ne $currentStorageAccountName)
{
Set-AzureSubscription -SubscriptionName $currentAzureSubscription.SubscriptionName -CurrentStorageAccount $StorageAccountName
}
}
}
else
{
# Change the current storage account
if ($StorageAccountName -ne $currentStorageAccountName)
{
Set-AzureSubscription -SubscriptionName $currentAzureSubscription.SubscriptionName -CurrentStorageAccount $StorageAccountName
}
}
$backupNameDelimeter = "_b_"
$diskDelimeter = "_d_"
$existingBackups = Get-AzureStorageBlob -Container $StorageAccountContainer |
Where-Object {$_.Name -match $(".*" + $ServiceName + "-" + $Name + $backupNameDelimeter + "[0-9][0-9]" + $diskDelimeter + "[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{4}\.vhd$")} |
Select-Object Name
$foundBackups = @()
if($existingBackups -ne $null)
{
foreach ($existingBackup in $existingBackups)
{
$parts = $existingBackup.Name -split $backupNameDelimeter
if ($parts.Count -ne 2)
{
throw "Unexpected backup format for blob name $existingBackup"
}
$backupPart = ""
$vmParts = $parts[0] -split "-"
if ($parts[1].Endswith(".vhd"))
{
$backupPart = $parts[1].Substring(0, $parts[1].Length - 4)
}
else
{
$backupPart = $parts[1]
}
$backupParts = $backupPart -split $diskDelimeter
if ($backupParts.Count -ne 2)
{
throw "The backup name does not conform to the naming convention."
}
$diskNumber = $backupParts[0]
$backupParts = $backupParts[1] -split "-"
if ($vmParts.Count -ne 2 -and $backupParts.Count -ne 4)
{
throw "The backup name does not conform to the naming convention."
}
$objBackup = New-Object System.Object
$objBackup | Add-Member -type NoteProperty -name ServiceName -value $vmParts[0]
$objBackup | Add-Member -type NoteProperty -name VmName -value $vmParts[1]
$objBackup | Add-Member -type NoteProperty -name BackupDate -value $($backupParts[1] + "/" + $backupParts[2] + "/" + $backupParts[0])
$objBackup | Add-Member -type NoteProperty -name BackupNumber -value $backupParts[3]
$objBackup | Add-Member -type NoteProperty -name BlobName -value $existingBackup.Name
$objBackup | Add-Member -type NoteProperty -name BackupId -value ([Int64]$($backupParts[0] + $backupParts[1] + $backupParts[2] + $backupParts[3]))
$foundBackups += $objBackup
}
}
# Restore the original CurrentStorageAccount setting
Set-AzureSubscription -SubscriptionName $currentAzureSubscription.SubscriptionName -CurrentStorageAccount $currentStorageAccountName
$foundBackups | Sort-Object -Property BackupId