Technical tidbits from the sysadmin world...
 

Azure Recovery Services Vault – Restore disks via powershell

The long overdue update to Azure’s Recovery Vault to support ARM virtual machines has finally arrived.  With it are some nice changes to how you backup and recover your VMs, however as is typical with Azure on release of a new or updated feature the documentation is not the best!

We’ve been waiting for the update to this part of Azure for quite a while at work and dove right in once it came out.  One of our main requirements is that in case of a restore we wanted to restore just the disks so that we can redeploy the server from Powershell with a more ‘advanced’ configuration (Availability Set, choose the IP, disk name etc).

Unfortunately the documentation on how to do a disk only restore is terribly incomplete but with some trial and error here is how you do it below.  A reminder that you will need at least Azure Powershell v1.4.0 to have the cmdlets.

Connect to the vault
#connect to the vault
$vault = Get-AzureRmRecoveryServicesVault -Name AzMonkey-RSV -ResourceGroupName AZMonkey
Set-AzureRmRecoveryServicesVaultContext -Vault $vault
Find which protected VM you want to restore from
#find protected VMs
Get-AzureRmRecoveryServicesBackupItem -BackupManagementType AzureVM -WorkloadType AzureVM

Which gives you an output similar to this:

Name                                     ContainerType        ContainerUniqueName                      WorkloadType         ProtectionStatus    
----                                     -------------        -------------------                      ------------         ----------------    
iaasvmcontainerv2;azmonkey;azmonkeyvm01  AzureVM              iaasvmcontainerv2;azmonkey;azmonkeyvm01  AzureVM              Healthy             
iaasvmcontainerv2;azmonkey;azmonkeyvm02  AzureVM              iaasvmcontainerv2;azmonkey;azmonkeyvm02  AzureVM              Healthy             
Select the VM (use the last portion of the name e.g. azmonkeyvm01 from the above output)
#Select the VM you wish to restore from
$backupitem=Get-AzureRmRecoveryServicesBackupItem -BackupManagementType AzureVM -WorkloadType AzureVM -Name azmonkeyvm02
See what restore points are available
#Check what restore points are available (last 7 days - adjust the -7 for more days)
$startDate = (Get-Date).AddDays(-7)
$endDate = Get-Date
Get-AzureRMRecoveryServicesBackupRecoveryPoint -Item $backupitem -StartDate $startdate.ToUniversalTime() -EndDate $enddate.ToUniversalTime()

Which gives you a list of recovery points (last 7 days from the command but you can change the start date!)

RecoveryPointId    RecoveryPointType  RecoveryPointTime      ContainerName                        ContainerType  
---------------    -----------------  -----------------      -------------                        -------------  
6653637261308      FileSystemConsi... 14/05/2016 1:06:25 AM  IaasVMContainer;iaasvmcontainerv2... AzureVM        
17221207186083     FileSystemConsi... 14/05/2016 12:33:33 AM IaasVMContainer;iaasvmcontainerv2... AzureVM        

Select the restore point (top in the list is 0 (1:06:25am), second listed is 1)
#Set the first part of the $rp variable to select a recovery point
$rp = Get-AzureRMRecoveryServicesBackupRecoveryPoint -Item $backupitem -StartDate $startdate.ToUniversalTime() -EndDate $enddate.ToUniversalTime()
#Select the restore point - latest is 0, next is 1 etc...
$rp[0]
Execute the restore
#Load the restore variable with the recovery point and storage account location to restore to (it uses a guid for the container and vhd name)
$restorejob = Restore-AzureRMRecoveryServicesBackupItem -RecoveryPoint $rp[0] -StorageAccountName azmonkeyslrs -StorageAccountResourceGroupName AZMonkey
#Execute the restore
$restorejob

Once you kick off the restore you can check in the Portal or powershell to see how the job is going (under backup jobs).

#Check progress
Get-AzureRmRecoveryServicesBackupJobDetails -job $restorejob

WorkloadName Operation Status StartTime EndTime JobID 
------------ --------- ------ --------- ------- ----- 
azmonkeyvm02 Restore InProgress 14/05/2016 4:43:53 AM 1/01/0001 12:00:00 AM c29fdfea-0986-4cee-a43f-72d5d818bee6

When it is finished you’ll see the following in the portal with the name and location of your restored vhd files:

Azure Recovery Services - Restore Complete

And with that, job done – provision your VM again with your powershell scripts.