Restore Azure Virtual Machines to an Availability Set Using PowerShell
In this post, I am going to show you one way to restore an Azure virtual machine from Azure Backup. From there, we will join that machine to an existing availability set.
When you do a simple next > next > next restore of an Azure virtual machine from Azure Backup, the newly created machine does not join an availability set. That is a problem because you might have joined the original machine to an availability set with high higher levels of planned or unplanned outage tolerance. You would expect the restored machine to have the same levels of redundancy. However, it will not. There is not a “re-join the original availability set” option.
There are a few workarounds available to us now. In this post, I am going to focus on the original workaround. I will show you how to do a restore of the disks to a storage account only. There will not be a virtual machine created. Using those disks, we will have to create a new virtual machine in the availability set of choice.
This example is based on Azure Resource Manager or ARM.
Restore the Disks Using PowerShell
The first things you need to do with Azure PowerShell:
- Make sure you have the latest version of the PowerShell modules or you will get weird errors.
- Sign into Azure.
- Select the correct subscription.
In my example, I “accidentally” deleted a protected virtual machine called vm-petri-01. This was in a resource group called rg-petrisource and stored in a storage account called rgpetrisourcedisks369. I had previously configured backups to a recovery services vault called rsv-petri.
I will store a few variables in memory and they can easily be reused:
$VMResourceGroupName = "rg-petrisource" $VMName = "vm-petri-01" $StorageAccount = “rgpetrisourcedisks369”
You need to set a recovery services vault context. This is like marking a recovery services vault as the current working resource. I will store the vault in a variable called $Vault and then set the vault context.
$Vault = Get-AzureRmRecoveryServicesVault -Name “rsv-petri” Set-AzureRmRecoveryServicesVaultContext -Vault $Vault
A virtual machine is backed up into a container. We will retrieve that container using the following line:
$NamedContainer = Get-AzureRmRecoveryServicesBackupContainer -ContainerType AzureVM –Status Registered -FriendlyName $VMName
We will select the item backed up for that virtual machine from the container:
$BackupItem = Get-AzureRmRecoveryServicesBackupItem –Container $NamedContainer –WorkloadType "AzureVM"
To do a restore, we must select a recovery point. The next line will retrieve all of the recovery points:
$RecoveryPoints = Get-AzureRmRecoveryServicesBackupRecoveryPoint -Item $BackupItem
The results of the above line are stored in an array. It is a variable with multiple selectable entries, one for each recovery point. I will display the available recovery points and I can pick one.
I am going to use the most recent recovery point, which is the first entry in the array (item 0). I can see more details about the recovery point by running:
$RecoveryPoints | fl *
Now it is time to do the restore. I will save the results of the restore action to a variable and I can track the job’s status:
$RestoreJob = Restore-AzureRmRecoveryServicesBackupItem -RecoveryPoint $RecoveryPoints -StorageAccountName $StorageAccount -StorageAccountResourceGroupName $ResourceGroupName
The job will take some time. You can track the job by querying the results of $RestoreJob.
Re-Create the Virtual Machine
The next step in this method is to create a new virtual machine using PowerShell. You could use JSON to create the new machine. There is a great script shared by Microsoft employee Rhoderick Milne. This will do the trick:
- Get the URI (the URL) of the restored OS disk from the storage account, which is easily found in the Azure Portal.
- Download the script.
- Modify the variables to account for things such as the desired machine name, disk URI, availability set name, and so on.
- Run the script.
When you browse your storage account, you will spot some very interesting BLOBs. Some JSON files are restored for you, which give you the ability to restore the virtual machine using a customizable JSON deployment!
A few minutes after running the script, you will have a new virtual machine with the restored operating system disk. If there were any data disks, you can attach the restored data disks to the virtual machine.