使用 Azure PowerShell 來設定 Azure 虛擬機器的災害復原

在本文中,您會了解如何使用 Azure PowerShell 來安裝和測試 Azure 虛擬機器的災害復原。

您將學習如何:

  • 建立復原服務保存庫。
  • 設定 PowerShell 工作階段的保存庫內容。
  • 讓保存庫準備好開始複寫 Azure 虛擬機器。
  • 建立網路對應。
  • 建立作為複寫虛擬機器目的地的儲存體帳戶。
  • 將 Azure 虛擬機器複寫到復原區域以供災害復原之用。
  • 執行測試容錯移轉、驗證和清除測試容錯移轉。
  • 容錯移轉至復原區域。

注意

可透過入口網站使用的案例功能,並非都能透過 Azure PowerShell 來使用。 目前不支援透過 Azure PowerShell 來使用的部分案例功能包括:

  • 能夠指定應該複寫虛擬機器中的所有磁碟,而不需要明確指定虛擬機器的每個磁碟。

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

必要條件

在開始之前:

登入您的 Microsoft Azure 訂用帳戶

使用 Connect-AzAccount Cmdlet 登入您的 Azure 訂用帳戶。

Connect-AzAccount

選取 Azure 訂閱。 您可以使用 Get-AzSubscription Cmdlet 取得您有權存取的 Azure 訂用帳戶清單。 使用 Set-AzContext Cmdlet 來選取要使用的 Azure 訂用帳戶。

Set-AzContext -SubscriptionId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

取得要複寫的虛擬機器詳細資料

在本文中,「美國東部」區域的虛擬機器會複寫並復原到「美國西部 2」區域。 要複寫的虛擬機器有一個 OS 磁碟和一個單一資料磁碟。 此範例所使用的虛擬機器名稱是 AzureDemoVM

# Get details of the virtual machine
$VM = Get-AzVM -ResourceGroupName "A2AdemoRG" -Name "AzureDemoVM"

Write-Output $VM
ResourceGroupName  : A2AdemoRG
Id                 : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/A2AdemoRG/providers/Microsoft.Compute/virtualMachines/AzureDemoVM
VmId               : 1b864902-c7ea-499a-ad0f-65da2930b81b
Name               : AzureDemoVM
Type               : Microsoft.Compute/virtualMachines
Location           : eastus
Tags               : {}
DiagnosticsProfile : {BootDiagnostics}
HardwareProfile    : {VmSize}
NetworkProfile     : {NetworkInterfaces}
OSProfile          : {ComputerName, AdminUsername, WindowsConfiguration, Secrets}
ProvisioningState  : Succeeded
StorageProfile     : {ImageReference, OsDisk, DataDisks}

取得虛擬機器磁碟的磁碟詳細資料。 稍後在開始複寫虛擬機器時,會用到磁碟詳細資料。

$OSDiskVhdURI = $VM.StorageProfile.OsDisk.Vhd
$DataDisk1VhdURI = $VM.StorageProfile.DataDisks[0].Vhd

建立復原服務保存庫

建立要在其中建立復原服務保存庫的資源群組。

重要

  • 復原服務保存庫和受保護的虛擬機器必須位於不同的 Azure 位置。
  • 復原服務保存庫的資源群組和受保護的虛擬機器必須位於不同的 Azure 位置。
  • 復原服務保存庫和其所屬的資源群組可位於相同的 Azure 位置。

在本文範例中,受保護的虛擬機器位於「美國東部」區域。 針對災害復原所選取的復原區域為「美國西部 2」區域。 復原服務保存庫和保存庫的資源群組都位於復原區域 (美國西部 2)。

#Create a resource group for the recovery services vault in the recovery Azure region
New-AzResourceGroup -Name "a2ademorecoveryrg" -Location "West US 2"
ResourceGroupName : a2ademorecoveryrg
Location          : westus2
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/a2ademorecoveryrg

建立復原服務保存庫。 在此範例中,會在美國西部 2 區域中建立名為 a2aDemoRecoveryVault 的復原服務保存庫。

#Create a new Recovery services vault in the recovery region
$vault = New-AzRecoveryServicesVault -Name "a2aDemoRecoveryVault" -ResourceGroupName "a2ademorecoveryrg" -Location "West US 2"

