在 Azure 中還原磁碟並建立已復原的 VMRestore a disk and create a recovered VM in Azure

Azure 備份會建立復原點,並儲存在異地備援復原保存庫。Azure Backup creates recovery points that are stored in geo-redundant recovery vaults. 當您從復原點還原時,可以還原整個 VM 或個別檔案。When you restore from a recovery point, you can restore the whole VM or individual files. 本文說明如何使用 CLI 還原完整的 VM。This article explains how to restore a complete VM using CLI. 在本教學課程中,您將了解如何:In this tutorial you learn how to:

  • 列出和選取復原點List and select recovery points
  • 從復原點還原磁碟Restore a disk from a recovery point
  • 從還原的磁碟建立 VMCreate a VM from the restored disk

如需使用 PowerShell 還原磁碟及建立已復原 VM 的相關資訊,請參閱使用 PowerShell 備份及還原 Azure VMFor information on using PowerShell to restore a disk and create a recovered VM, see Back up and restore Azure VMs with PowerShell.

開啟 Azure Cloud ShellOpen Azure Cloud Shell

Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,並且會透過瀏覽器來使用。Azure Cloud Shell is an interactive shell environment hosted in Azure and used through your browse. Azure Cloud Shell 可讓您使用 bashPowerShell 殼層來執行各種可與 Azure 服務搭配運作的工具。Azure Cloud Shell allows you to use either bash or PowerShell shells to run a variety of tools to work with Azure services. Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文的內容,而不必在本機環境上安裝任何工具。Azure Cloud Shell comes pre-installed with the commands to allow you to run the content of this article without having to install anything on your local environment.

若要在 Azure Cloud Shell 上執行本文所包含的任何程式碼,請開啟 Cloud Shell 工作階段、使用某個程式碼區塊上的 [複製] 按鈕來複製程式碼,然後使用 Ctrl+Shift+V (在 Windows 和 Linux 上) 或 Cmd+Shift+V (在 macOS 上) 將程式碼貼到 Cloud Shell 工作階段中。To run any code contained in this article on Azure Cloud Shell, open a Cloud Shell session, use the Copy button on a code block to copy the code, and paste it into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS. 貼上的文字不會自動執行,因此請按 Enter 來執行程式碼。Pasted text is not automatically executed, so press Enter to run code.

您可以使用下列方式來啟動 Azure Cloud Shell:You can launch Azure Cloud Shell with:

選取程式碼區塊右上角的 [試試看] 。Select Try It in the upper-right corner of a code block. 這__不會__自動將文字複製到 Cloud Shell。This doesn't automatically copy text to Cloud Shell. Azure Cloud Shell 的試試看範例
在瀏覽器中開啟 shell.azure.comOpen shell.azure.com in your browser. 啟動 Azure Cloud Shell 按鈕Launch Azure Cloud Shell button
選取 Azure 入口網站右上角功能表上的 [Cloud Shell] 按鈕。Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. Azure 入口網站中的 [Cloud Shell] 按鈕

如果您選擇在本機安裝和使用 CLI,本教學課程會要求您執行 Azure CLI 2.0.18 版或更新版本。If you choose to install and use the CLI locally, this tutorial requires that you are running the Azure CLI version 2.0.18 or later. 執行 az --version 以尋找版本。Run az --version to find the version. 如果您需要安裝或升級,請參閱安裝 Azure CLIIf you need to install or upgrade, see Install the Azure CLI.

必要條件Prerequisites

本教學課程需要已使用 Azure 備份保護的 Linux VM。This tutorial requires a Linux VM that has been protected with Azure Backup. 若要模擬意外刪除 VM 和復原程序,您可以在復原點從磁碟建立 VM。To simulate an accidental VM deletion and recovery process, you create a VM from a disk in a recovery point. 如果您需要已使用 Azure 備份保護的 Linux VM,請參閱使用 CLI 在 Azure 中備份虛擬機器If you need a Linux VM that has been protected with Azure Backup, see Back up a virtual machine in Azure with the CLI.

備份概觀Backup overview

