使用 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 PowerShell 或在 Azure 入口網站中執行這些步驟。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.

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. Cloud Shell 可讓您使用 bashPowerShell 以與 Azure 服務搭配使用。Cloud Shell lets you use either bash or PowerShell to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文提到的程式碼,而不必在本機環境上安裝任何工具。You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

若要啟動 Azure Cloud Shell:To launch Azure Cloud Shell:

選項Option 範例/連結Example/Link
選取程式碼區塊右上角的 [試試看] 。Select Try It in the upper-right corner of a code block. 選取 [試用] 並不會自動將程式碼複製到 Cloud Shell 中。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的試試看範例
請前往 https://shell.azure.com 或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. 在新視窗中啟動 Cloud ShellLaunch Cloud Shell in a new window
選取 Azure 入口網站右上角功能表列中的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

若要在 Azure Cloud Shell 中執行本文中的程式碼:To run the code in this article in Azure Cloud Shell:

  1. 啟動 Cloud Shell。Launch Cloud Shell.
  2. 選取程式碼區塊上的 [複製] 按鈕,複製程式碼。Select the Copy button on a code block to copy the code.
  3. 在 Windows 和 Linux 上按 Ctrl+Shift+V;或在 macOS 上按 Cmd+Shift+V,將程式碼貼到 Cloud Shell工作階段中。Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.
  4. 按下 Enter 鍵執行程式碼。Press Enter to run the code.

若要在本機安裝和使用 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 的虛擬機器、a VM named myVM,
  • eastus 位置中的資源。resources in the eastus location.
az backup vault create --resource-group myResourceGroup \
    --name myRecoveryServicesVault \
    --location eastus

根據預設,已針對異地備援儲存體設定復原服務保存庫。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

注意

如果 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 名稱,請如下所示提供 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

重要

使用 CLI 同時啟用多個 VM 的備份時,請確定單一原則不會有超過 100 部相關聯的 VM。While using CLI to enable backup for multiple VMs at once, ensure that a single policy doesn't have more than 100 VMs associated with it. 這是建議的最佳做法This is a recommended best practice. 目前,如果有超過 100 部 VM,但已規劃在未來新增檢查,則 PS 用戶端不會明確封鎖。Currently, the PS client doesn't explicitly block if there are more than 100 VMs but the check is planned to be added in the future.

開始備份作業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
  • --retain-until 值應設定為您希望使用復原點的最後可用日期 (採用 UTC 時間格式 dd-mm-yyyy)。--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

輸出會類似下列範例,其顯示的備份作業為 InProgress ︰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

當備份作業的「狀態」 回報 Completed ,您的 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.