您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

在 Azure 中备份 Linux 虚拟机Back up 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 just specific files. 本文介绍如何将单个文件还原到运行 nginx 的 Linux VM。This article explains how to restore a single file to a Linux VM running nginx. 如果没有可用的 VM,可以参考 Linux 快速入门创建一个 VM。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 simple 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 will 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. 文件恢复使用一个在 VM 上运行的脚本将恢复点装载为本地驱动器。File Recovery uses a script that runs on the VM, to mount the recovery point as local drive. 这些驱动器将保持装载 12 小时,以便可以从恢复点复制文件并将其还原到 VM。These drives will 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.209The 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. 在本地计算机上,打开已从中连接到 VM 的 IP 地址、显示 nginx 默认页面的浏览器标签页。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 close 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.