Write-Output $vault
Name              : a2aDemoRecoveryVault
ID                : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/a2ademorecoveryrg/providers/Microsoft.RecoveryServices/vaults/a2aDemoRecoveryVault
Type              : Microsoft.RecoveryServices/vaults
Location          : westus2
ResourceGroupName : a2ademorecoveryrg
SubscriptionId    : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Properties        : Microsoft.Azure.Commands.RecoveryServices.ARSVaultProperties

設定保存庫內容

設定要用於 PowerShell 工作階段的保存庫內容。 設定保存庫內容之後,會在所選取保存庫的內容中執行 PowerShell 工作階段中的 Azure Site Recovery 作業。

#Setting the vault context.
Set-AzRecoveryServicesAsrVaultContext -Vault $vault
ResourceName         ResourceGroupName ResourceNamespace          ResourceType
------------         ----------------- -----------------          -----------
a2aDemoRecoveryVault a2ademorecoveryrg Microsoft.RecoveryServices Vaults
#Delete the downloaded vault settings file
Remove-Item -Path $Vaultsettingsfile.FilePath

針對 Azure 至 Azure 移轉,您可以將保存庫內容設定為新建立的保存庫:

#Set the vault context for the PowerShell session.
Set-AzRecoveryServicesAsrVaultContext -Vault $vault

讓保存庫準備好開始複寫 Azure 虛擬機器

建立 Site Recovery 網狀架構物件來代表主要 (來源) 區域

保存庫中的網狀架構物件可代表 Azure 區域。 主要網狀架構物件是建立來代表保護虛擬機器之保存庫所屬的 Azure 區域。 在本文範例中,受保護的虛擬機器位於「美國東部」區域。

  • 每個區域只能建立單一網狀架構物件。
  • 如果您先前已在 Azure 入口網站中為 VM 啟用 Site Recovery 複寫,Site Recovery 便會自動建立網狀架構物件。 如果區域中已存在網狀架構物件,您便無法建立新的物件。

在您開始之前,請了解 Site Recovery 作業會以非同步方式執行。 您開始作業時,會提交 Azure Site Recovery 作業,並傳回作業追蹤物件。 請使用作業追蹤物件,來取得作業的最新狀態 (Get-AzRecoveryServicesAsrJob),以及監視作業的狀態。

#Create Primary ASR fabric
$TempASRJob = New-AzRecoveryServicesAsrFabric -Azure -Location 'East US'  -Name "A2Ademo-EastUS"

# Track Job status to check for completion
while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){
        #If the job hasn't completed, sleep for 10 seconds before checking the job status again
        sleep 10;
        $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob
}

#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded"
Write-Output $TempASRJob.State

$PrimaryFabric = Get-AzRecoveryServicesAsrFabric -Name "A2Ademo-EastUS"

如果會由相同的保存庫保護多個 Azure 區域的虛擬機器,請為每個來源 Azure 區域各建立一個網狀架構物件。

建立 Site Recovery 網狀架構物件來代表復原區域

復原網狀架構物件可代表復原 Azure 位置。 如果發生容錯移轉,虛擬機器會複寫並復原到復原網狀架構所代表的復原區域中。 此範例所使用的復原 Azure 區域是「美國西部 2」。

#Create Recovery ASR fabric
$TempASRJob = New-AzRecoveryServicesAsrFabric -Azure -Location 'West US 2'  -Name "A2Ademo-WestUS"

# Track Job status to check for completion
while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){
        sleep 10;
        $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob
}

#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded"
Write-Output $TempASRJob.State

$RecoveryFabric = Get-AzRecoveryServicesAsrFabric -Name "A2Ademo-WestUS"

在主要網狀架構中建立 Site Recovery 保護容器

保護容器是用來將複寫的項目群組到網狀架構內的容器。

#Create a Protection container in the primary Azure region (within the Primary fabric)
$TempASRJob = New-AzRecoveryServicesAsrProtectionContainer -InputObject $PrimaryFabric -Name "A2AEastUSProtectionContainer"

#Track Job status to check for completion
while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){
        sleep 10;
        $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob
}

Write-Output $TempASRJob.State

$PrimaryProtContainer = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $PrimaryFabric -Name "A2AEastUSProtectionContainer"

在復原網狀架構中建立 Site Recovery 保護容器

#Create a Protection container in the recovery Azure region (within the Recovery fabric)
$TempASRJob = New-AzRecoveryServicesAsrProtectionContainer -InputObject $RecoveryFabric -Name "A2AWestUSProtectionContainer"

