Back up an Azure VM using Azure Backup via REST API
Article
This article describes how to manage backups for an Azure VM using Azure Backup via REST API. Configure protection for the first time for a previously unprotected Azure VM, trigger an on-demand backup for a protected Azure VM and modify backup properties of a backed-up VM via REST API as explained here. To protect an Azure VM using the Azure portal, see this article.
Learn how to create vault and create policy REST API tutorials for creating new vaults and policies.
Let's assume you want to protect a VM testVM under a resource group testRG to a Recovery Services vault testVault, present within the resource group testVaultRG, with the default policy (named DefaultPolicy).
Configure backup for an unprotected Azure VM using REST API
Discover unprotected Azure VMs
First, the vault should be able to identify the Azure VM. This is triggered using the refresh operation. It's an asynchronous POST operation that makes sure the vault gets the latest list of all unprotected VM in the current subscription and 'caches' them. Once the VM is 'cached', Recovery services will be able to access the VM and protect it.
HTTP
POST https://management.azure.com/Subscriptions/{subscriptionId}/resourceGroups/{vaultresourceGroupname}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/{fabricName}/refreshContainers?api-version=2016-12-01
The POST URI has {subscriptionId}, {vaultName}, {vaultresourceGroupName}, {fabricName} parameters. The {fabricName} is "Azure". According to our example, {vaultName} is "testVault" and {vaultresourceGroupName} is "testVaultRG". As all the required parameters are given in the URI, there's no need for a separate request body.
HTTP
POST https://management.azure.com/Subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testVaultRG/providers/Microsoft.RecoveryServices/vaults/testVault/backupFabrics/Azure/refreshContainers?api-version=2016-12-01
Responses to refresh operation
The 'refresh' operation is an asynchronous operation. It means this operation creates another operation that needs to be tracked separately.
It returns two responses: 202 (Accepted) when another operation is created, and 200 (OK) when that operation completes.
Name
Type
Description
204 No Content
OK with No content returned
202 Accepted
Accepted
Example responses to refresh operation:
Once the POST request is submitted, a 202 (Accepted) response is returned.
Track the resulting operation using the "Location" header with a simple GET command
HTTP
GET https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testVaultRG/providers/microsoft.recoveryservices/vaults/testVault/backupFabrics/Azure/operationResults/aad204aa-a5cf-4be2-a7db-a224819e5890?api-version=2019-05-13
Once all the Azure VMs are discovered, the GET command returns a 204 (No Content) response. The vault is now able to discover any VM within the subscription.
You can confirm that "caching" is done by listing all protectable items under the subscription and locate the desired VM in the response. The response of this operation also gives you information on how Recovery Services identifies a VM. Once you're familiar with the pattern, you can skip this step and directly proceed to enabling protection.
This operation is a GET operation.
HTTP
GET https://management.azure.com/Subscriptions/{subscriptionId}/resourceGroups/{vaultresourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupProtectableItems?api-version=2016-12-01&$filter=backupManagementType eq 'AzureIaasVM'
The GET URI has all the required parameters. No additional request body is needed.
The number of values in a GET response is limited to 200 for a 'page'. Use the 'nextLink' field to get the URL for next set of responses.
The response contains the list of all unprotected Azure VMs and each {value} contains all the information required by Azure Recovery Service to configure backup. To configure backup, note the {name} field and the {virtualMachineId} field in {properties} section. Construct two variables from these field values as mentioned below.
After the relevant VM is "cached" and "identified", select the policy to protect. To know more about existing policies in the vault, refer to list Policy API. Then select the relevant policy by referring to the policy name. To create policies, refer to create policy tutorial. "DefaultPolicy" is selected in the example below.
Enabling protection is an asynchronous PUT operation that creates a 'protected item'.
The {containerName} and {protectedItemName} are as constructed above. The {fabricName} is "Azure". For our example, this translates to:
HTTP
PUT https://management.azure.com/Subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testVaultRG/providers/Microsoft.RecoveryServices/vaults/testVault/backupFabrics/Azure/protectionContainers/iaasvmcontainer;iaasvmcontainerv2;testRG;testVM/protectedItems/vm;iaasvmcontainerv2;testRG;testVM?api-version=2019-05-13
Create the request body
To create a protected item, following are the components of the request body.
The {sourceResourceId} is the {virtualMachineId} mentioned above from the response of list protectable items.
Responses to create protected item operation
@01011011
Responses to create protected item operation
The creation of a protected item is an asynchronous operation. It means this operation creates another operation that needs to be tracked separately.
It returns two responses: 202 (Accepted) when another operation is created, and 200 (OK) when that operation completes.
Example responses to create protected item operation:
Once you submit the PUT request for protected item creation or update, the initial response is 202 (Accepted) with a location header or Azure-async-header.
Then track the resulting operation using the location header or Azure-AsyncOperation header with a simple GET command.
HTTP
GET https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testVaultRG/providers/microsoft.recoveryservices/vaults/testVault/backupFabrics/Azure/protectionContainers/iaasvmcontainer;iaasvmcontainerv2;testRG;testVM/protectedItems/vm;testRG;testVM/operationsStatus/a0866047-6fc7-4ac3-ba38-fb0ae8aa550f?api-version=2019-05-13
Once the operation completes, it returns 200 (OK) with the protected item content in the response body.
This confirms that protection is enabled for the VM and the first backup will be triggered according to the policy schedule.
Excluding disks in Azure VM backup
Azure Backup also provides a way to selectively back up a subset of disks in Azure VM. More details are provided here. If you want to selectively back up few disks during enabling protection, the following code snippet should be the request body during enabling protection.
In the request body above, the list of disks to be backed up are provided in the extended properties section.
Property
Value
diskLunList
The disk LUN list is a list of LUNs of data disks. OS disk is always backed up and doesn't need to be mentioned.
IsInclusionList
Should be true for the LUNs to be included during backup. If it's false, the aforementioned LUNs will be excluded.
So, if the requirement is to back up only the OS disk, then all data disks should be excluded. An easier way is to say that no data disks should be included. So the disk LUN list will be empty and the IsInclusionList will be true. Similarly, think of what is the easier way of selecting a subset: A few disks should be always excluded or a few disks should always be included. Choose the LUN list and the boolean variable value accordingly.
Trigger an on-demand backup for a protected Azure VM
Once an Azure VM is configured for backup, backups happen according to the policy schedule. You can wait for the first scheduled backup or trigger an on-demand backup anytime. Retention for on-demand backups is separate from backup policy's retention and can be specified to a particular date-time. If not specified, it's assumed to be 30 days from the day of the trigger of on-demand backup.
Triggering an on-demand backup is a POST operation.
HTTP
POST https://management.azure.com/Subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.RecoveryServices/vaults/{vaultName}/backupFabrics/{fabricName}/protectionContainers/{containerName}/protectedItems/{protectedItemName}/backup?api-version=2016-12-01
The {containerName} and {protectedItemName} are as constructed above. The {fabricName} is "Azure". For our example, this translates to:
HTTP
POST https://management.azure.com/Subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testVaultRG/providers/Microsoft.RecoveryServices/vaults/testVault/backupFabrics/Azure/protectionContainers/iaasvmcontainer;iaasvmcontainerv2;testRG;testVM/protectedItems/vm;iaasvmcontainerv2;testRG;testVM/backup?api-version=2016-12-01
Create the request body for on-demand backup
To trigger an on-demand backup, following are the components of the request body.
The following request body defines properties required to trigger a backup for a protected item. If the retention isn't specified, it will be retained for 30 days from the time of trigger of the backup job.
Then track the resulting operation using the location header or Azure-AsyncOperation header with a simple GET command.
HTTP
GET https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testVaultRG/providers/microsoft.recoveryservices/vaults/testVault/backupFabrics/Azure/protectionContainers/iaasvmcontainer;iaasvmcontainerv2;testRG;testVM/protectedItems/vm;testRG;testVM/operationsStatus/a0866047-6fc7-4ac3-ba38-fb0ae8aa550f?api-version=2019-05-13
Once the operation completes, it returns 200 (OK) with the ID of the resulting backup job in the response body.
Modify the backup configuration for a protected Azure VM
Changing the policy of protection
To change the policy with which VM is protected, you can use the same format as enabling protection). Just provide the new policy ID in the request body and submit the request. For example: To change the policy of testVM from 'DefaultPolicy' to 'ProdPolicy', provide the 'ProdPolicy' ID in the request body.
If the Azure VM is already backed up, you can specify the list of disks to be backed up or excluded by changing the policy of protection. Just prepare the request in the same format as excluding disks during enabling protection
Important
The request body above is always the final copy of data disks to be excluded or included. This doesn't add to the previous configuration. For example: If you first update the protection as "exclude data disk 1" and then repeat with "exclude data disk 2", only data disk 2 is excluded in the subsequent backups and data disk 1 will be included. This is always the final list which will be included/excluded in the subsequent backups.
To get the current list of disks which are excluded or included, get the protected item information as mentioned here. The response will provide the list of data disk LUNs and indicates whether they're included or excluded.
Stop protection but retain existing data
To remove protection on a protected VM but retain the data already backed up, remove the policy in the request body and submit the request. Once the association with policy is removed, backups are no longer triggered and no new recovery points are created.
DELETE protection is an asynchronous operation. It means this operation creates another operation that needs to be tracked separately.
It returns two responses: 202 (Accepted) when another operation is created, and 204 (NoContent) when that operation completes.
Name
Type
Description
204 NoContent
NoContent
202 Accepted
Accepted
Important
In order to protect against accidental delete scenarios, there's a soft-delete feature available for Recovery Services vault. If the soft-delete state of the vault is set to enabled, then the delete operation won't immediately delete the data. It will be kept for 14 days and then permanently purged. You aren't charged for storage for this 14 days period. To undo the deletion operation, refer to the undo-delete section.
Undo the deletion
Undoing the accidental deletion is similar to creating the backup item. After you undo the deletion, the item is retained but no future backups are triggered.
Undo deletion is a PUT operation which is very similar to changing the policy and/or enabling the protection. Just provide the intent to undo the deletion with the variable isRehydrate in the request body and submit the request. For example: To undo the deletion for testVM, the following request body should be used.