教學課程:在 Azure 中備份和還原 Linux 虛擬機器的檔案Tutorial: Back up and restore files for Linux virtual machines in Azure

您可以定期建立備份以保護您的資料。You can protect your data by taking backups at regular intervals. 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 specific files. 本文說明如何將單一檔案還原成執行 nginx 的 Linux VM。This article explains how to restore a single file to a Linux VM running nginx. 如果您還沒有 VM 可以使用,請用 Linux 快速入門中的步驟建立一個。If you don't already have a VM to use, you can create one using the Linux quickstart. 在本教學課程中,您將了解如何:In this tutorial you learn how to:

  • 建立 VM 的備份Create a backup of a VM
  • 排定每日備份Schedule a daily backup
  • 從備份還原檔案Restore a file from a backup

備份概觀Backup overview

Azure 備份服務開始備份時,會觸發備份擴充功能以建立時間點快照集。When the Azure Backup service initiates a backup, it triggers the backup extension to take a point-in-time snapshot. Azure 備份服務在 Linux 中使用 VMSnapshotLinux 擴充功能。The Azure Backup service uses the VMSnapshotLinux extension in Linux. 如果 VM 正在執行,會在第一次 VM 備份期間安裝此擴充功能。The extension is installed during the first VM backup if the VM is running. 如果 VM 未在執行中,則備份服務會擷取基礎儲存體的快照集 (因為 VM 停止時不會發生任何應用程式寫入)。If the VM is not running, the Backup service takes a snapshot of the underlying storage (since no application writes occur while the VM is stopped).

根據預設,Azure 備份會為 Linux VM 建立檔案系統一致備份,但可以將它設定成建立應用程式一致備份 (使用備份前指令碼和備份後指令碼架構)By default, Azure Backup takes a file system consistent backup for Linux VM but it can be configured to take application consistent backup using pre-script and post-script framework. Azure 備份服務擷取快照集之後,資料會傳輸至保存庫。Once the Azure Backup service takes the snapshot, the data is transferred to the vault. 為了能更有效率,服務只會找出並傳輸自上次備份之後有變更的資料區塊。To maximize efficiency, the service 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.

建立備份Create a backup

建立復原服務保存庫的排程每日備份:Create a scheduled daily backup to a Recovery Services Vault:

  1. 登入 Azure 入口網站Sign in to the Azure portal.

  2. 在左邊的功能表上,選取 [虛擬機器] 。In the menu on the left, select Virtual machines.

  3. 從清單中選取要備份的 VM。From the list, select a VM to back up.

  4. 在 VM 刀鋒視窗的 [設定] 區段中,按一下 [備份] 。On the VM blade, in the Settings section, click Backup. [啟用備份] 刀鋒視窗隨即開啟。The Enable backup blade opens.

  5. 在 [復原服務保存庫] 中,按一下 [新建] 並提供新保存庫的名稱。In Recovery Services vault, click Create new and provide the name for the new vault. 新保存庫已在與虛擬機器相同的資源群組和位置中建立。A new vault is created in the same Resource Group and location as the virtual machine.

  6. 按一下 [備份原則] 。Click Backup policy. 在此範例中請保留預設值,按一下 [確定] 。For this example, keep the defaults and click OK.

  7. 在 [啟用備份] 刀鋒視窗上,按一下 [啟用備份] 。On the Enable backup blade, click Enable Backup. 這會根據預設排程建立每日備份。This creates a daily backup based on the default schedule.

  8. 若要建立初始復原點,在 [備份] 刀鋒視窗中按一下 [立即備份] 。To create an initial recovery point, on the Backup blade click Backup now.

  9. 在 [立即備份] 刀鋒視窗上,按一下日曆圖示,使用日曆控制項選取此復原點保留的最後一天,然後按一下 [備份] 。On the Backup Now blade, click the calendar icon, use the calendar control to select the last day this recovery point is retained, and click Backup.

  10. 在 VM 的 [備份] 刀鋒視窗中,您會看到已完成的復原點數目。In the Backup blade for your VM, you see the number of recovery points that are complete.

    復原點

第一次備份大約需要 20 分鐘的時間。The first backup takes about 20 minutes. 在備份完成之後,繼續進行本教學課程的下一個部分。Proceed to the next part of this tutorial after your backup is finished.

還原檔案Restore a file

如果您不小心刪除或變更某個檔案,可以使用「檔案復原」從您的備份保存庫復原檔案。If you accidentally delete or make changes to a file, you can use File Recovery to recover the file from your backup vault. 檔案復原使用在虛擬機器上執行的指令碼,將復原點掛接為本機磁碟機。File Recovery uses a script that runs on the VM, to mount the recovery point as a local drive. 這些磁碟機會保持掛接達 12 小時,讓您可以從復原點複製檔案,並將它們還原至 VM。These drives remain mounted for 12 hours so that you can copy files from the recovery point and restore them to the VM.

在此範例中,我們會示範如何復原預設的 nginx 網頁 /var/www/html/index.nginx-debian.html。In this example, we show how to recover the default nginx web page /var/www/html/index.nginx-debian.html. 我們的 VM 在範例中的公用 IP 位址是 13.69.75.209 。The public IP address of our VM in this example is 13.69.75.209. 您可以使用以下命令找到您的 VM 的 IP 位址︰You can find the IP address of your vm using:

