Share via


Herstel na noodgevallen van virtuele Azure-machines instellen met Azure PowerShell

In dit artikel leest u hoe u herstel na noodgevallen instelt en test voor virtuele Azure-machines met behulp van Azure PowerShell.

U leert het volgende:

  • Maak een Recovery Services-kluis.
  • Stel de kluiscontext in voor de PowerShell-sessie.
  • Bereid de kluis voor om virtuele Azure-machines te repliceren.
  • Netwerktoewijzingen maken.
  • Maak opslagaccounts om virtuele machines naar te repliceren.
  • Virtuele Azure-machines repliceren naar een herstelregio voor herstel na noodgevallen.
  • Voer een testfailover uit, valideer en opschoontestfailover.
  • Voer een failover uit naar de herstelregio.

Notitie

Niet alle scenariomogelijkheden die beschikbaar zijn via de portal, zijn mogelijk beschikbaar via Azure PowerShell. Enkele scenariomogelijkheden die momenteel niet worden ondersteund via Azure PowerShell, zijn:

  • De mogelijkheid om op te geven dat alle schijven in een virtuele machine moeten worden gerepliceerd zonder dat u expliciet elke schijf van de virtuele machine hoeft op te geven.

Notitie

Het wordt aanbevolen de Azure Az PowerShell-module te gebruiken om te communiceren met Azure. Zie Azure PowerShell installeren om aan de slag te gaan. Raadpleeg Azure PowerShell migreren van AzureRM naar Az om te leren hoe u naar de Azure PowerShell-module migreert.

Vereisten

Voordat u begint:

Aanmelden bij uw Microsoft Azure-abonnement

Meld u aan bij uw Azure-abonnement met de cmdlet Connect-AzAccount.

Connect-AzAccount

Selecteer uw Azure-abonnement. Gebruik de cmdlet Get-AzSubscription om de lijst met Azure-abonnementen weer te geven waartoe u toegang hebt. Selecteer het Azure-abonnement waarmee u wilt werken met behulp van de Set-AzContext cmdlet.

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

Details ophalen van de virtuele machine die moet worden gerepliceerd

In dit artikel wordt een virtuele machine in de regio VS - oost gerepliceerd naar en hersteld in de regio VS - west 2. De virtuele machine die wordt gerepliceerd, heeft een besturingssysteemschijf en één gegevensschijf. De naam van de virtuele machine die in het voorbeeld wordt gebruikt, is 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}

Schijfgegevens ophalen voor de schijven van de virtuele machine. Schijfgegevens worden later gebruikt bij het starten van replicatie voor de virtuele machine.

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

Een Recovery Services-kluis maken

Maak een resourcegroep waarin de Recovery Services-kluis moet worden gemaakt.

Belangrijk

  • De Recovery Services-kluis en de virtuele machines die worden beveiligd, moeten zich op verschillende Azure-locaties bevinden.
  • De resourcegroep van de Recovery Services-kluis en de virtuele machines die worden beveiligd, moeten zich op verschillende Azure-locaties bevinden.
  • De Recovery Services-kluis en de resourcegroep waartoe deze behoort, kunnen zich op dezelfde Azure-locatie bevinden.

In het voorbeeld in dit artikel bevindt de virtuele machine die wordt beveiligd zich in de regio VS - oost. De herstelregio die is geselecteerd voor herstel na noodgevallen is de regio VS - west 2. De Recovery Services-kluis en de resourcegroep van de kluis bevinden zich beide in de herstelregio VS - west 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

Maak een Recovery Services-kluis. In dit voorbeeld wordt een Recovery Services-kluis met de naam a2aDemoRecoveryVault gemaakt in de regio VS - west 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

De kluiscontext instellen

Stel de kluiscontext in voor gebruik in de PowerShell-sessie. Nadat de kluiscontext is ingesteld, worden Azure Site Recovery-bewerkingen in de PowerShell-sessie uitgevoerd in de context van de geselecteerde kluis.

#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

Voor een Azure-naar-Azure-migratie kunt u de kluiscontext instellen op de zojuist gemaakte kluis:

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

De kluis voorbereiden om virtuele Azure-machines te repliceren

Een Site Recovery-infrastructuurobject maken dat de primaire (bron) regio vertegenwoordigt

Het fabric-object in de kluis vertegenwoordigt een Azure-regio. Het primaire fabric-object wordt gemaakt om de Azure-regio weer te geven waartoe virtuele machines die worden beveiligd met de kluis. In het voorbeeld in dit artikel bevindt de virtuele machine die wordt beveiligd zich in de regio VS - oost.

  • Er kan slechts één fabric-object per regio worden gemaakt.
  • Als u site recovery-replicatie eerder hebt ingeschakeld voor een virtuele machine in Azure Portal, maakt Site Recovery automatisch een infrastructuurobject. Als er een fabric-object bestaat voor een regio, kunt u geen nieuw object maken.

