使用 PowerShell 在 Azure 中备份虚拟机Back up a virtual machine in Azure with PowerShell

Azure PowerShell AZ 模块用于从命令行或脚本创建和管理 Azure 资源。The Azure PowerShell AZ module is used to create and manage Azure resources from the command line or in scripts.

Azure 备份可备份本地计算机和应用以及 Azure VM。Azure Backup backs up on-premises machines and apps, and Azure VMs. 本文说明如何使用 AZ 模块备份 Azure VM。This article shows you how to back up an Azure VM with the AZ module. 或者,可以使用 Azure CLI 或在 Azure 门户中备份 VM。Alternatively, you can back up a VM using the Azure CLI, or in the Azure portal.

参考本快速入门可在现有的 Azure VM 上备份。This quick start enables backup on an existing Azure VM. 如果需要创建 VM,可以使用 Azure PowerShell 创建 VMIf you need to create a VM, you can create a VM with Azure PowerShell.

本快速入门需要 Azure PowerShell AZ 模块 1.0.0 版或更高版本。This quickstart requires the Azure PowerShell AZ module version 1.0.0 or later. 运行 Get-Module -ListAvailable Az 即可查找版本。Run Get-Module -ListAvailable Az to find the version. 如果需要进行安装或升级,请参阅安装 Azure PowerShell 模块If you need to install or upgrade, see Install Azure PowerShell module.

Note

本文进行了更新,以便使用新的 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.

登录和注册Sign in and register

  1. 使用 Connect-AzAccount 命令登录到 Azure 订阅,并按照屏幕上的说明进行操作。Log in to your Azure subscription with the Connect-AzAccount 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, as follows:

    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.

创建保管库时:When you create the vault:

  • 对于资源组和位置,指定要备份的 VM 的资源组和位置。For the resource group and location, specify the resource group and location of the VM you want to back up.
  • 如果使用此示例脚本创建了 VM,则资源组为 myResourceGroup,VM 为 *myVM,资源位于 WestEurope 区域中。If you used this sample script to create the VM, the resource group is myResourceGroup, the VM is *myVM, and the resources are in the WestEurope region.
  • 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.

现在创建保管库:Now create a vault:

  1. 使用 New-AzRecoveryServicesVault 创建保管库:Use the New-AzRecoveryServicesVault to create the vault:

    New-AzRecoveryServicesVault `
        -ResourceGroupName "myResourceGroup" `
        -Name "myRecoveryServicesVault" `
    -Location "chinanorth"
    
  2. 使用 Set-AzRecoveryServicesVaultContext 设置保管库上下文,如下所示:Set the vault context with Set-AzRecoveryServicesVaultContext, as follows:

    Get-AzRecoveryServicesVault `
        -Name "myRecoveryServicesVault" | Set-AzRecoveryServicesVaultContext
    
  3. 使用 Set-AzRecoveryServicesBackupProperties 更改保管库的存储冗余性配置 (LRS/GRS),如下所示:Change the storage redundancy configuration (LRS/GRS) of the vault with Set-AzRecoveryServicesBackupProperties, as follows:

    Get-AzRecoveryServicesVault `
        -Name "myRecoveryServicesVault" | Set-AzRecoveryServicesBackupProperties -BackupStorageRedundancy LocallyRedundant/GeoRedundant
    

    Note

    只有在没有受保管库保护的备份项的情况下,才能修改存储冗余性。Storage Redundancy can be modified only if there are no backup items protected to the vault.

为 Azure VM 启用备份Enable backup for an Azure VM

为 Azure VM 启用备份,并指定备份策略。You enable backup for an Azure VM, and specify a backup policy.

  • 策略定义备份运行的时间,以及备份创建恢复点应保留多长时间。The policy defines when backups run, and how long recovery points created by the backups should be retained.
  • 默认保护策略为 VM 一天运行一次备份,并将创建的恢复点保留 30 天。The default protection policy runs a backup once a day for the VM, and retains the created recovery points for 30 days. 可以使用此默认策略快速保护 VM。You can use this default policy to quickly protect your VM.

按如下所述启用备份:Enable backup as follows:

  1. 首先,使用 Get-AzRecoveryServicesBackupProtectionPolicy 设置默认策略:First, set the default policy with [Get-AzRecoveryServicesBackupProtectionPolicy:

    $policy = Get-AzRecoveryServicesBackupProtectionPolicy     -Name "DefaultPolicy"
    
  2. 使用 Enable-AzRecoveryServicesBackupProtection 启用 VM 备份。Enable VM backup with Enable-AzRecoveryServicesBackupProtection. 指定策略、资源组和 VM 名称。Specify the policy, the resource group, and the VM name.

    Enable-AzRecoveryServicesBackupProtection `
        -ResourceGroupName "myResourceGroup" `
        -Name "myVM" `
        -Policy $policy
    

