在 Azure 中將檔案還原到虛擬機器Restore files to a virtual machine 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. 本文詳細說明如何還原個別檔案。This article details how to restore individual files. 在本教學課程中,您將了解如何:In this tutorial you learn how to:

  • 列出和選取復原點List and select recovery points
  • 將復原點連線到 VMConnect a recovery point to a VM
  • 從復原點還原檔案Restore files from a recovery point

開啟 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:

選項Option 範例/連結Example/Link
選取程式碼區塊右上角的 [試試看] 。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 的試試看範例
在瀏覽器中開啟 Azure Cloud ShellOpen Azure Cloud Shell in your browser. <a href="https://shell.azure.com" title="啟動 Azure Cloud Shell
選取 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. 若要模擬意外刪除檔案和復原程序,您可以從網頁伺服器刪除頁面。To simulate an accidental file deletion and recovery process, you delete a page from a web server. 如果您需要執行網頁伺服器並已使用 Azure 備份保護的 Linux VM,請參閱使用 CLI 在 Azure 中備份虛擬機器If you need a Linux VM that runs a webserver and 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.

從 VM 刪除檔案Delete a file from a VM

如果您意外刪除或變更檔案,您可以從復原點還原個別檔案。If you accidentally delete or make changes to a file, you can restore individual files from a recovery point. 此程序可讓您瀏覽復原點中備份的檔案,只還原所需的檔案。This process allows you to browse the files backed up in a recovery point and restore only the files you need. 在此範例中,我們會從網頁伺服器刪除檔案,以示範檔案層級復原程序。In this example, we delete a file from a web server to demonstrate the file-level recovery process.

  1. 若要連線到您的 VM,請使用 az vm show 取得 VM 的 IP 位址:To connect to your VM, obtain the IP address of your VM with az vm show:

    az vm show --resource-group myResourceGroup --name myVM -d --query [publicIps] --o tsv
    
  2. 若要確認您的網站目前運作正常,請將網頁瀏覽器開啟到 VM 的公用 IP 位址。To confirm that your web site currently works, open a web browser to the public IP address of your VM. 保持開啟網頁瀏覽器視窗。Leave the web browser window open.

    預設 NGINX 網頁

  3. 透過 SSH 連線到您的 VM。Connect to your VM with SSH. 以您在上一個命令中取得的公用 IP 位址取代 publicIpAddressReplace publicIpAddress with the public IP address that you obtained in a previous command:

    ssh publicIpAddress
    
  4. 從網頁伺服器刪除預設頁面 (/var/www/html/index.nginx-debian.html),如下所示:Delete the default page from the web server at /var/www/html/index.nginx-debian.html as follows:

    sudo rm /var/www/html/index.nginx-debian.html
    
  5. 在您的網頁瀏覽器中,重新整理網頁。In your web browser, refresh the web page. 網站不會再載入頁面,如下列範例所示:The web site no longer loads the page, as shown in the following example:

    NGINX 網站不會再載入預設頁面

  6. 關閉 VM 的 SSH 工作階段,如下所示:Close the SSH session to your VM as follows:

    exit
    

產生檔案復原指令碼Generate file recovery script

為了還原您的檔案,Azure 備份會提供指令碼,以在作為本機磁碟區連線到復原點的 VM 上執行。To restore your files, Azure Backup provides a script to run on your VM that connects your recovery point as a local drive. 您可以瀏覽此本機磁碟機、將檔案還原到 VM 本身,然後中斷復原點連線。You can browse this local drive, restore files to the VM itself, then disconnect the recovery point. Azure 備份會根據排程和保留的指派原則,繼續備份您的資料。Azure Backup continues to back up your data based on the assigned policy for schedule and retention.

  1. 若要列出您 VM 的復原點,請使用 az backup recoverypoint listTo list recovery points for your VM, use az backup recoverypoint list. 在此範例中,我們為 myRecoveryServicesVault 中受保護的 VM (名為 myVM) 選取最近的復原點:In this example, we select the most recent recovery point for the VM named myVM that is protected in myRecoveryServicesVault:

    az backup recoverypoint list \
        --resource-group myResourceGroup \
        --vault-name myRecoveryServicesVault \
        --container-name myVM \
        --item-name myVM \
        --query [0].name \
        --output tsv
    
  2. 若要取得將復原點連線或掛接到 VM 的指令碼,請使用 az backup restore files mount-rpTo obtain the script that connects, or mounts, the recovery point to your VM, use az backup restore files mount-rp. 下列範例會取得 myRecoveryServicesVault 中受保護之 VM (名為 myVM) 的指令碼。The following example obtains the script for the VM named myVM that is protected in myRecoveryServicesVault.

    以您在上一個命令中取得的復原點名稱取代 myRecoveryPointNameReplace myRecoveryPointName with the name of the recovery point that you obtained in the preceding command:

    az backup restore files mount-rp \
        --resource-group myResourceGroup \
        --vault-name myRecoveryServicesVault \
        --container-name myVM \
        --item-name myVM \
        --rp-name myRecoveryPointName
    

    這會下載指令碼並顯示密碼,如下列範例所示:The script is downloaded and a password is displayed, as in the following example:

    File downloaded: myVM_we_1571974050985163527.sh. Use password c068a041ce12465
    
  3. 若要將指令碼傳送至您的 VM,請使用安全複製 (SCP)。To transfer the script to your VM, use Secure Copy (SCP). 提供已下載指令碼的名稱,並以您 VM 的公用 IP 位址取代 publicIpAddressProvide the name of your downloaded script, and replace publicIpAddress with the public IP address of your VM. 確定您在 SCP 命令結尾包含尾端 :,如下所示:Make sure you include the trailing : at the end of the SCP command as follows:

    scp myVM_we_1571974050985163527.sh 52.174.241.110:
    