#Track Job status to check for completion
while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){
        sleep 10;
        $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob
}

#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded"

Write-Output $TempASRJob.State

$RecoveryProtContainer = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $RecoveryFabric -Name "A2AWestUSProtectionContainer"

啟用區域至區域複寫時建立網狀架構和容器

啟用區域至區域複寫時,只會建立一個網狀架構。 但是會有兩個容器。 假設區域是西歐,請使用下列命令來取得主要和保護容器 -

$primaryProtectionContainer = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $fabric -Name "asr-a2a-default-westeurope-container"
$recoveryPprotectionContainer = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $fabric -Name "asr-a2a-default-westeurope-t-container"

建立複寫原則

#Create replication policy
$TempASRJob = New-AzRecoveryServicesAsrPolicy -AzureToAzure -Name "A2APolicy" -RecoveryPointRetentionInHours 24 -ApplicationConsistentSnapshotFrequencyInHours 4

#Track Job status to check for completion
while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){
        sleep 10;
        $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob
}

#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded"
Write-Output $TempASRJob.State

$ReplicationPolicy = Get-AzRecoveryServicesAsrPolicy -Name "A2APolicy"

在主要和復原保護容器之間建立保護容器對應

保護容器對應會使用復原保護容器和複寫原則來對應主要的保護容器。 請針對每個會用來在保護容器組之間複寫虛擬機器的複寫原則,各建立一個對應。

#Create Protection container mapping between the Primary and Recovery Protection Containers with the Replication policy
$TempASRJob = New-AzRecoveryServicesAsrProtectionContainerMapping -Name "A2APrimaryToRecovery" -Policy $ReplicationPolicy -PrimaryProtectionContainer $PrimaryProtContainer -RecoveryProtectionContainer $RecoveryProtContainer

#Track Job status to check for completion
while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){
        sleep 10;
        $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob
}

#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded"
Write-Output $TempASRJob.State

$EusToWusPCMapping = Get-AzRecoveryServicesAsrProtectionContainerMapping -ProtectionContainer $PrimaryProtContainer -Name "A2APrimaryToRecovery"

啟用區域至區域複寫時建立保護容器對應

啟用區域至區域複寫時,請使用下列命令來建立保護容器對應。 假設區域是西歐,則命令會是 -

$protContainerMapping = Get-AzRecoveryServicesAsrProtectionContainerMapping -ProtectionContainer $PrimprotectionContainer -Name "westeurope-westeurope-24-hour-retention-policy-s"

建立用於容錯回復 (在容錯移轉之後反向複寫) 的保護容器對應

在容錯移轉之後,您可在準備好讓已容錯移轉的虛擬機器回復到原始 Azure 區域時進行容錯回復。 為了進行容錯回復,系統會將已容錯移轉的虛擬機器,從容錯移轉後的區域反向複寫到原始區域。 在反向複寫時,原始區域和復原區域的角色會對調。 原始區域現在會變成新的復原區域,原本的復原區域現在則會變成主要區域。 反向複寫的保護容器對應會表示原始區域和復原區域的對調角色。

#Create Protection container mapping (for fail back) between the Recovery and Primary Protection Containers with the Replication policy
$TempASRJob = New-AzRecoveryServicesAsrProtectionContainerMapping -Name "A2ARecoveryToPrimary" -Policy $ReplicationPolicy -PrimaryProtectionContainer $RecoveryProtContainer -RecoveryProtectionContainer $PrimaryProtContainer

#Track Job status to check for completion
while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){
        sleep 10;
        $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob
}

#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded"
Write-Output $TempASRJob.State

$WusToEusPCMapping = Get-AzRecoveryServicesAsrProtectionContainerMapping -ProtectionContainer $RecoveryProtContainer -Name "A2ARecoveryToPrimary"

建立快取儲存體帳戶和目標儲存體帳戶

快取儲存體帳戶是和所複寫虛擬機器位於相同 Azure 區域的標準儲存體帳戶。 快取儲存體帳戶則會用來在變更移至復原 Azure 區域前,暫時保存複寫變更。 高流失支援也適用於 Azure Site Recovery,以取得更高的流失限制。 若要使用這項功能,請建立儲存體帳戶的進階區塊 Blob 類型,然後使用其做為快取儲存體帳戶。 您可以選擇 (但並非必要) 為虛擬機器的不同磁碟指定不同的快取儲存體帳戶。 如果您使用不同的快取儲存體帳戶,請確定其屬於相同類型 (標準或進階區塊 Blob)。 如需詳細資訊,請參閱 Azure VM 災害復原 - 高流失支援

