Exercise - Failover and failback using Azure Site Recovery
You can fail over protected resources in three ways: using the portal, using PowerShell, or automating the failover with an Azure Automation runbook.
With all your resources protected, you can run a real failover of your patient-records VM. With the DR drill complete, you'll do the failover with PowerShell and the portal. After completion, you'll be in a better position to recommend which approach your company should use.
In this exercise, you'll complete failover for a VM using PowerShell, and fail back the VM using the Azure portal.
Fail over a VM to a secondary region using PowerShell
Sign into the Azure portal with your own credentials.
Start a Cloud Shell and switch it to PowerShell.
Run the following commands:
$vault = Get-AzRecoveryServicesVault -Name "asr-vault" Set-AzRecoveryServicesAsrVaultContext -Vault $vault $PrimaryFabric = Get-AsrFabric -Name "asr-a2a-default-westus2" $PrimaryProtContainer = Get-ASRProtectionContainer -Fabric $PrimaryFabric $ReplicationProtectedItem = Get-ASRReplicationProtectedItem -ProtectionContainer $PrimaryProtContainer -FriendlyName "patient-records" $RecoveryPoints = Get-ASRRecoveryPoint -ReplicationProtectedItem $ReplicationProtectedItem $Job_Failover = Start-ASRUnplannedFailoverJob -ReplicationProtectedItem $ReplicationProtectedItem -Direction PrimaryToRecovery -RecoveryPoint $RecoveryPoints[-1] do { $Job_Failover = Get-ASRJob -Job $Job_Failover; sleep 30; } while (($Job_Failover.State -eq "InProgress") -or ($JobFailover.State -eq "NotStarted")) $Job_Failover.State $CommitFailoverJob = Start-ASRCommitFailoverJob -ReplicationProtectedItem $ReplicationProtectedItem Get-ASRJob -Job $CommitFailoverJob
These PowerShell commands:
- Store the Azure Site Recovery vault in a variable.
- Set the context for the session to your vault.
- Store the protected patient records from the vault.
- Get a list of all the recovery points.
- Trigger a failover for the latest recovery point.
- Show the failover result.
The failover can take a couple of minutes. While the script is running, leave Cloud Shell open, and go to the asr-vault.
On the left menu pane, under Monitoring, select Site Recovery jobs.
Note
You can view the progress of the failover job at the same time as the script is running.
When the failover completes, select Virtual machines from the Azure home page to check that the patient-record VM has been failed over to the east coast region.
There are now three VMs, with two named patient-records.
Reprotect the VM using PowerShell
After the failover has completed successfully, you can reprotect the VM.
Run the following commands:
$RecoveryFabric = Get-AsrFabric -Name "asr-a2a-default-eastus2" $RecoveryProtContainer = Get-ASRProtectionContainer -Fabric $RecoveryFabric $ProtectionContainerMapping = Get-AzRecoveryServicesAsrProtectionContainerMapping -ProtectionContainer $RecoveryProtContainer -Name eastus2-westus2-24-hour-retention-policy $StorageAccount = New-AzStorageAccount -ResourceGroupName "east-coast-rg" -AccountName "reprotectcache$(Get-Random)" -Location eastus2 -SkuName Standard_GRS $ResourceGroup = Get-AzResourceGroup -Name "west-coast-rg" $ReprotectJob = Update-AzRecoveryServicesAsrProtectionDirection -AzureToAzure -ProtectionContainerMapping $ProtectionContainerMapping -ReplicationProtectedItem $ReplicationProtectedItem -LogStorageAccountId $StorageAccount.ID -RecoveryResourceGroupId $ResourceGroup.ResourceId
These PowerShell commands:
- Set up variables that the
Update-AzRecoveryServicesAsrProtectionDirection
command will use. - Require a storage account to store the reprotected logs and data. This storage needs to be in the same region as the VM that is being protected.
- Start the reprotect job using the last line, and store a reference to it.
- Set up variables that the
Monitor and test using PowerShell
The job to reprotect the VM can take approximately 10 minutes to complete.
You can monitor the job running this PowerShell command:
Get-AzRecoveryServicesAsrJob -Job $ReprotectJob
This command returns the status of the job. The output looks like this example:
Name : 0993fa3c-6ac1-4d96-920d-df06830d49f2 ID : /Subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/east-coast-rg/providers/Microsoft.RecoveryServices/vaults/asr-vault/replicationJobs/0993fa3c-6ac1-4d96 -920d-df06830d49f2 Type : Microsoft.RecoveryServices/vaults/replicationJobs JobType : SwitchReplicationGroupProtection DisplayName : Reprotect ClientRequestId : State : Succeeded StateDescription : Completed StartTime : 7/22/19 10:25:49 AM EndTime : 7/22/19 10:35:07 AM TargetObjectId : 28542035-9d78-58c9-a3ec-0ad29b0a88d8 TargetObjectType : ProtectionEntity TargetObjectName : patient-records AllowedActions : Tasks : {} Errors : {}
Note
When the reprotection is complete, the State is Succeeded and the StateDescription is Completed.
Failback to the West US region using the portal
Close Cloud Shell to use the Azure portal more easily.
On the home page, select All resources.
Select asr-vault.
In the left menu pane, under Protected items, select Replicated items.
Select patient-records.
You can't fail back the VM until the replication has completed, and synchronization is 100% completed. The synchronization process can take several minutes to complete.
After synchronization completes, select Failover.
For the Recovery Point, select Latest processed (low RTO).
To begin the failback, select OK.
Monitor the failback
On the home page, select All resources.
Select asr-vault.
In the left menu pane, under Monitoring, select Site Recovery jobs.
Select the in-progress Failover job.