Menyiapkan pemulihan bencana untuk mesin virtual Azure menggunakan Azure PowerShell

Dalam artikel ini, Anda akan melihat cara menyiapkan dan menguji pemulihan bencana untuk komputer virtual Azure menggunakan Azure PowerShell.

Anda akan mempelajari cara untuk:

  • Buat vault Layanan Pemulihan.
  • Mengatur konteks brankas untuk sesi PowerShell.
  • Menyiapkan brankas untuk mulai mereplikasi komputer virtual Azure.
  • Membuat pemetaan jaringan.
  • Membuat akun penyimpanan untuk mereplikasi komputer virtual.
  • Mereplikasi komputer virtual Azure ke wilayah pemulihan untuk pemulihan bencana.
  • Jalankan pengujian failover, validasi, dan pengujian pembersihan failover.
  • Failover ke wilayah pemulihan.

Catatan

Tidak semua kemampuan skenario yang tersedia melalui portal mungkin tersedia melalui Azure PowerShell. Beberapa kapabilitas skenario yang saat ini tidak didukung melalui Azure PowerShell adalah:

  • Kemampuan untuk menentukan bahwa semua disk dalam komputer virtual harus direplikasi tanpa harus secara eksplisit menentukan setiap disk komputer virtual.

Catatan

Sebaiknya Anda menggunakan modul Azure Az PowerShell untuk berinteraksi dengan Azure. Lihat Menginstal Azure PowerShell untuk memulai. Untuk mempelajari cara bermigrasi ke modul Az PowerShell, lihat Memigrasikan Azure PowerShell dari AzureRM ke Az.

Prasyarat

Sebelum memulai:

Masuk ke langganan Microsoft Azure Anda

Masuk ke langganan Azure Anda dengan Connect-AzAccount cmdlet.

Connect-AzAccount

Pilih langganan Azure Anda. Gunakan Get-AzSubscription cmdlet untuk mendapatkan daftar langganan Azure yang dapat Anda akses. Pilih langganan Azure untuk dikerjakan menggunakan Set-AzContext cmdlet.

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

Dapatkan detail komputer virtual yang akan direplikasi

Dalam artikel ini, komputer virtual di wilayah AS Timur direplikasi dan dipulihkan di wilayah Barat AS 2. Komputer virtual yang direplikasi memiliki disk OS dan disk data tunggal. Nama komputer virtual yang digunakan dalam contoh adalah 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}

Dapatkan detil disk untuk disk komputer virtual. Detil cakram akan digunakan nanti ketika memulai replikasi untuk komputer virtual.

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

Buat brankas Recovery Services

Buat grup sumber daya untuk membuat brankas Layanan Pemulihan.

Penting

  • Brankas layanan Pemulihan dan komputer virtual yang dilindungi, harus berada di lokasi Azure yang berbeda.
  • Grup sumber daya brankas layanan Pemulihan, dan komputer virtual yang dilindungi, harus berada di lokasi Azure yang berbeda.
  • Brankas layanan Pemulihan, dan grup sumber daya tempatnya berada, dapat berada di lokasi Azure yang sama.

Dalam contoh dalam artikel ini, komputer virtual yang dilindungi berada di wilayah AS Timur. Wilayah pemulihan yang dipilih untuk pemulihan bencana adalah wilayah Barat AS 2. Brankas layanan pemulihan, dan grup sumber daya brankas, keduanya berada di wilayah pemulihan, US Barat 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

Buat kubah layanan Pemulihan. Dalam contoh ini, brankas Layanan Pemulihan bernamaa2aDemoRecoveryVault dibuat di wilayah Barat AS 2.

#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

Mengatur konteks vault

Kelola konteks brankas untuk digunakan dalam sesi PowerShell. Setelah konteks brankas diatur, operasi Azure Site Recovery dalam sesi PowerShell dilakukan dalam konteks brankas yang dipilih.

#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

Untuk migrasi Azure-ke-Azure, Anda bisa mengatur konteks brankas ke brankas yang baru dibuat:

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

Menyiapkan brankas untuk mulai mereplikasi komputer virtual Azure

Membuat objek fabric Site Recovery untuk mewakili wilayah utama (sumber)

Objek fabric dalam brankas mewakili wilayah Azure. Objek fabric utama dibuat untuk mewakili wilayah Azure tempat komputer virtual dilindungi ke brankas. Dalam contoh dalam artikel ini, komputer virtual yang dilindungi berada di wilayah AS Timur.

  • Hanya satu objek fabric yang dapat dibuat per wilayah.
  • Jika sebelumnya Anda telah mengaktifkan replikasi Site Recovery untuk komputer virtual di portal Microsoft Azure, Site Recovery akan membuat objek fabric secara otomatis. Jika objek fabric ada untuk suatu wilayah, Anda tidak dapat membuat objek baru.

