Back up Azure VMs with PowerShell
Note
We recommend that you use the Azure Az PowerShell module to interact with Azure. To get started, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
This tutorial describes how to deploy an Azure Backup Recovery Services vault to back up multiple Azure VMs using PowerShell.
In this tutorial you learn how to:
- Create a Recovery Services vault and set the vault context.
- Define a backup policy
- Apply the backup policy to protect multiple virtual machines
- Trigger an on-demand backup job for the protected virtual machines Before you can back up (or protect) a virtual machine, you must complete the prerequisites to prepare your environment for protecting your VMs.
Important
This tutorial assumes you've already created a resource group and an Azure virtual machine.
Sign in and register
Sign in to your Azure subscription with the
Connect-AzAccount
command and follow the on-screen directions.Connect-AzAccount
The first time you use Azure Backup, you must register the Azure Recovery Service provider in your subscription with Register-AzResourceProvider. If you've already registered, skip this step.
Register-AzResourceProvider -ProviderNamespace "Microsoft.RecoveryServices"
Create a Recovery Services vault
A Recovery Services vault is a logical container that stores backup data for protected resources, such as Azure VMs. When a backup job runs, it creates a recovery point inside the Recovery Services vault. You can then use one of these recovery points to restore data to a given point in time.
- In this tutorial, you create the vault in the same resource group and location as the VM you want to back up.
- Azure Backup automatically handles storage for backed up data. By default the vault uses Geo-Redundant Storage (GRS). Geo-redundancy ensures that backed up data is replicated to a secondary Azure region, hundreds of miles away from the primary region.
Create the vault as follows:
Use the New-AzRecoveryServicesVault to create the vault. Specify the resource group name and location of the VM you want to back up.
New-AzRecoveryServicesVault -Name myRSvault -ResourceGroupName "myResourceGroup" -Location "EastUS"
Many Azure Backup cmdlets require the Recovery Services vault object as an input. For this reason, it's convenient to store the Backup Recovery Services vault object in a variable.
$vault1 = Get-AzRecoveryServicesVault –Name myRSVault
Set the vault context with Set-AzRecoveryServicesVaultContext.
The vault context is the type of data protected in the vault.
Once the context is set, it applies to all subsequent cmdlets
Get-AzRecoveryServicesVault -Name "myRSVault" | Set-AzRecoveryServicesVaultContext
Back up Azure VMs
Backups run in accordance with the schedule specified in the backup policy. When you create a Recovery Services vault, it comes with default protection and retention policies.
- The default protection policy triggers a backup job once a day at a specified time.
- The default retention policy retains the daily recovery point for 30 days.
To enable and backup up the Azure VM in this tutorial, we do the following:
- Specify a container in the vault that holds your backup data with Get-AzRecoveryServicesBackupContainer.
- Each VM for backup is an item. To start a backup job, you obtain information about the VM with Get-AzRecoveryServicesBackupItem.
- Run an on-demand backup with Backup-AzRecoveryServicesBackupItem.
- The first initial backup job creates a full recovery point.
- After the initial backup, each backup job creates incremental recovery points.
- Incremental recovery points are storage and time-efficient, as they only transfer changes made since the last backup.
Enable and run the backup as follows:
$namedContainer = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -Status Registered -FriendlyName "V2VM"
$item = Get-AzRecoveryServicesBackupItem -Container $namedContainer -WorkloadType AzureVM
$job = Backup-AzRecoveryServicesBackupItem -Item $item
Troubleshooting
If you run into issues while backing up your virtual machine, review this troubleshooting article.
Deleting a Recovery Services vault
If you need to delete a vault, first delete recovery points in the vault, and then unregister the vault, as follows:
$Cont = Get-AzRecoveryServicesBackupContainer -ContainerType AzureVM -Status Registered
$PI = Get-AzRecoveryServicesBackupItem -Container $Cont[0] -WorkloadType AzureVm
Disable-AzRecoveryServicesBackupProtection -RemoveRecoveryPoints $PI[0]
Unregister-AzRecoveryServicesBackupContainer -Container $namedContainer
Remove-AzRecoveryServicesVault -Vault $vault1
Next steps
- Review a more detailed walkthrough of backing up and restoring Azure VMs with PowerShell.
- Manage and monitor Azure VMs
- Restore Azure VMs