當 Azure 起始備份時,VM 上的備份擴充功能會建立時間點快照集。When Azure initiates a backup, the backup extension on the VM takes a point-in-time snapshot. 此備份擴充功能會在要求第一個備份時安裝在 VM 上。The backup extension is installed on the VM when the first backup is requested. 如果進行備份時 VM 不在執行中,Azure 備份也可以建立基礎儲存體的快照集。Azure Backup can also take a snapshot of the underlying storage if the VM is not running when the backup takes place.

根據預設,Azure 備份會建立檔案系統一致的快照集。By default, Azure Backup takes a file system consistent backup. Azure 備份建立快照集之後,資料會傳輸至復原服務保存庫。Once Azure Backup takes the snapshot, the data is transferred to the Recovery Services vault. 為了能更有效率,Azure 備份只會找出並傳輸自上次備份之後有變更的資料區塊。To maximize efficiency, Azure Backup identifies and transfers only the blocks of data that have changed since the previous backup.

資料傳輸完畢後,系統會移除快照集並建立復原點。When the data transfer is complete, the snapshot is removed and a recovery point is created.

列出可用的復原點List available recovery points

若要還原磁碟,您可選取復原點作為復原資料的來源。To restore a disk, you select a recovery point as the source for the recovery data. 由於預設原則會每天建立復原點並保留 30 天,您可保留一組復原點,讓您選取特定的時間點進行復原。As the default policy creates a recovery point each day and retains them for 30 days, you can keep a set of recovery points that allows you to select a particular point in time for recovery.

若要查看可用的復原點清單,請使用 az backup recoverypoint listTo see a list of available recovery points, use az backup recoverypoint list. 復原點名稱用來復原磁碟。The recovery point name is used to recover disks. 在本教學課程中,我們需要最近可用的復原點。In this tutorial, we want the most recent recovery point available. --query [0].name 參數可選取最近的復原點名稱,如下所示:The --query [0].name parameter selects the most recent recovery point name as follows:

az backup recoverypoint list \
    --resource-group myResourceGroup \
    --vault-name myRecoveryServicesVault \
    --container-name myVM \
    --item-name myVM \
    --query [0].name \
    --output tsv

還原 VM 磁碟Restore a VM disk

若要從復原點還原您的磁碟,您可以先建立 Azure 儲存體帳戶。To restore your disk from the recovery point, you first create an Azure storage account. 此儲存體帳戶用來儲存已還原的磁碟。This storage account is used to store the restored disk. 在其他步驟中,已還原的磁碟用來建立 VM。In additional steps, the restored disk is used to create a VM.

  1. 若要建立儲存體帳戶,請使用 az storage account createTo create a storage account, use az storage account create. 儲存體帳戶名稱必須全部小寫,並且是全域唯一的。The storage account name must be all lowercase, and be globally unique. 以自己的唯一名稱取代 mystorageaccountReplace mystorageaccount with your own unique name:

    az storage account create \
        --resource-group myResourceGroup \
        --name mystorageaccount \
        --sku Standard_LRS
    
  2. 使用 az backup restore restore-disks 從您的復原點還原磁碟。Restore the disk from your recovery point with az backup restore restore-disks. 以您在上一個命令中建立的儲存體帳戶名稱取代 mystorageaccountReplace mystorageaccount with the name of the storage account you created in the preceding command. 以您在先前 az backup recoverypoint list 命令的輸出中取得的復原點名稱取代 myRecoveryPointNameReplace myRecoveryPointName with the recovery point name you obtained in the output from the previous az backup recoverypoint list command:

    az backup restore restore-disks \
        --resource-group myResourceGroup \
        --vault-name myRecoveryServicesVault \
        --container-name myVM \
        --item-name myVM \
        --storage-account mystorageaccount \
        --rp-name myRecoveryPointName
    

監視還原作業Monitor the restore job

若要監視還原作業的狀態,請使用 az backup job listTo monitor the status of restore job, 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 restore job is InProgress:

