在本文中,您將瞭解如何使用 Azure PowerShell 設定及測試 Azure 虛擬機的災害復原。
您將瞭解如何:
- 建立復原服務保存庫。
- 設定 PowerShell 工作階段的保存庫內容。
- 讓保存庫準備好開始複寫 Azure 虛擬機器。
- 建立網路映射。
- 建立作為複寫虛擬機器目的地的儲存體帳戶。
- 將 Azure 虛擬機複寫至復原區域以進行災害復原。
- 執行測試容錯移轉、驗證和清除測試容錯移轉。
- 容錯移轉至復原區域。
備註
並非所有可透過入口網站取得的案例功能都可以透過 Azure PowerShell 取得。 Azure PowerShell 目前不支援的一些案例功能包括:
- 可以指定虛擬機中的所有磁碟都應該複製,而不需要逐一明確指定虛擬機的每個磁碟。
備註
建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 開始使用前,請參閱安裝 Azure PowerShell。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az。
先決條件
在開始之前:
- 請確定您已瞭解 案例架構和元件。
- 檢閱所有元件的支援需求。
- 您有 Azure PowerShell
Az模組。 如果您需要安裝或升級 Azure PowerShell,請遵循 本指南來安裝和設定 Azure PowerShell。
登入您的 Microsoft Azure 訂用帳戶
使用 Cmdlet 登入您的 Azure 訂用 Connect-AzAccount 帳戶。
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"
$recoveryProtectionContainer = 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 類型的儲存帳戶,然後使用它作為快取儲存帳戶。 適用於高效能 SSD v2/Ultra 磁碟的 Azure Site Recovery 僅支援在資料處理頻繁(高活動量)的情況下使用。 使用 SkuName Premium_LRS 和 Kind BlockBlobStorage 來啟用高周轉率。 您可以選擇為虛擬機的不同磁碟指定不同的快取記憶體帳戶,但並非必要。 如果您使用不同的快取儲存體帳戶,請確定其屬於相同類型 (標準或進階區塊 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
if ($RecoveryReplicaDiskAccountType -in @("PremiumV2_LRS", "Ultra_LRS")) {
$RecoveryReplicaDiskAccountType = "Premium_LRS"
}
$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 : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
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 : aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb
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 建立復原計劃和測試復原方案的故障轉移。