启动备份作业Start a backup job

备份会根据备份策略中指定的计划来运行。Backups run in accordance with the schedule specified in the backup policy. 还可以运行临时备份:You can also run an ad hoc backup:

  • 第一个初始备份作业会创建一个完整恢复点。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.

若要运行临时备份,可使用 Backup-AzRecoveryServicesBackupItemTo run an ad hoc backup, you use the Backup-AzRecoveryServicesBackupItem.

运行临时备份作业,如下所示:Run an ad hoc backup job as follows:

  1. 指定容器,获取 VM 信息,并运行备份。Specify the container, obtain VM information, and run the backup.

    $backupcontainer = Get-AzRecoveryServicesBackupContainer `
        -ContainerType "AzureVM" `
        -FriendlyName "myVM"
    
    $item = Get-AzRecoveryServicesBackupItem `
        -Container $backupcontainer `
        -WorkloadType "AzureVM"
    
    Backup-AzRecoveryServicesBackupItem -Item $item
    
  2. 由于第一个备份作业会创建完整恢复点,因此可能需要等待多达 20 分钟。You might need to wait up to 20 minutes, since the first backup job creates a full recovery point. 如下一个过程所述来监视作业。Monitor the job as described in the next procedure.

监视备份作业Monitor the backup job

  1. 运行 Get-AzRecoveryservicesBackupJob 以监视作业状态。Run Get-AzRecoveryservicesBackupJob to monitor the job status.

    Get-AzRecoveryservicesBackupJob
    

    输出类似于以下示例,该示例显示作业处于“正在进行”状态:Output is similar to the following example, which shows the job as InProgress:

    WorkloadName   Operation         Status       StartTime              EndTime                JobID
    ------------   ---------         ------       ---------              -------                -----
    myvm           Backup            InProgress   9/18/2017 9:38:02 PM                          9f9e8f14
    myvm           ConfigureBackup   Completed    9/18/2017 9:33:18 PM   9/18/2017 9:33:51 PM   fe79c739
    
  2. 当作业状态是“已完成”时,VM 受保护并存储了完整恢复点。When the job status is Completed, the VM is protected and has a full recovery point stored.

清理部署Clean up the deployment

如果不再需要备份 VM,则可以清理它。If you no longer need to back up the VM, you can clean it up.

  • 如果要尝试还原 VM,请跳过清理。If you want to try out restoring the VM, skip the clean up.
  • 如果使用了现有的 VM,可以跳过最后一个 Remove-AzResourceGroup cmdlet,以保留资源组和 VM。If you used an existing VM, you can skip the final Remove-AzResourceGroup cmdlet to leave the resource group and VM in place.

禁用保护,删除还原点和保管库。Disable protection, remove the restore points and vault. 然后删除资源组和关联 VM 资源,如下所示:Then delete the resource group and associated VM resources, as follows:

Disable-AzRecoveryServicesBackupProtection -Item $item -RemoveRecoveryPoints
$vault = Get-AzRecoveryServicesVault -Name "myRecoveryServicesVault"
Remove-AzRecoveryServicesVault -Vault $vault
Remove-AzResourceGroup -Name "myResourceGroup"

后续步骤Next steps

本快速入门介绍了如何创建恢复服务保管库,如何在 VM 上启用保护,以及如何创建初始恢复点。In this quickstart, you created a Recovery Services vault, enabled protection on a VM, and created the initial recovery point.