az vm show --resource-group myResourceGroup --name myVM -d --query [publicIps] --o tsv
  1. 在您的本機電腦上開啟瀏覽器,輸入在您的 VM 公用 IP 位址,應該可以看到預設的 nginx 網頁。On your local computer, open a browser and type in the public IP address of your VM to see the default nginx web page.

    預設的 nginx 網頁

  2. 透過 SSH 連線到您的 VM。SSH into your VM.

    ssh 13.69.75.209
    
  3. 刪除 /var/www/html/index.nginx-debian.html。Delete /var/www/html/index.nginx-debian.html.

    sudo rm /var/www/html/index.nginx-debian.html
    
  4. 在您的本機電腦上,按 CTRL + F5 重新整理瀏覽器,應該會看到該預設 nginx 網頁已不存在。On your local computer, refresh the browser by hitting CTRL + F5 to see that default nginx page is gone.

    預設的 nginx 網頁

  5. 在您的本機電腦上,登入 Azure 入口網站On your local computer, sign in to the Azure portal.

  6. 在左邊的功能表上,選取 [虛擬機器] 。In the menu on the left, select Virtual machines.

  7. 從清單中選取 VM。From the list, select the VM.

  8. 在 VM 刀鋒視窗的 [設定] 區段中,按一下 [備份] 。On the VM blade, in the Settings section, click Backup. [備份] 刀鋒視窗隨即開啟。The Backup blade opens.

  9. 在刀鋒視窗頂端的功能表中,選取 [檔案復原] 。In the menu at the top of the blade, select File Recovery. [檔案復原] 刀鋒視窗隨即開啟。The File Recovery blade opens.

  10. 在 [步驟 1:選取復原點] 中,從下拉式清單選取復原點。In Step 1: Select recovery point, select a recovery point from the drop-down.

  11. 步驟 2:下載指令碼以瀏覽及復原檔案中,按一下 [下載可執行檔] 按鈕。In Step 2: Download script to browse and recover files, click the Download Executable button. 將下載的檔案儲存到您的本機電腦。Save the downloaded file to your local computer.

  12. 按一下 [下載指令碼] 將指令碼檔案下載至本機。Click Download script to download the script file locally.

  13. 開啟 Bash 提示字元並輸入下列命令,將其中的 Linux_myVM_05-05-2017.sh 取代為您下載之指令碼的正確路徑和檔名,將 azureuser 取代為 VM 的使用者名稱,將 13.69.75.209 取代為您的 VM 公用 IP 位址。Open a Bash prompt and type the following, replacing Linux_myVM_05-05-2017.sh with the correct path and filename for the script that you downloaded, azureuser with the username for the VM and 13.69.75.209 with the public IP address for your VM.

    scp Linux_myVM_05-05-2017.sh azureuser@13.69.75.209:
    
  14. 在您的本機電腦上,開啟與 VM 的 SSH 連線。On your local computer, open an SSH connection to the VM.

    ssh 13.69.75.209
    
  15. 在您的 VM 上,新增指令碼檔案的執行權限。On your VM, add execute permissions to the script file.

    chmod +x Linux_myVM_05-05-2017.sh
    
  16. 您的 VM 上,執行指令碼將復原點掛接為檔案系統。On your VM, run the script to mount the recovery point as a filesystem.

    ./Linux_myVM_05-05-2017.sh
    
  17. 指令碼的輸出會提供掛接點的路徑。The output from the script gives you the path for the mount point. 輸出大致如下:The output looks similar to this:

    Microsoft Azure VM Backup - File Recovery
    ______________________________________________
    
    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-20170505191055/Volume1
    
    ************ Open File Explorer to browse for files. ************
    
    After recovery, to remove the disks and close the connection to the recovery point, please click 'Unmount Disks' in step 3 of the portal.
    
    Please enter 'q/Q' to exit...
    
  18. 在您的 VM 上,從刪除檔案之際的掛接點複製 nginx 預設網頁。On your VM, copy the nginx default web page from the mount point back to where you deleted the file.

    sudo cp ~/myVM-20170505191055/Volume1/var/www/html/index.nginx-debian.html /var/www/html/
    
  19. 在您的本機電腦上開啟瀏覽器索引標籤,您已連線到顯示 nginx 預設網頁之 VM 的 IP 位址。On your local computer, open the browser tab where you are connected to the IP address of the VM showing the nginx default page. 按 CTRL + F5 重新整理瀏覽器頁面。Press CTRL + F5 to refresh the browser page. 您現在應該會看到預設網頁再次運作。You should now see that the default page is working again.

    預設的 nginx 網頁

  20. 在您的本機電腦上,回到 Azure 入口網站的瀏覽器索引標籤,並在 [步驟 3︰在復原後取消掛接磁碟] 中按一下 [取消掛接磁碟] 按鈕。On your local computer, go back to the browser tab for the Azure portal and in Step 3: Unmount the disks after recovery click the Unmount Disks button. 如果您忘記執行此步驟,與此掛接點的連線會在 12 小時後自動關閉。If you forget to do this step, the connection to the mountpoint is automatically closed after 12 hours. 在這 12 小時後,您必須下載新的指令碼來建立新的掛接點。After those 12 hours, you need to download a new script to create a new mountpoint.

後續步驟Next steps

在本教學課程中,您已了解如何:In this tutorial, you learned how to:

  • 建立 VM 的備份Create a backup of a VM
  • 排定每日備份Schedule a daily backup
  • 從備份還原檔案Restore a file from a backup

請前進到下一個教學課程,了解如何監視虛擬機器。Advance to the next tutorial to learn about monitoring virtual machines.