Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

How to protect a virtual machine using Azure Backup

·906 words·5 mins· 100 views · 5 likes ·
Azure CLI Azure PowerShell Azure Virtual Desktop Backup-AzRecoveryServicesBackupItem

Hi everyone, today I want to show you how to enable backups on an existing VM using Azure command-line tools, and then we will check that it works correctly by doing a quick backup.

Prerequisites

  • You already created the necessary Recovery Services vault. If you want to know how to create one of them, check out this link.
Azure PowerShell Workaround>

Azure PowerShell Workaround #

Check out this link if you want to know how to install the PowerShell Azure module on your machine. The simplest way to get started is to sign in interactively at the command line.

Connect-AzAccount

This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account. You can choose the default subscription if you have more than one associated with your mail account. To perform this task, we will use the following commands:

Get-AzSubscription
Select-AzSubscription -Subscription "My Subscription"

Once you set your default subscription, you’re ready to start.

Set the variables>

Set the variables #

Here, we define the characteristics of our environment and the resource’s properties.

$resourceGroupName="RG-DEMO-WE"
$location="westeurope"
$recoveryServicesVaultName="RVS-DEMO-WE"
$vmName="VM-DEMO-WE"
Set the backup policy>

Set the backup policy #

First, you need to select the backup policy that you will use. The policy defines when the backups are executed and how long the created recovery points are kept. I will use the default policy in this article, but you will most likely use a custom policy in a production environment. To do this, you should use the Get-AzRecoveryServicesBackupProtectionPolicy cmdlet with the following syntax, and the result is stored in the $policy variable to be used in the next step.

 $policy = Get-AzRecoveryServicesBackupProtectionPolicy `
    -Name "DefaultPolicy" 
Enable backup on the VM>

Enable backup on the VM #

To enable the backup of your Azure VM, you should use the Enable-AzRecoveryServicesBackupProtection cmdlet with the following syntax; the policy selected in the previous step is used here.

Enable-AzRecoveryServicesBackupProtection `
    -Name $vmName `
    -ResourceGroupName $resourceGroupName `
    -Policy $policy
Start the backup job>

Start the backup job #

Once the backup is enabled on the Azure VM, it will run according to the backup policy, but you can also run a backup on demand, and that’s what we’ll do in this step. You should first get the container in the service vault that stores your backup data using the Get-AzRecoveryServicesBackupContainer cmdlet and store the result in the $backupcontainer variable that will be used later.

$backupcontainer = Get-AzRecoveryServicesBackupContainer `
    -ContainerType "AzureVM" `
    -FriendlyName $vmName

Then get information about the item that will be backed up, in this case, the virtual machine, with the Get-AzRecoveryServicesBackupItem cmdlet and store the result in the $backupitem variable that will be used later.

$bacukpItem = Get-AzRecoveryServicesBackupItem `
    -Container $backupcontainer `
    -WorkloadType "AzureVM"

And finally, with the Backup-AzRecoveryServicesBackupItem cmdlet, start the backup.

Backup-AzRecoveryServicesBackupItem -Item $backupItem
View the status of backups>

View the status of backups #

To view the status of the backup job, you should use the Get-AzRecoveryservicesBackupJob cmdlet.

Get-AzRecoveryServicesBackupJob -Status InProgress

Get-AzRecoveryServicesBackupJob

Disable backup protection on the VM>

Disable backup protection on the VM #

Suppose you want to disable the backup protection of your virtual machine. In that case, you should use the Disable-AzRecoveryServicesBackupProtection cmdlet, this command stops the scheduled backups, and if executed with the RemoveRecoveryPoints parameter, it also deletes the existing recovery points.

$backupcontainer = Get-AzRecoveryServicesBackupContainer `
    -ContainerType "AzureVM" `
    -FriendlyName $vmName

$backupItem = Get-AzRecoveryServicesBackupItem `
    -Container $backupcontainer `
    -WorkloadType "AzureVM"

Disable-AzRecoveryServicesBackupProtection `
    -Item $backupItem `
    -RemoveRecoveryPoints
Azure CLI Workaround>

Azure CLI Workaround #

In this case, we will use Azure Cloud Shell, a browser-based shell built into Azure Portal. This allows us to use the Azure command-line tools (Azure CLI and Azure PowerShell) directly from a browser. If you want to know more about Azure Cloud Shell, check out this link. First, we define the characteristics of our environment and store the values in variables.

resourceGroupName="RG-DEMO-WE"
location="westeurope"
recoveryServicesVaultName="RVS-DEMO-WE"
vmName="VM-DEMO-WE"
Enable backup on the VM>

Enable backup on the VM #

To enable the backup of your Azure VM, you should use the following command, select the backup policy in the –policy-name parameter.

az backup protection enable-for-vm \
--vm $vmName \
--resource-group $resourceGroupName \
--vault-name $recoveryServicesVaultName \
--policy-name DefaultPolicy
Start a backup job>

Start a backup job #

Once the backup protection is enabled on the Azure VM, it will run according to the backup policy, but you can also run an on-demand backup using the following command.

az backup protection backup-now \
--resource-group $resourceGroupName \
--vault-name $recoveryServicesVaultName \
--container-name $vmName \
--item-name $vmName \
--backup-management-type AzureIaaSVM
View the status of backups>

View the status of backups #

To view the status of the backup job, you should use the following command.

az backup job list \
--resource-group $resourceGroupName \
--vault-name $recoveryServicesVaultName \
--status InProgress \
--output table

azure vm backup

Disable backup protection on the VM>

Disable backup protection on the VM #

If you want to disable the backup protection of your virtual machine, you should use the following command. Note that the --delete-backup-data parameter deletes the backup data in the recovery services vault.

az backup protection disable \
--resource-group $resourceGroupName \
--vault-name $recoveryServicesVaultName \
--container-name $vmName \
--item-name $vmName \
--backup-management-type AzureIaaSVM \
--delete-backup-data

Thanks for reading my post. I hope you find it helpful. In the following article, I’ll show you how to restore a VM from a backup using Azure PowerShell and CLI.

Check out this link if you want to know more about Azure Backup.