#Create Cache storage account for replication logs in the primary region
$EastUSCacheStorageAccount = New-AzStorageAccount -Name "a2acachestorage" -ResourceGroupName "A2AdemoRG" -Location 'East US' -SkuName Standard_LRS -Kind Storage

針對未使用受控磁碟的虛擬機器,目標儲存體帳戶是復原區域中要作為虛擬機器磁碟複寫目的地的儲存體帳戶。 目標儲存體帳戶可以是標準儲存體帳戶,也可以是進階儲存體帳戶。 請根據磁碟的資料變更率 (IO 寫入速率) 以及 Azure Site Recovery 針對儲存體類型所支援的變換限制,來選取所需的儲存體帳戶種類。

#Create Target storage account in the recovery region. In this case a Standard Storage account
$WestUSTargetStorageAccount = New-AzStorageAccount -Name "a2atargetstorage" -ResourceGroupName "a2ademorecoveryrg" -Location 'West US 2' -SkuName Standard_LRS -Kind Storage

建立網路對應

網路對應會將主要區域的虛擬網路,對應至復原區域的虛擬網路。 網路對應會指定復原區域中的 Azure 虛擬網路,而主要虛擬網路中的虛擬機器便應容錯移轉至此網路。 一個 Azure 虛擬網路只能對應至復原區域中的單一 Azure 虛擬網路。

  • 在復原區域中建立要作為容錯移轉目的地的 Azure 虛擬網路:

      #Create a Recovery Network in the recovery region
      $WestUSRecoveryVnet = New-AzVirtualNetwork -Name "a2arecoveryvnet" -ResourceGroupName "a2ademorecoveryrg" -Location 'West US 2' -AddressPrefix "10.0.0.0/16"
    
      Add-AzVirtualNetworkSubnetConfig -Name "default" -VirtualNetwork $WestUSRecoveryVnet -AddressPrefix "10.0.0.0/20" | Set-AzVirtualNetwork
    
      $WestUSRecoveryNetwork = $WestUSRecoveryVnet.Id
    
  • 擷取主要虛擬網路。 虛擬機器所連線的 VNet:

      #Retrieve the virtual network that the virtual machine is connected to
    
      #Get first network interface card(nic) of the virtual machine
      $SplitNicArmId = $VM.NetworkProfile.NetworkInterfaces[0].Id.split("/")
    
      #Extract resource group name from the ResourceId of the nic
      $NICRG = $SplitNicArmId[4]
    
      #Extract resource name from the ResourceId of the nic
      $NICname = $SplitNicArmId[-1]
    
      #Get network interface details using the extracted resource group name and resource name
      $NIC = Get-AzNetworkInterface -ResourceGroupName $NICRG -Name $NICname
    
      #Get the subnet ID of the subnet that the nic is connected to
      $PrimarySubnet = $NIC.IpConfigurations[0].Subnet
    
      # Extract the resource ID of the Azure virtual network the nic is connected to from the subnet ID
      $EastUSPrimaryNetwork = (Split-Path(Split-Path($PrimarySubnet.Id))).Replace("\","/")
    
  • 在主要虛擬網路與復原虛擬網路之間建立網路對應:

      #Create an ASR network mapping between the primary Azure virtual network and the recovery Azure virtual network
      $TempASRJob = New-AzRecoveryServicesAsrNetworkMapping -AzureToAzure -Name "A2AEusToWusNWMapping" -PrimaryFabric $PrimaryFabric -PrimaryAzureNetworkId $EastUSPrimaryNetwork -RecoveryFabric $RecoveryFabric -RecoveryAzureNetworkId $WestUSRecoveryNetwork
    
      #Track Job status to check for completion
      while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){
              sleep 10;
              $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob
      }
    
      #Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded"
      Write-Output $TempASRJob.State
    
  • 建立反方向 (容錯回復) 的網路對應:

    #Create an ASR network mapping for fail back between the recovery Azure virtual network and the primary Azure virtual network
    $TempASRJob = New-AzRecoveryServicesAsrNetworkMapping -AzureToAzure -Name "A2AWusToEusNWMapping" -PrimaryFabric $RecoveryFabric -PrimaryAzureNetworkId $WestUSRecoveryNetwork -RecoveryFabric $PrimaryFabric -RecoveryAzureNetworkId $EastUSPrimaryNetwork
    
    #Track Job status to check for completion
    while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){
            sleep 10;
            $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob
    }
    
    #Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded"
    Write-Output $TempASRJob.State
    

