将文件还原到 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

如果选择在本地安装并使用 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. 若要模拟意外文件删除和恢复过程,请从 Web 服务器中删除一页。To simulate an accidental file deletion and recovery process, you delete a page from a web server. 如果需要运行 Web 服务器且已使用 Azure 备份保护的 Linux VM,请参阅在 Azure 中使用 CLI 备份虚拟机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. 在此示例中,我们从 Web 服务器中删除文件来演示文件级恢复过程。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. 若要确认网站当前是否正常运行,请打开 Web 浏览器到 VM 的公共 IP 地址。To confirm that your web site currently works, open a web browser to the public IP address of your VM. 保持 Web 浏览器窗口处于打开状态。Leave the web browser window open.

    默认的 NGINX 网页

  3. 使用 SSH 连接到 VM。Connect to your VM with SSH. 将 publicIpAddress 替换为你在前一个命令中获取的公共 IP 地址:Replace publicIpAddress with the public IP address that you obtained in a previous command:

    ssh publicIpAddress
    
  4. 从 Web 服务器中的 /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. 在 Web 浏览器中,刷新网页。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 list 命令。To list recovery points for your VM, use az backup recoverypoint list. 在此示例中,我们为在 myRecoveryServicesVault 中受保护的名为 myVM 的 VM 选择最近的恢复点: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-rp 命令。To obtain the script that connects, or mounts, the recovery point to your VM, use az backup restore files mount-rp. 下面的示例可为在 myRecoveryServicesVault 中受保护的名为 myVM 的 VM 获取脚本。The following example obtains the script for the VM named myVM that is protected in myRecoveryServicesVault.

    将 myRecoveryPointName 替换为你在前一个命令中获取的恢复点的名称:Replace 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). 提供已下载脚本的名称,并将 publicIpAddress 替换为 VM 的公共 IP 地址。Provide 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. 将 publicIpAddress 替换为 VM 的公共 IP 地址,如下所示: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/Volume1:The following example output shows that the recovery point is mounted at /home/azureuser/myVM-20170919213536/Volume1:

    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. 在 Web 浏览器中,刷新网页。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.

    将 myRecoveryPointName 替换为你在之前命令中获取的恢复点的名称。Replace 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,并且还原了 Web 服务器的文件。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.