使用 PowerShell 在 Azure 中備份虛擬機器Back up a virtual machine in Azure with PowerShell

Azure PowerShell AZ 模組用於從命令列或在指令碼中建立和管理 Azure 資源。The Azure PowerShell AZ module is used to create and manage Azure resources from the command line or in scripts.

Azure 備份會備份內部部署機器與應用程式,以及 Azure VM。Azure Backup backs up on-premises machines and apps, and Azure VMs. 本文說明如何使用 AZ 模組來備份 Azure VM。This article shows you how to back up an Azure VM with the AZ module. 此外,您可以使用 Azure CLI 或在Azure 入口網站中備份 VM。Alternatively, you can back up a VM using the Azure CLI, or in the Azure portal.

本快速入門能夠在現有的 Azure VM 上進行備份。This quickstart enables backup on an existing Azure VM. 如果您需要建立 VM,您可以使用 Azure PowerShell 來建立 VMIf you need to create a VM, you can create a VM with Azure PowerShell.

本快速入門需要 Azure PowerShell AZ 模組 1.0.0 版或更新版本。This quickstart requires the Azure PowerShell AZ module version 1.0.0 or later. 執行 Get-Module -ListAvailable Az 以尋找版本。Run Get-Module -ListAvailable Az to find the version. 如果您需要安裝或升級,請參閱安裝 Azure PowerShell 模組If you need to install or upgrade, see Install Azure PowerShell module.

注意

本文已更新為使用新的 Azure PowerShell Az 模組。This article has been updated to use the new Azure PowerShell Az module. AzureRM 模組在至少 2020 年 12 月之前都還會持續收到錯誤 (Bug) 修正,因此您仍然可以持續使用。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要深入了解新的 Az 模組和 AzureRM 的相容性,請參閱新的 Azure PowerShell Az 模組簡介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 如需 Az 模組安裝指示,請參閱安裝 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

登入並註冊Sign in and register

  1. 使用 Connect-AzAccount 命令登入 Azure 訂用帳戶並遵循畫面上的指示。Log in to your Azure subscription with the Connect-AzAccount command and follow the on-screen directions.

    Connect-AzAccount
    
  2. 第一次使用 Azure 備份時,您必須使用 Register-AzResourceProvider 在您的訂用帳戶中註冊 Azure 復原服務提供者,如下所示:The first time you use Azure Backup, you must register the Azure Recovery Service provider in your subscription with Register-AzResourceProvider, as follows:

    Register-AzResourceProvider -ProviderNamespace "Microsoft.RecoveryServices"
    

建立復原服務保存庫Create a Recovery Services vault

復原服務保存庫是一個邏輯容器,可儲存受保護資源 (例如 Azure VM) 的備份資料。A Recovery Services vault is a logical container that stores backup data for protected resources, such as Azure VMs. 當備份作業執行時,它會在復原服務保存庫內建立復原點。When a backup job runs, it creates a recovery point inside the Recovery Services vault. 然後您可以使用其中一個復原點,將資料還原到指定的時間點。You can then use one of these recovery points to restore data to a given point in time.

當您建立保存庫:When you create the vault:

  • 針對資源群組和位置,指定您想要備份的資源群組和 VM 的位置。For the resource group and location, specify the resource group and location of the VM you want to back up.
  • 如果您使用範例指令碼來建立 VM,則資源群組為 myResourceGroup、VM 為*myVM,而資源位於 WestEurope 區域。If you used this sample script to create the VM, the resource group is myResourceGroup, the VM is *myVM, and the resources are in the WestEurope region.
  • Azure 備份會自動處理用於備份資料的儲存體。Azure Backup automatically handles storage for backed up data. 保存庫預設會使用異地備援儲存體 (GRS)By default the vault uses Geo-Redundant Storage (GRS). 異地備援可確保會將備份的資料複寫到與主要區域相距數百英哩的次要 Azure 區域。Geo-redundancy ensures that backed up data is replicated to a secondary Azure region, hundreds of miles away from the primary region.