複寫 Azure 虛擬機器

受控磁碟複寫 Azure 虛擬機器。

#Get the resource group that the virtual machine must be created in when failed over.
$RecoveryRG = Get-AzResourceGroup -Name "a2ademorecoveryrg" -Location "West US 2"

#Specify replication properties for each disk of the VM that is to be replicated (create disk replication configuration)

#OsDisk
$OSdiskId = $vm.StorageProfile.OsDisk.ManagedDisk.Id
$RecoveryOSDiskAccountType = $vm.StorageProfile.OsDisk.ManagedDisk.StorageAccountType
$RecoveryReplicaDiskAccountType = $vm.StorageProfile.OsDisk.ManagedDisk.StorageAccountType

$OSDiskReplicationConfig = New-AzRecoveryServicesAsrAzureToAzureDiskReplicationConfig -ManagedDisk -LogStorageAccountId $EastUSCacheStorageAccount.Id `
         -DiskId $OSdiskId -RecoveryResourceGroupId  $RecoveryRG.ResourceId -RecoveryReplicaDiskAccountType  $RecoveryReplicaDiskAccountType `
         -RecoveryTargetDiskAccountType $RecoveryOSDiskAccountType

# Data disk
$datadiskId1 = $vm.StorageProfile.DataDisks[0].ManagedDisk.Id
$RecoveryReplicaDiskAccountType = $vm.StorageProfile.DataDisks[0].ManagedDisk.StorageAccountType
$RecoveryTargetDiskAccountType = $vm.StorageProfile.DataDisks[0].ManagedDisk.StorageAccountType

$DataDisk1ReplicationConfig  = New-AzRecoveryServicesAsrAzureToAzureDiskReplicationConfig -ManagedDisk -LogStorageAccountId $EastUSCacheStorageAccount.Id `
         -DiskId $datadiskId1 -RecoveryResourceGroupId $RecoveryRG.ResourceId -RecoveryReplicaDiskAccountType $RecoveryReplicaDiskAccountType `
         -RecoveryTargetDiskAccountType $RecoveryTargetDiskAccountType

#Create a list of disk replication configuration objects for the disks of the virtual machine that are to be replicated.
$diskconfigs = @()
$diskconfigs += $OSDiskReplicationConfig, $DataDisk1ReplicationConfig

#Start replication by creating replication protected item. Using a GUID for the name of the replication protected item to ensure uniqueness of name.
$TempASRJob = New-AzRecoveryServicesAsrReplicationProtectedItem -AzureToAzure -AzureVmId $VM.Id -Name (New-Guid).Guid -ProtectionContainerMapping $EusToWusPCMapping -AzureToAzureDiskReplicationConfiguration $diskconfigs -RecoveryResourceGroupId $RecoveryRG.ResourceId

非受控磁碟複寫 Azure 虛擬機器。

#Specify replication properties for each disk of the VM that is to be replicated (create disk replication configuration)

#Disk replication configuration for the OS disk
$OSDiskReplicationConfig = New-AzRecoveryServicesAsrAzureToAzureDiskReplicationConfig -VhdUri $OSDiskVhdURI.Uri -LogStorageAccountId $EastUSCacheStorageAccount.Id -RecoveryAzureStorageAccountId $WestUSTargetStorageAccount.Id

#Disk replication configuration for data disk
$DataDisk1ReplicationConfig = New-AzRecoveryServicesAsrAzureToAzureDiskReplicationConfig -VhdUri $DataDisk1VhdURI.Uri -LogStorageAccountId $EastUSCacheStorageAccount.Id -RecoveryAzureStorageAccountId $WestUSTargetStorageAccount.Id

#Create a list of disk replication configuration objects for the disks of the virtual machine that are to be replicated.
$diskconfigs = @()
$diskconfigs += $OSDiskReplicationConfig, $DataDisk1ReplicationConfig