Sebelum Anda mulai, pahami bahwa operasi Site Recovery dijalankan secara asinkron. Saat Anda memulai operasi, tugas Azure Site Recovery dikirimkan dan objek penelusuran pekerjaan dikembalikan. Gunakan objek penelusuran pekerjaan untuk mendapatkan status terbaru untuk pekerjaan ( Get-AzRecoveryServicesAsrJob ), dan untuk memantau status operasi.

#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"

Jika komputer virtual dari beberapa wilayah Azure dilindungi ke brankas yang sama, buat satu objek fabric untuk setiap sumber data wilayah Azure.

Membuat objek fabric Site Recovery untuk mewakili wilayah pemulihan

Objek fabric pemulihan mewakili pemulihan lokasi Azure. Jika ada failover, komputer virtual direplikasi dan dipulihkan ke wilayah pemulihan yang diwakili oleh fabric pemulihan. Wilayah pemulihan Azure yang digunakan dalam contoh ini adalah Barat AS 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"

Membuat kontainer perlindungan Site Recovery di fabric utama

Kontainer perlindungan adalah wadah yang digunakan untuk mengelompokkan item yang direplikasi dalam fabric.

#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"

Membuat kontainer perlindungan Site Recovery di fabric pemulihan

#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"

Pembuatan fabric dan kontainer saat mengaktifkan replikasi zona ke zona

Ketika mengaktifkan replikasi zona ke zona, hanya satu fabric yang akan dibuat. Tapi akan ada dua kontainer. Dengan asumsi bahwa wilayah ini adalah Eropa Barat, gunakan perintah berikut untuk mendapatkan kontainer utama dan perlindungan -

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

Buat kebijakan replikasi

#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"

Membuat pemetaan kontainer perlindungan antara kontainer perlindungan primer dan pemulihan

Pemetaan kontainer perlindungan memetakan kontainer perlindungan utama dengan kontainer perlindungan pemulihan dan kebijakan replikasi. Buat satu pemetaan untuk setiap kebijakan replikasi yang akan Anda gunakan untuk mereplikasi komputer virtual di antara pasangan kontainer perlindungan.

#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"

Proteksi pembuatan pemetaan kontainer saat mengaktifkan replikasi zona ke zona

Saat mengaktifkan replikasi zona ke zona, gunakan perintah di bawah ini untuk membuat pemetaan kontainer perlindungan. Dengan asumsi bahwa wilayah ini adalah Eropa Barat, perintah akan berupa-

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

Membuat pemetaan kontainer perlindungan untuk failback (replikasi terbalik setelah failover)

Setelah failover, ketika Anda siap untuk membawa komputer virtual yang gagal kembali ke wilayah Azure asli, Anda melakukan failback. Untuk failback, komputer virtual yang gagal direplikasi terbalik dari wilayah yang gagal ke wilayah asli. Untuk replikasi terbalik, peran wilayah asli, dan saklar wilayah pemulihan. Wilayah asal sekarang menjadi wilayah pemulihan baru, dan apa yang awalnya wilayah pemulihan sekarang menjadi wilayah utama. Pemetaan kontainer perlindungan untuk replikasi terbalik mewakili peran yang dialihkan dari wilayah asli dan pemulihan.

#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"

Membuat akun penyimpanan cache dan akun penyimpanan target

Akun penyimpanan cache adalah akun penyimpanan standar di wilayah Azure yang sama dengan komputer virtual yang direplikasi. Akun penyimpanan cache digunakan untuk menahan perubahan replikasi untuk sementara, sebelum perubahan dipindahkan ke wilayah pemulihan Azure. Dukungan churn tinggi juga tersedia di Azure Site Recovery untuk mendapatkan batas churn yang lebih tinggi. Untuk menggunakan fitur ini, buat jenis Blob Blok Premium dari akun penyimpanan lalu gunakan sebagai akun penyimpanan cache. Anda dapat memilih (tetapi tidak perlu) untuk menentukan akun penyimpanan cache untuk disk yang berbeda dari komputer virtual. Jika Anda menggunakan akun penyimpanan cache yang berbeda, pastikan akun tersebut memiliki jenis yang sama (Blob Blok Standar atau Premium). Untuk informasi selengkapnya, lihat Pemulihan Bencana Azure VM - Dukungan Churn Tinggi.