Voordat u begint, moet u begrijpen dat Site Recovery-bewerkingen asynchroon worden uitgevoerd. Wanneer u een bewerking start, wordt een Azure Site Recovery-taak verzonden en wordt er een object voor het bijhouden van taken geretourneerd. Gebruik het object voor het bijhouden van taken om de meest recente status voor de taak (Get-AzRecoveryServicesAsrJob) op te halen en om de status van de bewerking te controleren.

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

Als virtuele machines uit meerdere Azure-regio's worden beveiligd met dezelfde kluis, maakt u één fabric-object voor elke Azure-bronregio.

Een Site Recovery-infrastructuurobject maken dat de herstelregio vertegenwoordigt

Het recovery fabric-object vertegenwoordigt de Azure-herstellocatie. Als er een failover is, worden virtuele machines gerepliceerd en hersteld naar de herstelregio die wordt vertegenwoordigd door de herstelinfrastructuur. De azure-herstelregio die in dit voorbeeld wordt gebruikt, is VS - west 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"

Een Site Recovery-beveiligingscontainer maken in de primaire infrastructuur

De beveiligingscontainer is een container die wordt gebruikt om gerepliceerde items in een infrastructuur te groeperen.

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

Een Site Recovery-beveiligingscontainer maken in de herstelinfrastructuur

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

Fabric en container maken bij het inschakelen van zone-naar-zonereplicatie

Wanneer u zone-naar-zonereplicatie inschakelt, wordt slechts één infrastructuur gemaakt. Maar er zijn twee containers. Ervan uitgaande dat de regio Europa - west is, gebruikt u de volgende opdrachten om de primaire en beveiligingscontainers op te halen -

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

Een replicatiebeleid maken

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

Een beveiligingscontainertoewijzing maken tussen de primaire en herstelbeveiligingscontainer

Een beveiligingscontainertoewijzing wijst de primaire beveiligingscontainer toe aan een herstelbeveiligingscontainer en een replicatiebeleid. Maak één toewijzing voor elk replicatiebeleid dat u gebruikt om virtuele machines tussen een beveiligingscontainerpaar te repliceren.

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

Maken van beveiligingscontainertoewijzing bij het inschakelen van zone-naar-zonereplicatie

Wanneer u zone-naar-zonereplicatie inschakelt, gebruikt u de onderstaande opdracht om beveiligingscontainertoewijzing te maken. Ervan uitgaande dat de regio Europa - west is, is de opdracht -

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

Een beveiligingscontainertoewijzing maken voor failback (omgekeerde replicatie na een failover)

Wanneer u na een failover klaar bent om de virtuele machine waarvoor een failover is uitgevoerd, weer terug te brengen naar de oorspronkelijke Azure-regio, voert u een failback uit. Als u een failback wilt uitvoeren, wordt de virtuele machine waarvoor een failover is uitgevoerd, omgekeerd van de regio waarvoor een failover is uitgevoerd naar de oorspronkelijke regio. Voor omgekeerde replicatie zijn de rollen van de oorspronkelijke regio en de herstelregioswitch. De oorspronkelijke regio wordt nu de nieuwe herstelregio en wat oorspronkelijk de herstelregio was, wordt nu de primaire regio. De beveiligingscontainertoewijzing voor omgekeerde replicatie vertegenwoordigt de geschakelde rollen van de oorspronkelijke en herstelregio's.

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

Cacheopslagaccount en doelopslagaccount maken

Een cacheopslagaccount is een standaardopslagaccount in dezelfde Azure-regio als de virtuele machine die wordt gerepliceerd. Het cacheopslagaccount wordt gebruikt om replicatiewijzigingen tijdelijk vast te houden, voordat de wijzigingen worden verplaatst naar de Azure-herstelregio. Ondersteuning voor hoog verloop is ook beschikbaar in Azure Site Recovery om hogere verlooplimieten te krijgen. Als u deze functie wilt gebruiken, maakt u een Premium Blok-blobtype opslagaccounts en gebruikt u deze vervolgens als het cacheopslagaccount. U kunt ervoor kiezen om verschillende cacheopslagaccounts op te geven voor de verschillende schijven van een virtuele machine. Als u verschillende cacheopslagaccounts gebruikt, moet u ervoor zorgen dat deze van hetzelfde type zijn (Standard of Premium Blok-blobs). Zie Herstel na noodgevallen voor Azure-VM's - Ondersteuning voor hoog verloop voor meer informatie.