#Get the resource group that the virtual machine must be created in when failed over.
$RecoveryRG = Get-AzResourceGroup -Name "a2ademorecoveryrg" -Location "West US 2"

#Start replication by creating replication protected item. Using a GUID for the name of the replication protected item to ensure uniqueness of name.
$TempASRJob = New-AzRecoveryServicesAsrReplicationProtectedItem -AzureToAzure -AzureVmId $VM.Id -Name (New-Guid).Guid -ProtectionContainerMapping $EusToWusPCMapping -AzureToAzureDiskReplicationConfiguration $diskconfigs -RecoveryResourceGroupId $RecoveryRG.ResourceId

#Track Job status to check for completion
while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){
        sleep 10;
        $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob
}

#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded"
Write-Output $TempASRJob.State

複寫作業啟動成功後,系統就會將虛擬機器資料複寫至復原區域。

複寫程序一開始會先在復原區域中植入虛擬機器複寫磁碟的複本。 這個階段稱為初始複寫階段。

初始複寫完成後,複寫會進入差異同步處理階段。 此時,系統會保護虛擬機器,供您對其執行測試容錯移轉作業。 初始複寫完成後,代表虛擬機器的複寫項目狀態會進入受保護狀態。

請藉由取得虛擬機器所對應的受保護複寫項目詳細資料,來監視虛擬機器的複寫狀態和複寫健康情況。

Get-AzRecoveryServicesAsrReplicationProtectedItem -ProtectionContainer $PrimaryProtContainer | Select FriendlyName, ProtectionState, ReplicationHealth
FriendlyName ProtectionState ReplicationHealth
------------ --------------- -----------------
AzureDemoVM  Protected       Normal

執行測試容錯移轉、驗證和清除測試容錯移轉

虛擬機器的複寫到達受保護狀態之後,就能對虛擬機器 (對虛擬機器的受保護複寫項目) 執行測試容錯移轉作業。

#Create a separate network for test failover (not connected to my DR network)
$TFOVnet = New-AzVirtualNetwork -Name "a2aTFOvnet" -ResourceGroupName "a2ademorecoveryrg" -Location 'West US 2' -AddressPrefix "10.3.0.0/16"

Add-AzVirtualNetworkSubnetConfig -Name "default" -VirtualNetwork $TFOVnet -AddressPrefix "10.3.0.0/20" | Set-AzVirtualNetwork

$TFONetwork= $TFOVnet.Id

執行測試容錯移轉。

$ReplicationProtectedItem = Get-AzRecoveryServicesAsrReplicationProtectedItem -FriendlyName "AzureDemoVM" -ProtectionContainer $PrimaryProtContainer

$TFOJob = Start-AzRecoveryServicesAsrTestFailoverJob -ReplicationProtectedItem $ReplicationProtectedItem -AzureVMNetworkId $TFONetwork -Direction PrimaryToRecovery

等待測試容錯移轉作業完成。

Get-AzRecoveryServicesAsrJob -Job $TFOJob
Name             : 3dcb043e-3c6d-4e0e-a42e-8d4245668547
ID               : /Subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/a2ademorecoveryrg/providers/Microsoft.RecoveryServices/vaults/a2aDemoR
                   ecoveryVault/replicationJobs/3dcb043e-3c6d-4e0e-a42e-8d4245668547
Type             : Microsoft.RecoveryServices/vaults/replicationJobs
JobType          : TestFailover
DisplayName      : Test failover
ClientRequestId  : 1ef8515b-b130-4452-a44d-91aaf071931c ActivityId: 907bb2bc-ebe6-4732-8b66-77d0546eaba8
State            : Succeeded
StateDescription : Completed
StartTime        : 4/25/2018 4:29:43 AM
EndTime          : 4/25/2018 4:33:06 AM
TargetObjectId   : ce86206c-bd78-53b4-b004-39b722c1ac3a
TargetObjectType : ProtectionEntity
TargetObjectName : azuredemovm
AllowedActions   :
Tasks            : {Prerequisites check for test failover, Create test virtual machine, Preparing the virtual machine, Start the virtual machine}
Errors           : {}

測試容錯移轉作業成功完成之後,就可以連線至測試容錯移轉虛擬機器,並驗證測試容錯移轉。

在測試容錯移轉虛擬機器上完成測試後,請啟動清除測試容錯移轉作業來清除測試複本。 此作業會刪除測試容錯移轉所建立的虛擬機器測試複本。

