Automating Windows #Azure VM backups

Using Azure IaaS is great, but like all IaaS solutions it still requires some administrative efforts. In my last installment, I detailed how to use Windows Azure backup and recovery services to protect data contained in virtual machines and how to monitor the success of the backup operations with System Center Operations Manager ( Protecting data is only one of the aspects for a complete data protection solution, a complete virtual machine corruption it’s not addressed by data protection. Since we don’t have access to host based backup a common strategy is to create a clone of the virtual machine’s disks.

In Azure this needs to be accomplished stopping the virtual machine and copying the virtual disks, the high-level steps are:

  • Stop the VM
  • Identify and copy the OS disk
  • Identify and copy the Data disks
  • Restart the VM

Obviously when drilling into the single steps things are a little more complicated; fortunately, Windows Azure has a complete SDK and a powershell module that can be leveraged.

This post presents a powershell module to accomplish this task hiding all the complexities underneath a single easy call. The module exposes a Backup-AzureVM function. The function works under the following assumptions:

  • The powershell module has been imported
  • The current subscription hosts the VM to be backed up
  • The current subscription hosts the storage account used for the backup copies

The final result is a copy of the VM disks, optionally including data disks. The backup disks will be time stamped following this schema:  <vm name>-<yyyyMMdd>-<HHmm>-<original blob name>

The function takes the following parameters:

          ServiceName – name of the Azure service hosting the VM

          VMName – name of the virtual machine

          BackupContainerName – name of the backup container

          BackupStorageAccountName – name of the backup storage account

          IncludeDataDisks – set if data disks need to backed up

All this said, you can now backup your VM with 4 lines of powershell code



    Import-Module Azure


    Select-AzureSubscription -SubscriptionName ‘QND Subscription’

    Backup-AzureVM -serviceName ‘QNDBackup’ -vmName ‘QNDTest1’ -backupContainerName ‘backup’ -backupStorageAccountName ‘qndvms’ -includeDataDisks


This snippet takes a backup copy of all the disks for the VM named QNDTest1 in service QNDBackup for subscription ‘QND Subscription’ and saves them into the ‘backup’ container in storage account ‘qndvms’.

As you can read the function make it possible to have a specific storage account as a target for your backed up disks. This permits to save backup copies in a different storage account from the one that contains your running virtual machines. This is useful if you want this storage account not to be geo-replicated (it costs less), or if you just want to keep it separated from the storage account used for the running virtual machines. Using a different storage account has a drawback: longer virtual machine downtime. For example in my tests backing up a VM with an OS data disk set at 50 GB and a data disk sized 60 GB takes 70’ when using different storage accounts, while only 3’ when using the same storage account used to host virtual machines.

This is a starting point, a whole deployment backup can be automated, using this module, with System Center SMA taking into account Virtual Machines’s availability sets. A good idea for another post.

The powershell module can be downloaded from Technet Gallery.

  • Daniele

This posting is provided “AS IS” with no warranties, and confers no rights.

  1. #1 by gator on May 7, 2014 - 2:33 pm

    This works great, exactly what I was needed (I was in the process of writing my own when I found this). Thank you for creating and making it available. A couple of small issues I found:

    1. The flag “-dstStorageAccountName” should actually be “-backupStorageAccountName”

    2. $StartTime is not defined or assigned in the script, so an error will occur “Cannot find an overload for “Subtract” and the argument count: 1″. I resolved this by adding it in the try block of Backup-AzureVM:

    try {

    $StartTime = Get-Date

    • #2 by Daniele Grandini on May 9, 2014 - 7:28 am

      Hi Gator,
      hapy to be helpful :-), you’re right I changed the parameters name to be more consistent and I dind’t fix the sample. Regarding the $startTime issue, it’s junk left from the debug code. You can add $startTime=get-date before the Start-AzureStorageBlobCopy and then possibly dump the elapsed time, or remove the $elapsed altogether.

      – Daniele

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: