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

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.

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:

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

Set the variables

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

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.

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.

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.

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.

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

View the status of backups

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

Get-AzRecoveryServicesBackupJob

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.

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.

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.

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.

View the status of backups

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

azure vm backup

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.

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.