Automating Azure VM Backups Using ARM Templates

In this post I will give a quick overview of automating Azure VM backups using ARM templates. There are quick start templates for backups when you search for them but the syntax for the resource names didn’t make sense to me, hence this post to explain it a bit more. I will also touch on using Azure Resource Explorer which is a great tool for understanding the ARM API.

Azure VM Backups Background

To backup Azure VMs with Azure Backup you have to create a Recovery Services vault, create a backup policy which contains the schedule and backup retention settings and register your VMs for backups to the vault and selected policy. There are quick start templates to create vaults, policies and schedules here. Although you won’t see it from the portal there is also the notion of a container for storing backups. The type of container depends on the items that are backed up, there are containers for Azure VMs, SQL backups and Windows backups. This is the part that wasn’t clear to me in authoring ARM templates but next I’ll show you how to shed some light on them.

Azure VM Backup Resource Syntax

One way to figure out the syntax for an ARM template is to look at existing resources and export the template from the Azure Portal using the Automation script blade but that will export your vault only, not your VM backups.

Next I tried Azure Resource Explorer, drilling down to my recovery vault I didn’t see the VMs that are registered for backups or the policies but I did manage to find the deployments for them in:

{resourcegroup}\Microsoft.Resources\deployments

The deployments to create a backup policy are named CreatePolicy* and the deployments to register a VM for backups are named ConfigureProtection*. In the deployment for VM protection I managed to find the syntax for the resourceName.

Automating Azure VM Backups Using ARM Templates

The ARM template to register multiple VMs for backups is in my GitHub repo here. The template assumes the VMs are not in the same resource group as the recovery vault since the backup vault was contained in a separate management resource group in this case.

A Bit More On Azure Resource Explorer

I highly recommend taking a few minutes to take a look at the Azure Resource Explorer. It is a great way to explore the syntax for Azure ARM Rest API, there are tabs to execute some Rest API commands directly from the explorer, PowerShell samples to create the currently selected resource and documentation for the selected resource.

Francois Delport

Azure Backup

I have been using Azure Backup for a while now and I’m going to give a quick overview, especially around notifications which are not that easy to find. The official FAQ documentation is over here.

Azure Backup is able to backup entire Azure VMs, on-premise Windows Servers, SQL Server databases and files/folders. Some of the backups types like SQL and Hyper-V require Data Protection Mananger (DPM) which is part of System Centre or Azure Backup Server which is a stand alone product .The official getting started guide is  here but the short version is:

  • Create a backup vault that will contain your backups.
  • Create retention policies if you don’t want to use the default one.
  • For on-premise: Download the backup agent and credentials from the Azure portal, install the backup agent and configure the backup schedule.
  • For Azure VMs: Discover and register Azure VMs using the Azure Portal.

You can create fine grained retention policies to govern your data storage  ranging from 1 day to 99 years enabling you to replace tape archives for instance. The backups are compressed and incremental, even the full machine image backups will compare changes at the block level, you can get more details here. This saves space but takes a little longer to restore since your data is reconstructed using a chain of incremental backups but in my experience it wasn’t so slow that it became a problem.

It is important but also obvious that your first backup will be a full one. Depending on the size of the servers or files you are backing up you may want to do this on physical media and ship it to Microsoft instead of using your internet connection see details here. You can choose local redundant or geo redundant storage for your backup vaults when you create them, it can’t be changed once you registered items for backup.

One aspect of Azure Backup that I found difficult to use was notifications. I think most users would like to know when a backup failed via email and the obvious place to do this would be the backup section in the portal with an add alert button for instance but that is not the case. You can setup alerts using management services and Powershell, this method is not specifically for backups you can use it to receive alerts for any job failure. At the time of writing this only worked with VM backups not other types. You can find the details here. The short version is:

  • Retrieve the resource URI for your backup vault. One way is to use the management services section of the old azure portal, find your backup jobs and view the details of the job to get the resource URI.
  • Run the “Add-AlertRule” Powershell cmdlet to create a new notification for that resource URI. This will be at the vault level, not a specific backup job.

You can also roll your own by creating a script to retrieve backups jobs but at the time of writing this only worked for Azure VM backups not other types.

$vaults = Get-AzureRmBackupVault

foreach ($vault in $vaults)
{
    $jobs = Get-AzureRmBackupJob -Vault $vault -From $startdate 
            ` -To $enddate

    foreach($job in $jobs)
    {
        #your own code to do something
    }
}

Francois Delport