Set up disaster recovery of Hyper-V VMs to a secondary site by using PowerShell (Resource Manager)
This article shows how to automate the steps for replication of Hyper-V VMs in System Center Virtual Machine Manager clouds to a Virtual Machine Manager cloud in a secondary on-premises site by using Azure Site Recovery.
Note
This article uses the Azure Az PowerShell module, which is the recommended PowerShell module for interacting with Azure. To get started with the Az PowerShell module, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
Prerequisites
- Review the scenario architecture and components.
- Review the support requirements for all components.
- Make sure that Virtual Machine Manager servers and Hyper-V hosts comply with support requirements.
- Check that the VMs you want to replicate comply with replicated machine support.
Prepare for network mapping
Network mapping maps between on-premises Virtual Machine Manager VM networks in source and target clouds. Mapping does the following:
- Connects VMs to appropriate target VM networks after failover.
- Optimally places replica VMs on target Hyper-V host servers.
- If you don't configure network mapping, replica VMs won't be connected to a VM network after failover.
Prepare Virtual Machine Manager as follows:
- Make sure you have Virtual Machine Manager logical networks on the source and target Virtual Machine Manager servers:
- The logical network on the source server should be associated with the source cloud in which Hyper-V hosts are located.
- The logical network on the target server should be associated with the target cloud.
- Make sure you have VM networks on the source and target Virtual Machine Manager servers. VM networks should be linked to the logical network in each location.
- Connect VMs on the source Hyper-V hosts to the source VM network.
Prepare for PowerShell
Make sure you have Azure PowerShell ready to go:
- If you already use PowerShell, upgrade to version 0.8.10 or later. Learn more about how to set up PowerShell.
- After you set up and configure PowerShell, review the service cmdlets.
- To learn more about how to use parameter values, inputs, and outputs in PowerShell, read the Get started guide.
Set up a subscription
From PowerShell, sign in to your Azure account.
$UserName = "<user@live.com>" $Password = "<password>" $SecurePassword = ConvertTo-SecureString -AsPlainText $Password -Force $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword Connect-AzAccount #-Credential $CredRetrieve a list of your subscriptions, with the subscription IDs. Note the ID of the subscription in which you want to create the Recovery Services vault.
Get-AzSubscriptionSet the subscription for the vault.
Set-AzContext –SubscriptionID <subscriptionId>
Create a Recovery Services vault
Create an Azure Resource Manager resource group if you don't have one.
New-AzResourceGroup -Name #ResourceGroupName -Location #locationCreate a new Recovery Services vault. Save the vault object in a variable to be used later.
$vault = New-AzRecoveryServicesVault -Name #vaultname -ResourceGroupName #ResourceGroupName -Location #locationYou can retrieve the vault object after you create it by using the
Get-AzRecoveryServicesVaultcmdlet.
Set the vault context
Retrieve an existing vault.
$vault = Get-AzRecoveryServicesVault -Name #vaultnameSet the vault context.
Set-AzRecoveryServicesAsrVaultContext -Vault $vault
Install the Site Recovery provider
On the Virtual Machine Manager machine, create a directory by running the following command:
New-Item -Path C:\ASR -ItemType DirectoryExtract the files by using the downloaded provider setup file.
pushd C:\ASR\ .\AzureSiteRecoveryProvider.exe /x:. /qInstall the provider, and wait for installation to finish.
.\SetupDr.exe /i $installationRegPath = "HKLM:\Software\Microsoft\Microsoft System Center Virtual Machine Manager Server\DRAdapter" do { $isNotInstalled = $true; if(Test-Path $installationRegPath) { $isNotInstalled = $false; } }While($isNotInstalled)Register the server in the vault.
$BinPath = $env:SystemDrive+"\Program Files\Microsoft System Center 2012 R2\Virtual Machine Manager\bin" pushd $BinPath $encryptionFilePath = "C:\temp\".\DRConfigurator.exe /r /Credentials $VaultSettingFilePath /vmmfriendlyname $env:COMPUTERNAME /dataencryptionenabled $encryptionFilePath /startvmmservice
Create and associate a replication policy
Create a replication policy, in this case for Hyper-V 2012 R2, as follows:
$ReplicationFrequencyInSeconds = "300"; #options are 30,300,900 $PolicyName = “replicapolicy” $RepProvider = HyperVReplica2012R2 $Recoverypoints = 24 #specify the number of hours to retain recovery points $AppConsistentSnapshotFrequency = 4 #specify the frequency (in hours) at which app consistent snapshots are taken $AuthMode = "Kerberos" #options are "Kerberos" or "Certificate" $AuthPort = "8083" #specify the port number that will be used for replication traffic on Hyper-V hosts $InitialRepMethod = "Online" #options are "Online" or "Offline" $policyresult = New-AzRecoveryServicesAsrPolicy -Name $policyname -ReplicationProvider $RepProvider -ReplicationFrequencyInSeconds $Replicationfrequencyinseconds -NumberOfRecoveryPointsToRetain $recoverypoints -ApplicationConsistentSnapshotFrequencyInHours $AppConsistentSnapshotFrequency -Authentication $AuthMode -ReplicationPort $AuthPort -ReplicationMethod $InitialRepMethodNote
The Virtual Machine Manager cloud can contain Hyper-V hosts running different versions of Windows Server, but the replication policy is for a specific version of an operating system. If you have different hosts running on different operating systems, create separate replication policies for each system. For example, if you have five hosts running on Windows Server 2012 and three hosts running on Windows Server 2012 R2, create two replication policies. You create one for each type of operating system.
Retrieve the primary protection container (primary Virtual Machine Manager cloud) and recovery protection container (recovery Virtual Machine Manager cloud).
$PrimaryCloud = "testprimarycloud" $primaryprotectionContainer = Get-AzRecoveryServicesAsrProtectionContainer -FriendlyName $PrimaryCloud; $RecoveryCloud = "testrecoverycloud" $recoveryprotectionContainer = Get-AzRecoveryServicesAsrProtectionContainer -FriendlyName $RecoveryCloud;Retrieve the replication policy you created by using the friendly name.
$policy = Get-AzRecoveryServicesAsrPolicy -FriendlyName $policynameStart the association of the protection container (Virtual Machine Manager cloud) with the replication policy.
$associationJob = New-AzRecoveryServicesAsrProtectionContainerMapping -Policy $Policy -PrimaryProtectionContainer $primaryprotectionContainer -RecoveryProtectionContainer $recoveryprotectionContainerWait for the policy association job to finish. To check if the job is finished, use the following PowerShell snippet:
$job = Get-AzRecoveryServicesAsrJob -Job $associationJob if($job -eq $null -or $job.StateDescription -ne "Completed") { $isJobLeftForProcessing = $true; }After the job finishes processing, run the following command:
if($isJobLeftForProcessing) { Start-Sleep -Seconds 60 } While($isJobLeftForProcessing)
To check the completion of the operation, follow the steps in Monitor activity.
Configure network mapping
Use this command to retrieve servers for the current vault. The command stores the Site Recovery servers in the
$Serversarray variable.$Servers = Get-AzRecoveryServicesAsrFabricRun this command to retrieve the networks for the source Virtual Machine Manager server and the target Virtual Machine Manager server.
$PrimaryNetworks = Get-AzRecoveryServicesAsrNetwork -Fabric $Servers[0] $RecoveryNetworks = Get-AzRecoveryServicesAsrNetwork -Fabric $Servers[1]Note
The source Virtual Machine Manager server can be the first or second one in the server array. Check Virtual Machine Manager server names, and retrieve the networks appropriately.
This cmdlet creates a mapping between the primary network and the recovery network. It specifies the primary network as the first element of
$PrimaryNetworks. It specifies the recovery network as the first element of$RecoveryNetworks.New-AzRecoveryServicesAsrNetworkMapping -PrimaryNetwork $PrimaryNetworks[0] -RecoveryNetwork $RecoveryNetworks[0]
Enable protection for VMs
After the servers, clouds, and networks are configured correctly, enable protection for VMs in the cloud.
To enable protection, run the following command to retrieve the protection container:
$PrimaryProtectionContainer = Get-AzRecoveryServicesAsrProtectionContainer -FriendlyName $PrimaryCloudNameGet the protection entity (VM), as follows:
$protectionEntity = Get-AzRecoveryServicesAsrProtectableItem -FriendlyName $VMName -ProtectionContainer $PrimaryProtectionContainerEnable replication for the VM.
$jobResult = New-AzRecoveryServicesAsrReplicationProtectedItem -ProtectableItem $protectionentity -ProtectionContainerMapping $policy -VmmToVmm
Note
If you wish to replicate to CMK enabled managed disks in Azure, do the following steps using Az PowerShell 3.3.0 onwards:
- Enable failover to managed disks by updating VM properties
- Use the
Get-AzRecoveryServicesAsrReplicationProtectedItemcmdlet to fetch the disk ID for each disk of the protected item - Create a dictionary object using
New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"cmdlet to contain the mapping of disk ID to disk encryption set. These disk encryption sets are to be pre-created by you in the target region. - Update the VM properties using
Set-AzRecoveryServicesAsrReplicationProtectedItemcmdlet by passing the dictionary object in DiskIdToDiskEncryptionSetMap parameter.
Run a test failover
To test your deployment, run a test failover for a single virtual machine. You also can create a recovery plan that contains multiple VMs and run a test failover for the plan. Test failover simulates your failover and recovery mechanism in an isolated network.
Retrieve the VM into which VMs will fail over.
$Servers = Get-AzRecoveryServicesASRFabric $RecoveryNetworks = Get-AzRecoveryServicesAsrNetwork -Name $Servers[1]Perform a test failover.
For a single VM:
$protectionEntity = Get-AzRecoveryServicesAsrProtectableItem -FriendlyName $VMName -ProtectionContainer $PrimaryprotectionContainer $jobIDResult = Start-AzRecoveryServicesAsrTestFailoverJob -Direction PrimaryToRecovery -ReplicationProtectedItem $protectionEntity -VMNetwork $RecoveryNetworks[1]For a recovery plan:
$recoveryplanname = "test-recovery-plan" $recoveryplan = Get-AzRecoveryServicesAsrRecoveryPlan -FriendlyName $recoveryplanname $jobIDResult = Start-AzRecoveryServicesAsrTestFailoverJob -Direction PrimaryToRecovery -RecoveryPlan $recoveryplan -VMNetwork $RecoveryNetworks[1]
To check the completion of the operation, follow the steps in Monitor activity.
Run planned and unplanned failovers
Perform a planned failover.
For a single VM:
$protectionEntity = Get-AzRecoveryServicesAsrProtectableItem -Name $VMName -ProtectionContainer $PrimaryprotectionContainer $jobIDResult = Start-AzRecoveryServicesAsrPlannedFailoverJob -Direction PrimaryToRecovery -ReplicationProtectedItem $protectionEntityFor a recovery plan:
$recoveryplanname = "test-recovery-plan" $recoveryplan = Get-AzRecoveryServicesAsrRecoveryPlan -FriendlyName $recoveryplanname $jobIDResult = Start-AzRecoveryServicesAsrPlannedFailoverJob -Direction PrimaryToRecovery -RecoveryPlan $recoveryplanPerform an unplanned failover.
For a single VM:
$protectionEntity = Get-AzRecoveryServicesAsrProtectableItem -Name $VMName -ProtectionContainer $PrimaryprotectionContainer $jobIDResult = Start-AzRecoveryServicesAsrUnplannedFailoverJob -Direction PrimaryToRecovery -ReplicationProtectedItem $protectionEntityFor a recovery plan:
$recoveryplanname = "test-recovery-plan" $recoveryplan = Get-AzRecoveryServicesAsrRecoveryPlan -FriendlyName $recoveryplanname $jobIDResult = Start-AzRecoveryServicesAsrUnplannedFailoverJob -Direction PrimaryToRecovery -RecoveryPlan $recoveryplan
Monitor activity
Use the following commands to monitor failover activity. Wait for the processing to finish in between jobs.
Do
{
$job = Get-AzRecoveryServicesAsrJob -TargetObjectId $associationJob.JobId;
Write-Host "Job State:{0}, StateDescription:{1}" -f Job.State, $job.StateDescription;
if($job -eq $null -or $job.StateDescription -ne "Completed")
{
$isJobLeftForProcessing = $true;
}
if($isJobLeftForProcessing)
{
Start-Sleep -Seconds 60
}
}While($isJobLeftForProcessing)
Next steps
Learn more about Site Recovery with Resource Manager PowerShell cmdlets.
Povratne informacije
Pošalјite i prikažite povratne informacije za