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

Azure CLI 用于从命令行或脚本创建和管理 Azure 资源。The Azure CLI is used to create and manage Azure resources from the command line or in scripts. 可以通过定期创建备份来保护数据。You can protect your data by taking backups at regular intervals. Azure 备份可创建恢复点,这些恢复点可存储在异地冗余的恢复保管库中。Azure Backup creates recovery points that can be stored in geo-redundant recovery vaults. 本文详细介绍如何使用 Azure CLI 在 Azure 中备份虚拟机 (VM)。This article details how to back up a virtual machine (VM) in Azure with the Azure CLI. 也可以使用 Azure PowerShellAzure 门户执行这些步骤。You can also perform these steps with Azure PowerShell or in the Azure portal.

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

若要在本地安装和使用 CLI,必须运行 Azure CLI 2.0.18 或更高版本。To install and use the CLI locally, you must run Azure CLI version 2.0.18 or later. 若要查找 CLI 版本,请运行 az --versionTo find the CLI version, run az --version. 如需进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

创建恢复服务保管库Create a recovery services vault

恢复服务保管库是一个逻辑容器,用于存储每个受保护资源(例如 Azure VM)的备份数据。A Recovery Services vault is a logical container that stores the backup data for each protected resource, such as Azure VMs. 运行受保护资源的备份作业时,该作业会在恢复服务保管库中创建一个恢复点。When the backup job for a protected resource 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.

使用 az backup vault create 创建恢复服务保管库。Create a Recovery Services vault with az backup vault create. 指定与想要保护的 VM 相同的资源组和位置。Specify the same resource group and location as the VM you wish to protect. 如果使用过 VM 快速入门,则已创建:If you used the VM quickstart, then you created:

  • 名为 myResourceGroup 的资源组;a resource group named myResourceGroup,
  • 名为 myVM 的 VM;a VM named myVM,
  • chinanorth 位置的资源。resources in the chinanorth location.
az backup vault create --resource-group myResourceGroup \
    --name myRecoveryServicesVault \
    --location chinanorth

默认情况下,恢复服务保管库是针对异地冗余存储设置的。By default, the Recovery Services vault is set for Geo-Redundant storage. 异地冗余存储可确保将备份数据复制到距主区域数百英里以外的辅助 Azure 区域。Geo-Redundant storage ensures your backup data is replicated to a secondary Azure region that is hundreds of miles away from the primary region. 如果存储冗余设置需要修改,请使用 az backup vault backup-properties set cmdlet。If the storage redundancy setting needs to be modified, use az backup vault backup-properties set cmdlet.

az backup vault backup-properties set \
    --name myRecoveryServicesVault  \
    --resource-group myResourceGroup \
    --backup-storage-redundancy "LocallyRedundant/GeoRedundant" 

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

创建一个保护策略,用于定义:备份作业的运行时间以及恢复点的存储期限。Create a protection policy to define: when a backup job runs, and how long the recovery points are stored. 默认保护策略每天运行一个备份作业,并将恢复点保留 30 天。The default protection policy runs a backup job each day and retains recovery points for 30 days. 可以使用这些默认策略值来快速保护 VM。You can use these default policy values to quickly protect your VM. 若要为 VM 启用备份保护,请使用 az backup protection enable-for-vmTo enable backup protection for a VM, use az backup protection enable-for-vm. 指定要保护的资源组和 VM,再指定要使用的策略:Specify the resource group and VM to protect, then the policy to use:

az backup protection enable-for-vm \
    --resource-group myResourceGroup \
    --vault-name myRecoveryServicesVault \
    --vm myVM \
    --policy-name DefaultPolicy

Note

如果 VM 与保管库不在同一个资源组中,则 myResourceGroup 引用创建保管库所在的资源组。If the VM is not in the same resource group as that of vault, then myResourceGroup refers to the resource group where vault was created. 如下所示,请提供 VM ID 而不是 VM 名称。Instead of VM name, provide the VM ID as indicated below.

