Backing up and restoring Azure VMs

 

Note – none of the details below are required for simply creating and using VMs in Azure directly.  The details come into play if you start ending to copy VHDs around and recreate VMs from them.

Install Azure Powershell

You need the command line Azure tools for PowerShell. Good description of getting started is here: http://azure.microsoft.com/en-us/documentation/articles/install-configure-powershell/

from here (download and install):

http://go.microsoft.com/fwlink/p/?linkid=320376&clcid=0x409

Will take a few minutes probably as it has a number of prerequisites that it may also have to fetch.  I also installed Windows Azure powershell

image

Eventually you will get a Windows Azure program folder and within that a Windows Azure Powershell shortcut

image

Connect to your subscription

I used the certificate method described here: http://azure.microsoft.com/en-us/documentation/articles/install-configure-powershell/

  • Get-AzurePublishSettingsFile (opens browser here you log onto your Azure account and it generates and downloads a file for publishing settings)
  • Import-AzurePublishSettingsFile  "C:\Temp\Microsoft Partner Network-Windows Azure  MSDN - Visual Studio Premium-6-11-2014-credentials.publishsettings"  (Import subscription settings)

Accessing your VMs

If all has gone well – Get-AzureVM should now list your VMs

image

There are options for starting and stopping – but I’d just use the Azure web pages normally.

Copying VHDs (Blobs) between storage accounts

From: http://michaelwasham.com/windows-azure-powershell-reference-guide/copying-vhds-blobs-between-storage-accounts/

Michael gives a nice overview of how Azure storage is structured and some indicative timings as to how long copies may take (for 127GB VHD 12 minutes within data centre but cross stamp and 23 minutes between data centres (- I suspect this depends on where the datacentres are)

This command will complete quickly – but the copy will take a while to complete – so see section below on checking progress

Select-AzureSubscription "Windows Azure  MSDN - Visual Studio Premium" 

 
### Source VHD (West US) - authenticated container ###
$srcUri = "https://[StorageName].blob.core.windows.net/vhds/Test.vhd" 

 
### Source Storage Account (West US) ###
$srcStorageAccount = [SourceAccountName]
$srcStorageKey = [SourceKey]

 
### Target Storage Account (West US) ###
$destStorageAccount = [DestinationAccountName]
$destStorageKey = [DestinationKey]

 
### Create the source storage account context ### 
$srcContext = New-AzureStorageContext  –StorageAccountName $srcStorageAccount `
                                        -StorageAccountKey $srcStorageKey  

 
### Create the destination storage account context ### 
$destContext = New-AzureStorageContext  –StorageAccountName $destStorageAccount `
                                        -StorageAccountKey $destStorageKey  

 
### Destination Container Name ### 
$containerName = "copiedvhds"

 
### Create the container on the destination ### 
New-AzureStorageContainer -Name $containerName -Context $destContext 

 
### Start the asynchronous copy - specify the source authentication with -SrcContext ### 
$blob1 = Start-AzureStorageBlobCopy -srcUri $srcUri `
                                    -SrcContext $srcContext `
                                    -DestContainer $containerName `
                                    -DestBlob "testD2.vhd" `
                                    -DestContext $destContext

I get an error – but the command seems to complete OK

image

Checking the progress of a blob copy

These can take a while – especially if between data centres – so its useful to be able to check how they are progressing.

$context = New-AzureStorageContext -StorageAccountName jamestestbackup –StorageAccountKey [your key]

Get-AzureStorageBlob -Container "copiedvhds" -Context $context | Get-AzureStorageBlobCopyState

image

Stopping an asynchronous blob

Copying blobs around can be tricky – especially if you are moving between accounts (and storage zones which makes things slow).

I managed to get some copies that seemed to appear in the destination – but were locked with a lease for a pending copy.  I managed to sop these pending copies by:

Select-AzureSubscription -SubscriptionName "Your Subscription Name"

$context = New-AzureStorageContext -StorageAccountName jamestestbackup -StorageAccountKey [your key]

Get-AzureStorageBlob -Container "copiedvhds" -Context $context| Stop-AzureStorageBlobCopy –Force

Creating a new VM from a BLOB

You can not do this until the copy is complete.  The Azure interface seems to give no indication of when this is (see above for powershell script).  If its not complete you will get an error about a lease being held (as the copy is still in progress) when you try and create the vhd

There are a couple of steps here:

1) You must use the VM, Disks section to create a VHD Disk from your BLOB (note your blob must have a .vhd file extension for you to be able to see it

2) Then you can create a new VM.  Use the “From Gallery” option and you will see an option bottom left that allows you to pick your own disks

Free tools

This can be useful as an alternative interface for working with blobs

http://www.cerebrata.com/download/azure-explorer

Comments

Find out more