#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

Untuk komputer virtual yang tidak menggunakan disk terkelola, akun penyimpanan target adalah akun penyimpanan di wilayah pemulihan di mana disk komputer virtual direplikasi. Akun penyimpanan target dapat berupa akun penyimpanan standar atau akun penyimpanan premium. Pilih jenis akun penyimpanan yang diperlukan berdasarkan tingkat perubahan data (laju penulisan IO) untuk disk dan Azure Site Recovery yang didukung batas churn untuk jenis penyimpanan.

#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

Membuat pemetaan jaringan

Pemetaan jaringan memetakan jaringan virtual di wilayah utama ke jaringan virtual di wilayah pemulihan. Pemetaan jaringan menentukan jaringan virtual Azure di wilayah pemulihan, bahwa komputer virtual di jaringan virtual utama harus gagal. Satu jaringan virtual Azure hanya dapat dipetakan ke satu jaringan virtual Azure di wilayah pemulihan.

  • Buat jaringan virtual Azure di wilayah pemulihan agar mengalami kegagalan:

      #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
    
  • Ambil jaringan virtual utama. VNet yang terhubung ke komputer virtual:

      #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("\","/")
    
  • Buat pemetaan jaringan antara jaringan virtual utama dan jaringan virtual pemulihan:

      #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
    
  • Buat pemetaan jaringan untuk arah sebaliknya (fail back):

    #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
    

Replikasi komputer virtual Azure

Replikasi komputer virtual Azure dengan disk terkelola.

#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

Replikasi komputer virtual Azure dengan disk terkelola.

#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

Saat operasi mulai-replikasi berhasil, data komputer virtual akan terreplikasi di wilayah pemulihan.

Proses replikasi dimulai dengan menambahkan salinan disk replikasi mesin virtual di wilayah pemulihan. Fase ini disebut fase replikasi awal.

Setelah replikasi awal selesai, replikasi berpindah ke fase sinkronisasi diferensial. Pada titik ini, komputer virtual dilindungi dan operasi uji failover dapat dilakukan di atasnya. Status replikasi item yang direplikasi yang mewakili komputer virtual masuk ke kondisi Terlindungi setelah replikasi awal selesai.

Pantau keadaan replikasi dan kesehatan replikasi untuk komputer virtual dengan mendapatkan detail item yang dilindungi replikasi yang sesuai dengannya.

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

Jalankan pengujian failover, validasi, dan pengujian pembersihan failover

Setelah replikasi untuk komputer virtual telah mencapai keadaan terlindungi, operasi pengujian failover dapat dilakukan pada komputer virtual (pada item yang dilindungi replikasi dari komputer virtual).

#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

Jalankan pengujian failover.

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

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

Tunggu hingga operasi pengujian failover selesai.

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           : {}

Setelah pekerjaan pengujian failover berhasil diselesaikan, Anda dapat terhubung ke pengujian fail over melalui komputer virtual, dan memvalidasi pengujian failover.

Setelah pengujian selesai pada pengujian fail over melalui komputer virtual, bersihkan salinan pengujian dengan memulai operasi pengujian pembersihan failover. Operasi ini menghapus salinan pengujian komputer virtual yang dibuat oleh pengujian failover.

$Job_TFOCleanup = Start-AzRecoveryServicesAsrTestFailoverCleanupJob -ReplicationProtectedItem $ReplicationProtectedItem

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

Failover ke Azure

Failover komputer virtual ke titik pemulihan tertentu.

$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

Ketika pekerjaan failover berhasil, Anda dapat menerapkan operasi failover.

$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           : {}

Melindungi ulang dan fail back ke wilayah sumber

Setelah failover, ketika Anda siap untuk kembali ke wilayah asli, mulai replikasi terbalik untuk item yang dilindungi replikasi menggunakan 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

Setelah perlindungan ulang selesai, Anda dapat fail over ke arah sebaliknya, AS Barat ke AS Timur, dan fail back ke wilayah sumber.

Menonaktifkan replikasi

Anda dapat menonaktifkan replikasi dengan Remove-AzRecoveryServicesAsrReplicationProtectedItem cmdlet.

Remove-AzRecoveryServicesAsrReplicationProtectedItem -ReplicationProtectedItem $ReplicationProtectedItem

Langkah berikutnya

Lihat referensi PowerShell Azure Site Recovery untuk mempelajari bagaimana Anda bisa melakukan pekerjaan lain seperti membuat rencana pemulihan dan menguji kegagalan paket pemulihan dengan PowerShell.