#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

Voor virtuele machines die geen beheerde schijven gebruiken, is het doelopslagaccount het opslagaccount in de herstelregio waarnaar schijven van de virtuele machine worden gerepliceerd. Het doelopslagaccount kan een standaardopslagaccount of een Premium-opslagaccount zijn. Selecteer het type opslagaccount dat is vereist op basis van de gegevenswijzigingssnelheid (IO-schrijfsnelheid) voor de schijven en de ondersteunde verlooplimieten van Azure Site Recovery voor het opslagtype.

#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

Netwerktoewijzingen maken

Een netwerktoewijzing wijst virtuele netwerken in de primaire regio toe aan virtuele netwerken in de herstelregio. De netwerktoewijzing specificeert het virtuele Azure-netwerk in de herstelregio, waarnaar een virtuele machine in het primaire virtuele netwerk een failover moet uitvoeren. Eén virtueel Azure-netwerk kan worden toegewezen aan slechts één virtueel Azure-netwerk in een herstelregio.

  • Maak een virtueel Azure-netwerk in de herstelregio om een failover naar:

      #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
    
  • Haal het primaire virtuele netwerk op. Het VNet waarmee de virtuele machine is verbonden:

      #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("\","/")
    
  • Maak netwerktoewijzing tussen het primaire virtuele netwerk en het virtuele herstelnetwerk:

      #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
    
  • Netwerktoewijzing maken voor de omgekeerde richting (failback):

    #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
    

Virtuele Azure-machine repliceren

Repliceer de virtuele Azure-machine met beheerde schijven.

#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

Repliceer de virtuele Azure-machine met niet-beheerde schijven.

#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

Zodra de replicatiebewerking is gestart, worden de gegevens van de virtuele machine gerepliceerd naar de herstelregio.

Het replicatieproces begint met het seeden van een kopie van de replicerende schijven van de virtuele machine in de herstelregio. Deze fase wordt de initiële replicatiefase genoemd.

Nadat de initiële replicatie is voltooid, wordt de replicatie verplaatst naar de differentiële synchronisatiefase. Op dit moment wordt de virtuele machine beveiligd en kan er een testfailoverbewerking worden uitgevoerd. De replicatiestatus van het gerepliceerde item dat de virtuele machine vertegenwoordigt, gaat naar de beveiligde status nadat de initiële replicatie is voltooid.

Bewaak de replicatiestatus en replicatiestatus voor de virtuele machine door details op te halen van het item dat overeenkomt met de replicatiebeveiliging.

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

Een testfailover uitvoeren, valideren en opschonen van testfailover

Nadat de replicatie voor de virtuele machine een beveiligde status heeft bereikt, kan een testfailoverbewerking worden uitgevoerd op de virtuele machine (op het met replicatie beveiligde item van de virtuele machine).

#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

Een testfailover uitvoeren.

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

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

Wacht tot de testfailoverbewerking is voltooid.

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

Nadat de testfailovertaak is voltooid, kunt u verbinding maken met de virtuele machine waarvoor de test is mislukt en de testfailover valideren.

Zodra het testen is voltooid op de virtuele machine waarvoor de test is uitgevoerd, schoont u de testkopie op door de testfailoverbewerking te starten. Met deze bewerking verwijdert u de testkopie van de virtuele machine die is gemaakt door de testfailover.

$Job_TFOCleanup = Start-AzRecoveryServicesAsrTestFailoverCleanupJob -ReplicationProtectedItem $ReplicationProtectedItem

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

Failover naar Azure

Voer een failover van de virtuele machine uit naar een specifiek herstelpunt.

$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

Wanneer de failovertaak is geslaagd, kunt u de failoverbewerking doorvoeren.

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

Opnieuw beveiligen en failback uitvoeren naar de bronregio

Wanneer u na een failover terug wilt gaan naar de oorspronkelijke regio, start u omgekeerde replicatie voor het met replicatie beveiligde item met behulp van de 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

Nadat de beveiliging is voltooid, kunt u een failover uitvoeren in de omgekeerde richting, VS - west naar VS - oost en een failback naar de bronregio uitvoeren.

Replicatie uitschakelen

U kunt replicatie uitschakelen met de Remove-AzRecoveryServicesAsrReplicationProtectedItem cmdlet.

Remove-AzRecoveryServicesAsrReplicationProtectedItem -ReplicationProtectedItem $ReplicationProtectedItem

Volgende stappen

Bekijk de Naslaginformatie over Azure Site Recovery PowerShell voor meer informatie over hoe u andere taken kunt uitvoeren, zoals het maken van herstelplannen en het testen van failover van herstelplannen met PowerShell.