Name      Operation        Status      Item Name    Start Time UTC       Duration
--------  ---------------  ----------  -----------  -------------------  --------------
7f2ad916  Restore          InProgress  myvm         2017-09-19T19:39:52  0:00:34.520850
a0a8e5e6  Backup           Completed   myvm         2017-09-19T03:09:21  0:15:26.155212
fe5d0414  ConfigureBackup  Completed   myvm         2017-09-19T03:03:57  0:00:31.191807

當還原作業的 [狀態] 回報 [已完成] 時,磁碟便已還原到儲存體帳戶。When the Status of the restore job reports Completed, the disk has been restored to the storage account.

將還原的磁碟轉換成受控磁碟Convert the restored disk to a Managed Disk

還原作業會建立非受控磁碟。The restore job creates an unmanaged disk. 若要從磁碟建立 VM,它必須先轉換成受控磁碟。In order to create a VM from the disk, it must first be converted to a managed disk.

  1. 使用 az storage account show-connection-string 取得儲存體帳戶的連接字串。Obtain the connection information for your storage account with az storage account show-connection-string. 以您的儲存體帳戶名稱取代 mystorageaccount,如下所示:Replace mystorageaccount with the name of your storage account as follows:

    export AZURE_STORAGE_CONNECTION_STRING=$( az storage account show-connection-string \
        --resource-group myResourceGroup \
        --output tsv \
        --name mystorageaccount )
    
  2. 您的非受控磁碟會在儲存體帳戶中保護。Your unmanaged disk is secured in the storage account. 下列命令可取得非受控磁碟的相關資訊,並建立名為 uri 的變數,當您建立受控磁碟時,該變數使用於下一個步驟。The following commands get information about your unmanaged disk and create a variable named uri that is used in the next step when you create the Managed Disk.

    container=$(az storage container list --query [0].name -o tsv)
    blob=$(az storage blob list --container-name $container --query [0].name -o tsv)
    uri=$(az storage blob url --container-name $container --name $blob -o tsv)
    
  3. 您現在可以使用 az disk create,從已復原的磁碟建立受控磁碟。Now you can create a Managed Disk from your recovered disk with az disk create. 上一個步驟中的 Uri 變數會作為受控磁碟的來源。The uri variable from the preceding step is used as the source for your Managed Disk.

    az disk create \
        --resource-group myResourceGroup \
        --name myRestoredDisk \
        --source $uri
    
  4. 由於您現在有來自已還原磁碟的受控磁碟,請使用 az storage account delete 清除非受控磁碟和儲存體帳戶。As you now have a Managed Disk from your restored disk, clean up the unmanaged disk and storage account with az storage account delete. 以您的儲存體帳戶名稱取代 mystorageaccount,如下所示:Replace mystorageaccount with the name of your storage account as follows:

    az storage account delete \
        --resource-group myResourceGroup \
        --name mystorageaccount
    

從還原的磁碟建立 VMCreate a VM from the restored disk

最後一個步驟是從受控磁碟建立 VM。The final step is to create a VM from the Managed Disk.

  1. 使用 az vm create 從受控磁碟建立 VM,如下所示:Create a VM from your Managed Disk with az vm create as follows:

    az vm create \
        --resource-group myResourceGroup \
        --name myRestoredVM \
        --attach-os-disk myRestoredDisk \
        --os-type linux
    
  2. 若要確認已從復原的磁碟建立 VM,請使用 az vm list 列出資源群組中的 VM,如下所示:To confirm that your VM has been created from your recovered disk, list the VMs in your resource group with az vm list as follows:

    az vm list --resource-group myResourceGroup --output table
    

後續步驟Next steps

在本教學課程中,您已從復原點還原磁碟,並從磁碟建立 VM。In this tutorial, you restored a disk from a recovery point and then created a VM from the disk. 您已了解如何︰You learned how to:

  • 列出和選取復原點List and select recovery points
  • 從復原點還原磁碟Restore a disk from a recovery point
  • 從還原的磁碟建立 VMCreate a VM from the restored disk

前進到下一個教學課程,了解如何從復原點還原個別的檔案。Advance to the next tutorial to learn about restoring individual files from a recovery point.