使用 PowerShell 备份 Azure VMBack up Azure VMs with PowerShell

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

本教程介绍如何使用 PowerShell 部署 Azure 备份以备份多个 Azure VM。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
  • 对保护的虚拟机触发按需备份作业 在备份(或保护)虚拟机之前,必须完成先决条件中的步骤来准备好保护 VM 的环境。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.

重要

本教程假定已创建资源组和 Azure 虚拟机。This tutorial assumes you've already created a resource group and an Azure virtual machine.

登录和注册Sign in and register

  1. 运行 Connect-AzAccount -Environment AzureChinaCloud 命令以登录 Azure 订阅,并按照屏幕上的说明操作。Sign in to your Azure subscription with the Connect-AzAccount -Environment AzureChinaCloud command and follow the on-screen directions.

    Connect-AzAccount -Environment AzureChinaCloud
    
  2. 首次使用 Azure 备份时,必须使用 Register-AzResourceProvider 在订阅中注册 Azure 恢复服务提供程序。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

恢复服务保管库是一个逻辑容器,用于存储受保护资源(例如 Azure VM)的备份数据。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.

  • 在本教程中,会在与要备份的 VM 相同的资源组和位置中创建保管库。In this tutorial, you create the vault in the same resource group and location as the VM you want to back up.
  • Azure 备份会自动处理备份数据的存储。Azure Backup automatically handles storage for backed up data. 默认情况下,保管库使用异地冗余存储 (GRS)By default the vault uses Geo-Redundant Storage (GRS). 异地冗余可确保将备份数据复制到距主区域数百英里以外的辅助 Azure 区域。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:

  1. 使用 New-AzRecoveryServicesVault 创建保管库。Use the New-AzRecoveryServicesVaultto create the vault. 指定要备份的 VM 的资源组名称和位置。Specify the resource group name and location of the VM you want to back up.

    New-AzRecoveryServicesVault -Name myRSvault -ResourceGroupName "myResourceGroup" -Location "ChinaNorth"
    
  2. 许多 Azure 备份 cmdlet 要求使用恢复服务保管库对象作为输入。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
    
  3. 使用 Set-AzRecoveryServicesVaultContext 设置保管库上下文。Set the vault context with Set-AzRecoveryServicesVaultContext.

    • 保管库上下文是在保管库中受保护的数据的类型。The vault context is the type of data protected in the vault.

    • 设置上下文后,它将应用于所有后续 cmdletOnce the context is set, it applies to all subsequent cmdlets

      Get-AzRecoveryServicesVault -Name "myRSVault" | Set-AzRecoveryServicesVaultContext
      

备份 Azure VMBack 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.
  • 默认保留策略将每日恢复点保留 30 天。The default retention policy retains the daily recovery point for 30 days.

为了在本教程中启用和备份 Azure VM,我们执行以下操作:To enable and backup up the Azure VM in this tutorial, we do the following:

  1. 使用 Get-AzRecoveryServicesBackupContainer 指定保管库中保存备份数据的容器。Specify a container in the vault that holds your backup data with Get-AzRecoveryServicesBackupContainer.
  2. 进行备份的每个 VM 都是一个项目。Each VM for backup is an item. 若要启动备份作业,请使用 Get-AzRecoveryServicesBackupItem 获取有关 VM 的信息。To start a backup job, you obtain information about the VM with Get-AzRecoveryServicesBackupItem.
  3. 使用 Backup-AzRecoveryServicesBackupItem 运行按需备份。Run an on-demand backup withBackup-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