現在請建立保存庫:Now create a vault:

  1. 使用 New-AzRecoveryServicesVault 來建立保存庫:Use the New-AzRecoveryServicesVault to create the vault:

    New-AzRecoveryServicesVault `
        -ResourceGroupName "myResourceGroup" `
        -Name "myRecoveryServicesVault" `
    -Location "WestEurope"
    
  2. 使用 Set-AzRecoveryServicesVaultContext 設定保存庫內容,如下所示:Set the vault context with Set-AzRecoveryServicesVaultContext, as follows:

    Get-AzRecoveryServicesVault `
        -Name "myRecoveryServicesVault" | Set-AzRecoveryServicesVaultContext
    
  3. 使用Set-AzRecoveryServicesBackupProperty 變更保存庫的儲存體備援組態 (LRS/GRS),如下所示:Change the storage redundancy configuration (LRS/GRS) of the vault with Set-AzRecoveryServicesBackupProperty, as follows:

    Get-AzRecoveryServicesVault `
        -Name "myRecoveryServicesVault" | Set-AzRecoveryServicesBackupProperty -BackupStorageRedundancy LocallyRedundant/GeoRedundant
    

    注意

    只有當保存庫中沒有受保護的備份項目時,才可以修改儲存體備援。Storage Redundancy can be modified only if there are no backup items protected to the vault.

啟用 Azure VM 的備份Enable backup for an Azure VM

您可以啟用 Azure VM 的備份功能,並指定備份原則。You enable backup for an Azure VM, and specify a backup policy.

  • 原則可定義何時執行備份,以及備份所建立的復原點應保留多長的時間。The policy defines when backups run, and how long recovery points created by the backups should be retained.
  • 預設保護原則會每天對 VM 執行備份一次,並將建立的復原點保留 30 天。The default protection policy runs a backup once a day for the VM, and retains the created recovery points for 30 days. 您可以使用此預設原則來快速保護您的 VM。You can use this default policy to quickly protect your VM.

請依照下列方式啟用備份:Enable backup as follows:

  1. 首先,使用 Get-AzRecoveryServicesBackupProtectionPolicy 設定預設原則:First, set the default policy with Get-AzRecoveryServicesBackupProtectionPolicy:

    $policy = Get-AzRecoveryServicesBackupProtectionPolicy     -Name "DefaultPolicy"
    
  2. 使用 Enable-AzRecoveryServicesBackupProtection 啟用 VM 備份。Enable VM backup with Enable-AzRecoveryServicesBackupProtection. 指定原則、資源群組及 VM 名稱。Specify the policy, the resource group, and the VM name.

    Enable-AzRecoveryServicesBackupProtection `
        -ResourceGroupName "myResourceGroup" `
        -Name "myVM" `
        -Policy $policy
    

開始備份作業Start a backup job

根據備份原則中指定的排程執行備份。Backups run in accordance with the schedule specified in the backup policy. 您也可以執行臨機操作備份:You can also run an ad hoc backup:

  • 第一個初始備份作業會建立完整復原點。The first initial backup job creates a full recovery point.
  • 在初始備份之後,每個備份作業都會建立增量復原點。After the initial backup, each backup job creates incremental recovery points.
  • 增量復原點符合儲存和時間效率,因為它只會傳輸自上次備份後所做的變更。Incremental recovery points are storage and time-efficient, as they only transfer changes made since the last backup.

若要執行臨機操作備份,請您使用 Backup-AzRecoveryServicesBackupItemTo run an ad hoc backup, you use the Backup-AzRecoveryServicesBackupItem.

執行臨機操作備份作業,如下所示:Run an ad hoc backup job as follows:

  1. 指定容器,取得 VM 資訊,然後執行備份。Specify the container, obtain VM information, and run the backup.

    $backupcontainer = Get-AzRecoveryServicesBackupContainer `
        -ContainerType "AzureVM" `
        -FriendlyName "myVM"
    
    $item = Get-AzRecoveryServicesBackupItem `
        -Container $backupcontainer `
        -WorkloadType "AzureVM"
    
    Backup-AzRecoveryServicesBackupItem -Item $item
    
  2. 因為第一個備份作業會建立完整復原點,所以您可能需要等待長達 20 分鐘。You might need to wait up to 20 minutes, since the first backup job creates a full recovery point. 請監視作業,如下一個程序所述。Monitor the job as described in the next procedure.

監視備份作業Monitor the backup job

  1. 執行 Get-AzRecoveryservicesBackupJob 以監視作業狀態。Run Get-AzRecoveryservicesBackupJob to monitor the job status.

    Get-AzRecoveryservicesBackupJob
    

    輸出會類似下列範例,其顯示的作業為 [進行中]︰Output is similar to the following example, which shows the job as InProgress:

    WorkloadName   Operation         Status       StartTime              EndTime                JobID
    ------------   ---------         ------       ---------              -------                -----
    myvm           Backup            InProgress   9/18/2017 9:38:02 PM                          9f9e8f14
    myvm           ConfigureBackup   Completed    9/18/2017 9:33:18 PM   9/18/2017 9:33:51 PM   fe79c739
    
  2. 當作業狀態為 [已完成] 時,表示 VM 受到保護,並已儲存完整的復原點。When the job status is Completed, the VM is protected and has a full recovery point stored.

清除部署Clean up the deployment

如果您不再需要備份 VM,便可加以清除。If you no longer need to back up the VM, you can clean it up.

  • 如果您想要嘗試還原 VM,請略過清除程序。If you want to try out restoring the VM, skip the clean up.
  • 如果您使用現有的 VM,可以略過最後的 Remove-AzResourceGroup Cmdlet,讓資源群組和 VM 留在原處。If you used an existing VM, you can skip the final Remove-AzResourceGroup cmdlet to leave the resource group and VM in place.

停用保護,移除還原點和保存庫。Disable protection, remove the restore points and vault. 然後,刪除資源群組和相關聯的 VM 資源,如下所示:Then delete the resource group and associated VM resources, as follows:

Disable-AzRecoveryServicesBackupProtection -Item $item -RemoveRecoveryPoints
$vault = Get-AzRecoveryServicesVault -Name "myRecoveryServicesVault"
Remove-AzRecoveryServicesVault -Vault $vault
Remove-AzResourceGroup -Name "myResourceGroup"

後續步驟Next steps

在本快速入門中,您已建立復原服務保存庫、啟用 VM 的保護功能,並建立初始復原點。In this quickstart, you created a Recovery Services vault, enabled protection on a VM, and created the initial recovery point.