將檔案還原到您的 VMRestore file to your VM

將復原指令碼複製到您的 VM 之後,您現在可以連線到復原點並還原檔案。With the recovery script copied to your VM, you can now connect the recovery point and restore files.

  1. 透過 SSH 連線到您的 VM。Connect to your VM with SSH. 以您 VM 的公用 IP 位址取代 publicIpAddress,如下所示:Replace publicIpAddress with the public IP address of your VM as follows:

    ssh publicIpAddress
    
  2. 為了讓您的指令碼正確執行,請使用 chmod 新增執行權限。To allow your script to run correctly, add execute permissions with chmod. 輸入您自己的指令碼名稱:Enter the name of your own script:

    chmod +x myVM_we_1571974050985163527.sh
    
  3. 若要掛接復原點,請執行指令碼。To mount the recovery point, run the script. 輸入您自己的指令碼名稱:Enter the name of your own script:

    ./myVM_we_1571974050985163527.sh
    

    當指令碼執行時,系統會提示您輸入密碼以存取復原點。As the script runs, you are prompted to enter a password to access the recovery point. 輸入透過上一個 az backup restore files mount-rp 命令產生復原指令碼之輸出中所顯示的密碼。Enter the password shown in the output from the previous az backup restore files mount-rp command that generated the recovery script.

    指令碼的輸出會提供復原點的路徑。The output from the script gives you the path for the recovery point. 下列範例輸出顯示復原點掛接在 /home/azureuser/myVM-20170919213536/Volume1The following example output shows that the recovery point is mounted at /home/azureuser/myVM-20170919213536/Volume1:

    Microsoft Azure VM Backup - File Recovery
    ______________________________________________
    Please enter the password as shown on the portal to securely connect to the recovery point. : c068a041ce12465
    
    Connecting to recovery point using ISCSI service...
    
    Connection succeeded!
    
    Please wait while we attach volumes of the recovery point to this machine...
    
    ************ Volumes of the recovery point and their mount paths on this machine ************
    
    Sr.No.  |  Disk  |  Volume  |  MountPath
    
    1)  | /dev/sdc  |  /dev/sdc1  |  /home/azureuser/myVM-20170919213536/Volume1
    
    ************ Open File Explorer to browse for files. ************
    
  4. 使用 cp 將 NGINX 預設網頁從已掛接的復原點複製回到原始檔案位置。Use cp to copy the NGINX default web page from the mounted recovery point back to the original file location. 以您自己的位置取代 /home/azureuser/myVM-20170919213536/Volume1 掛接點:Replace the /home/azureuser/myVM-20170919213536/Volume1 mount point with your own location:

    sudo cp /home/azureuser/myVM-20170919213536/Volume1/var/www/html/index.nginx-debian.html /var/www/html/
    
  5. 在您的網頁瀏覽器中,重新整理網頁。In your web browser, refresh the web page. 網站現在會正確載入,如下列範例所示:The web site now loads correctly again, as shown in the following example:

    NGINX 網站現在會正確載入

  6. 關閉 VM 的 SSH 工作階段,如下所示:Close the SSH session to your VM as follows:

    exit
    
  7. 使用 az backup restore files unmount-rp 從您的 VM 卸載復原點。Unmount the recovery point from your VM with az backup restore files unmount-rp. 下列範例會從 myRecoveryServicesVault 中名為 myVM 的 VM 卸載復原點。The following example unmounts the recovery point from the VM named myVM in myRecoveryServicesVault.

    以您在先前命令中取得的復原點名稱取代 myRecoveryPointNameReplace myRecoveryPointName with the name of your recovery point that you obtained in the previous commands:

    az backup restore files unmount-rp \
        --resource-group myResourceGroup \
        --vault-name myRecoveryServicesVault \
        --container-name myVM \
        --item-name myVM \
        --rp-name myRecoveryPointName
    

後續步驟Next steps

在本教學課程中,您已將復原點連線到 VM 和網頁伺服器的已還原檔案。In this tutorial, you connected a recovery point to a VM and restored files for a web server. 您已了解如何︰You learned how to:

  • 列出和選取復原點List and select recovery points
  • 將復原點連線到 VMConnect a recovery point to a VM
  • 從復原點還原檔案Restore files from a recovery point

前進到下一個教學課程,以了解如何將 Windows Server 備份到 Azure。Advance to the next tutorial to learn about how to back up Windows Server to Azure.