az backup protection enable-for-vm \
    --resource-group myResourceGroup \
    --vault-name myRecoveryServicesVault \
    --vm $(az vm show -g VMResourceGroup -n MyVm --query id | tr -d '"') \
    --policy-name DefaultPolicy

启动备份作业Start a backup job

若要立即开始备份而不是等待默认策略根据计划的时间运行作业,请使用 az backup protection backup-nowTo start a backup now rather than wait for the default policy to run the job at the scheduled time, use az backup protection backup-now. 第一个备份作业会创建一个完整恢复点。This first backup job creates a full recovery point. 此初始备份后的每个备份作业会创建增量恢复点。Each backup job after this initial backup creates incremental recovery points. 增量恢复点有利于存储并具有时效性,因为它们仅传输自上次备份以来所做的更改。Incremental recovery points are storage and time-efficient, as they only transfer changes made since the last backup.

以下参数用于备份 VM:The following parameters are used to back up the VM:

  • --container-name 是 VM 的名称--container-name is the name of your VM
  • --item-name 是 VM 的名称--item-name is the name of your VM
  • 应以 UTC 时间格式 (dd-mm-yyyy) 将 --retain-until 值设置为希望恢复点可用的最后一个日期--retain-until value should be set to the last available date, in UTC time format (dd-mm-yyyy), that you wish the recovery point to be available

以下示例备份名为 myVM 的 VM,并将恢复点的过期时间设置为 2017 年 10 月 18 日:The following example backs up the VM named myVM and sets the expiration of the recovery point to October 18, 2017:

az backup protection backup-now \
    --resource-group myResourceGroup \
    --vault-name myRecoveryServicesVault \
    --container-name myVM \
    --item-name myVM \
    --retain-until 18-10-2017

监视备份作业Monitor the backup job

若要监视备份作业的状态,请使用 az backup job listTo monitor the status of backup jobs, use az backup job list:

az backup job list \
    --resource-group myResourceGroup \
    --vault-name myRecoveryServicesVault \
    --output table

输出如以下示例所示,其中显示备份作业的状态为“正在进行”:The output is similar to the following example, which shows the backup job is InProgress:

Name      Operation        Status      Item Name    Start Time UTC       Duration
--------  ---------------  ----------  -----------  -------------------  --------------
a0a8e5e6  Backup           InProgress  myvm         2017-09-19T03:09:21  0:00:48.718366
fe5d0414  ConfigureBackup  Completed   myvm         2017-09-19T03:03:57  0:00:31.191807

当备份作业的“状态”报告为“已完成”时,表示 VM 已受恢复服务的保护,并且已存储完整的恢复点。When the Status of the backup job reports Completed, your VM is protected with Recovery Services and has a full recovery point stored.

清理部署Clean up deployment

如果不再需要,可以在 VM 上禁用保护,删除还原点和恢复服务保管库,然后删除资源组和关联的 VM 资源。When no longer needed, you can disable protection on the VM, remove the restore points and Recovery Services vault, then delete the resource group and associated VM resources. 如果使用了现有的 VM,可以跳过最后一个 az group delete 命令,以保留资源组和 VM。If you used an existing VM, you can skip the final az group delete command to leave the resource group and VM in place.

若要尝试备份教程,了解如何还原 VM 的数据,请转到后续步骤If you want to try a Backup tutorial that explains how to restore data for your VM, go to Next steps.

az backup protection disable \
    --resource-group myResourceGroup \
    --vault-name myRecoveryServicesVault \
    --container-name myVM \
    --item-name myVM \
    --delete-backup-data true
az backup vault delete \
    --resource-group myResourceGroup \
    --name myRecoveryServicesVault \
az group delete --name myResourceGroup

后续步骤Next steps

在本快速入门中,我们创建了恢复服务保管库,在 VM 上启用了保护,并创建了初始恢复点。In this quick start, you created a Recovery Services vault, enabled protection on a VM, and created the initial recovery point. 若要详细了解 Azure 备份和恢复服务,请继续学习其他教程。To learn more about Azure Backup and Recovery Services, continue to the tutorials.