$Job_TFOCleanup = Start-AzRecoveryServicesAsrTestFailoverCleanupJob -ReplicationProtectedItem $ReplicationProtectedItem

Get-AzRecoveryServicesAsrJob -Job $Job_TFOCleanup | Select State
State
-----
Succeeded

容錯移轉至 Azure

將虛擬機器容錯移轉至特定復原點。

$RecoveryPoints = Get-AzRecoveryServicesAsrRecoveryPoint -ReplicationProtectedItem $ReplicationProtectedItem

#The list of recovery points returned may not be sorted chronologically and will need to be sorted first, in order to be able to find the oldest or the latest recovery points for the virtual machine.
"{0} {1}" -f $RecoveryPoints[0].RecoveryPointType, $RecoveryPoints[-1].RecoveryPointTime
CrashConsistent 4/24/2018 11:10:25 PM
#Start the fail over job
$Job_Failover = Start-AzRecoveryServicesAsrUnplannedFailoverJob -ReplicationProtectedItem $ReplicationProtectedItem -Direction PrimaryToRecovery -RecoveryPoint $RecoveryPoints[-1]

do {
        $Job_Failover = Get-AzRecoveryServicesAsrJob -Job $Job_Failover;
        sleep 30;
} while (($Job_Failover.State -eq "InProgress") -or ($JobFailover.State -eq "NotStarted"))

$Job_Failover.State
Succeeded

當容錯移轉作業成功時,您可以認可容錯移轉作業。

$CommitFailoverJOb = Start-AzRecoveryServicesAsrCommitFailoverJob -ReplicationProtectedItem $ReplicationProtectedItem

Get-AzRecoveryServicesAsrJob -Job $CommitFailoverJOb
Name             : 58afc2b7-5cfe-4da9-83b2-6df358c6e4ff
ID               : /Subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/a2ademorecoveryrg/providers/Microsoft.RecoveryServices/vaults/a2aDemoR
                   ecoveryVault/replicationJobs/58afc2b7-5cfe-4da9-83b2-6df358c6e4ff
Type             : Microsoft.RecoveryServices/vaults/replicationJobs
JobType          : CommitFailover
DisplayName      : Commit
ClientRequestId  : 10a95d6c-359e-4603-b7d9-b7ee3317ce94 ActivityId: 8751ada4-fc42-4238-8de6-a82618408fcf
State            : Succeeded
StateDescription : Completed
StartTime        : 4/25/2018 4:50:58 AM
EndTime          : 4/25/2018 4:51:01 AM
TargetObjectId   : ce86206c-bd78-53b4-b004-39b722c1ac3a
TargetObjectType : ProtectionEntity
TargetObjectName : azuredemovm
AllowedActions   :
Tasks            : {Prerequisite check, Commit}
Errors           : {}

重新保護並容錯回復至來源區域

在容錯移轉之後,當您準備好回到原始區域時,請使用 Update-AzRecoveryServicesAsrProtectionDirection Cmdlet 啟動複寫受保護項目的反向複寫。

#Create Cache storage account for replication logs in the primary region
$WestUSCacheStorageAccount = New-AzStorageAccount -Name "a2acachestoragewestus" -ResourceGroupName "A2AdemoRG" -Location 'West US' -SkuName Standard_LRS -Kind Storage
#Use the recovery protection container, new cache storage account in West US and the source region VM resource group
Update-AzRecoveryServicesAsrProtectionDirection -ReplicationProtectedItem $ReplicationProtectedItem -AzureToAzure
-ProtectionContainerMapping $WusToEusPCMapping -LogStorageAccountId $WestUSCacheStorageAccount.Id -RecoveryResourceGroupID $sourceVMResourcegroup.ResourceId

重新保護完成之後,您可以反向進行容錯移轉 (美國西部至美國東部),然後容錯回復至來源區域。

停用複寫

您可以使用 Remove-AzRecoveryServicesAsrReplicationProtectedItem Cmdlet 停用複寫。

Remove-AzRecoveryServicesAsrReplicationProtectedItem -ReplicationProtectedItem $ReplicationProtectedItem

下一步

請參閱 Azure Site Recovery PowerShell 參考,以了解如何使用 PowerShell 執行其他工作,例如建立復原計劃和測試復原計劃的容錯移轉。