|
| 1 | +--- |
| 2 | +title: Shared disks in Azure Site Recovery using PowerShell |
| 3 | +description: This article describes how set up disaster recovery for shared disks using PowerShell. |
| 4 | +ms.topic: how-to |
| 5 | +ms.service: azure-site-recovery |
| 6 | +ms.date: 04/02/2025 |
| 7 | +ms.author: ankitadutta |
| 8 | +author: ankitaduttaMSFT |
| 9 | +--- |
| 10 | + |
| 11 | +# Set up disaster recovery for shared disks using PowerShell |
| 12 | + |
| 13 | +This article describes how to set up disaster recovery for Azure to Azure shared disks VM using PowerShell. For more information about shared disks, see [Shared disks in Azure Site Recovery](./shared-disk-support-matrix.md). |
| 14 | + |
| 15 | +[!INCLUDE [updated-for-az](~/reusable-content/ce-skilling/azure/includes/updated-for-az.md)] |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +Before you start: |
| 20 | +- Make sure that you understand the [scenario architecture and components](azure-to-azure-architecture.md). |
| 21 | +- Review the [support requirements](azure-to-azure-support-matrix.md) for all components. |
| 22 | +- You have the Azure PowerShell `Az` module. If you need to install or upgrade Azure PowerShell, follow this [Guide to install and configure Azure PowerShell](/powershell/azure/install-azure-powershell). |
| 23 | + |
| 24 | + |
| 25 | +## Set up the environment |
| 26 | + |
| 27 | +Sign in to your Azure account and set the context to the subscription where you want to enable replication. |
| 28 | + |
| 29 | +```powershell |
| 30 | +Connect-AzAccount |
| 31 | +``` |
| 32 | + |
| 33 | +Select your Azure subscription. Use the Get-AzSubscription cmdlet to get the list of Azure subscriptions you have access to. Select the Azure subscription to work with using the Set-AzContext cmdlet. |
| 34 | + |
| 35 | +```powershell |
| 36 | +Set-AzContext -SubscriptionId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 37 | +``` |
| 38 | + |
| 39 | +## Get the resource group and VM details |
| 40 | + |
| 41 | +In this article, a virtual machine in the East US region is replicated to and recovered in the West US 2 region. The virtual machine being replicated has an OS disk and a single data disk. The name of the virtual machine used in the example is `AzureDemoVM`. |
| 42 | + |
| 43 | +```powershell |
| 44 | +# Get details of the virtual machine |
| 45 | +$VM = Get-AzVM -ResourceGroupName "A2AdemoRG" -Name "AzureDemoVM" |
| 46 | +Write-Output $V |
| 47 | +``` |
| 48 | + |
| 49 | +```Output |
| 50 | +ResourceGroupName : A2AdemoRG |
| 51 | +Id : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/A2AdemoRG/providers/Microsoft.Compute/virtualMachines/AzureDemoVM |
| 52 | +VmId : 1b864902-c7ea-499a-ad0f-65da2930b81b |
| 53 | +Name : AzureDemoVM |
| 54 | +Type : Microsoft.Compute/virtualMachines |
| 55 | +Location : eastus |
| 56 | +Tags : {} |
| 57 | +DiagnosticsProfile : {BootDiagnostics} |
| 58 | +HardwareProfile : {VmSize} |
| 59 | +NetworkProfile : {NetworkInterfaces} |
| 60 | +OSProfile : {ComputerName, AdminUsername, WindowsConfiguration, Secrets} |
| 61 | +ProvisioningState : Succeeded |
| 62 | +StorageProfile : {ImageReference, OsDisk, DataDisks} |
| 63 | +``` |
| 64 | + |
| 65 | +Get disk details for the virtual machine's disks. Disk details will be used later when starting replication for the virtual machine. |
| 66 | + |
| 67 | +```azurepowershell |
| 68 | +$OSDiskVhdURI = $VM.StorageProfile.OsDisk.Vhd |
| 69 | +$DataDisk1VhdURI = $VM.StorageProfile.DataDisks[0].Vhd |
| 70 | +``` |
| 71 | + |
| 72 | +## Create a Recovery Services vault |
| 73 | + |
| 74 | +Create a resource group in which to create the Recovery Services vault. |
| 75 | + |
| 76 | +> [!IMPORTANT] |
| 77 | +> * The Recovery services vault and the virtual machines being protected, must be in different Azure locations. |
| 78 | +> * The resource group of the Recovery services vault, and the virtual machines being protected, must be in different Azure locations. |
| 79 | +> * The Recovery services vault, and the resource group to which it belongs, can be in the same Azure location. |
| 80 | +
|
| 81 | +In the example in this article, the virtual machine being protected is in the East US region. The recovery region selected for disaster recovery is the West US 2 region. The recovery services vault, and the resource group of the vault, are both in the recovery region, West US 2. |
| 82 | + |
| 83 | +```powershell |
| 84 | +#Create a resource group for the recovery services vault in the recovery Azure region |
| 85 | +New-AzResourceGroup -Name "a2ademorecoveryrg" -Location "West US 2" |
| 86 | +``` |
| 87 | + |
| 88 | +```Output |
| 89 | +ResourceGroupName : a2ademorecoveryrg |
| 90 | +Location : westus2 |
| 91 | +ProvisioningState : Succeeded |
| 92 | +Tags : |
| 93 | +ResourceId : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/a2ademorecoveryrg |
| 94 | +``` |
| 95 | + |
| 96 | +Create a Recovery services vault. In this example, a Recovery Services vault named `a2aDemoRecoveryVault` is created in the West US 2 region. |
| 97 | + |
| 98 | +```powershell |
| 99 | +#Create a new Recovery services vault in the recovery region |
| 100 | +$vault = New-AzRecoveryServicesVault -Name "a2aDemoRecoveryVault" -ResourceGroupName "a2ademorecoveryrg" -Location "West US 2" |
| 101 | +
|
| 102 | +Write-Output $vault |
| 103 | +``` |
| 104 | + |
| 105 | +```Output |
| 106 | +Name : a2aDemoRecoveryVault |
| 107 | +ID : /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/a2ademorecoveryrg/providers/Microsoft.RecoveryServices/vaults/a2aDemoRecoveryVault |
| 108 | +Type : Microsoft.RecoveryServices/vaults |
| 109 | +Location : westus2 |
| 110 | +ResourceGroupName : a2ademorecoveryrg |
| 111 | +SubscriptionId : xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 112 | +Properties : Microsoft.Azure.Commands.RecoveryServices.ARSVaultProperties |
| 113 | +``` |
| 114 | + |
| 115 | +## Prepare the vault to start replicating Azure virtual machines |
| 116 | + |
| 117 | +### Create a Site Recovery fabric object to represent the primary (source) region |
| 118 | + |
| 119 | +The fabric object in the vault represents an Azure region. The primary fabric object is created to represent the Azure region that virtual machines being protected to the vault belong to. In the example in this article, the virtual machine being protected is in the East US region. |
| 120 | + |
| 121 | +- Only one fabric object can be created per region. |
| 122 | +- If you've previously enabled Site Recovery replication for a VM in the Azure portal, Site Recovery creates a fabric object automatically. If a fabric object exists for a region, you can't create a new one. |
| 123 | + |
| 124 | +Before you start, understand that Site Recovery operations are executed asynchronously. When you initiate an operation, an Azure Site Recovery job is submitted and a job tracking object is returned. Use the job tracking object to get the latest status for the job (`Get-AzRecoveryServicesAsrJob`), and to monitor the status of the operation. |
| 125 | + |
| 126 | +```powershell |
| 127 | +#Create Primary ASR fabric |
| 128 | +$TempASRJob = New-AzRecoveryServicesAsrFabric -Azure -Location 'East US' -Name "A2Ademo-EastUS" |
| 129 | +
|
| 130 | +# Track Job status to check for completion |
| 131 | +while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){ |
| 132 | + #If the job hasn't completed, sleep for 10 seconds before checking the job status again |
| 133 | + sleep 10; |
| 134 | + $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob |
| 135 | +} |
| 136 | +
|
| 137 | +#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded" |
| 138 | +Write-Output $TempASRJob.State |
| 139 | +
|
| 140 | +$PrimaryFabric = Get-AzRecoveryServicesAsrFabric -Name "A2Ademo-EastUS" |
| 141 | +``` |
| 142 | + |
| 143 | +If virtual machines from multiple Azure regions are being protected to the same vault, create one fabric object for each source Azure region. |
| 144 | + |
| 145 | +### Create a Site Recovery fabric object to represent the recovery region |
| 146 | + |
| 147 | +The recovery fabric object represents the recovery Azure location. If there's a failover, virtual machines are replicated and recovered to the recovery region represented by the recovery fabric. The recovery Azure region used in this example is West US 2. |
| 148 | + |
| 149 | +```powershell |
| 150 | +#Create Recovery ASR fabric |
| 151 | +$TempASRJob = New-AzRecoveryServicesAsrFabric -Azure -Location 'West US 2' -Name "A2Ademo-WestUS" |
| 152 | +
|
| 153 | +# Track Job status to check for completion |
| 154 | +while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){ |
| 155 | + sleep 10; |
| 156 | + $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob |
| 157 | +} |
| 158 | +
|
| 159 | +#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded" |
| 160 | +Write-Output $TempASRJob.State |
| 161 | +
|
| 162 | +$RecoveryFabric = Get-AzRecoveryServicesAsrFabric -Name "A2Ademo-WestUS" |
| 163 | +``` |
| 164 | + |
| 165 | +### Create a Site Recovery protection container in the primary fabric |
| 166 | + |
| 167 | +The protection container is a container used to group replicated items within a fabric. |
| 168 | + |
| 169 | +```powershell |
| 170 | +#Create a Protection container in the primary Azure region (within the Primary fabric) |
| 171 | +$TempASRJob = New-AzRecoveryServicesAsrProtectionContainer -InputObject $PrimaryFabric -Name "A2AEastUSProtectionContainer" |
| 172 | +
|
| 173 | +#Track Job status to check for completion |
| 174 | +while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){ |
| 175 | + sleep 10; |
| 176 | + $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob |
| 177 | +} |
| 178 | +
|
| 179 | +Write-Output $TempASRJob.State |
| 180 | +
|
| 181 | +$PrimaryProtContainer = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $PrimaryFabric -Name "A2AEastUSProtectionContainer" |
| 182 | +``` |
| 183 | + |
| 184 | +#### Fabric and container creation when enabling zone to zone replication |
| 185 | + |
| 186 | +When enabling zone to zone replication, only one fabric will be created. But there will be two containers. Assuming that the region is West Europe, use following commands to get the primary and protection containers - |
| 187 | + |
| 188 | +```powershell |
| 189 | +$primaryProtectionContainer = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $fabric -Name "asr-a2a-default-westeurope-container" |
| 190 | +$recoveryProtectionContainer = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $fabric -Name "asr-a2a-default-westeurope-t-container" |
| 191 | +``` |
| 192 | + |
| 193 | +### Create a replication policy |
| 194 | + |
| 195 | +```powershell |
| 196 | +#Create replication policy |
| 197 | +$TempASRJob = New-AzRecoveryServicesAsrPolicy -AzureToAzure -Name "A2APolicy" -RecoveryPointRetentionInHours 24 -ApplicationConsistentSnapshotFrequencyInHours 4 |
| 198 | +
|
| 199 | +#Track Job status to check for completion |
| 200 | +while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){ |
| 201 | + sleep 10; |
| 202 | + $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob |
| 203 | +} |
| 204 | +
|
| 205 | +#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded" |
| 206 | +Write-Output $TempASRJob.State |
| 207 | +
|
| 208 | +$ReplicationPolicy = Get-AzRecoveryServicesAsrPolicy -Name "A2APolicy" |
| 209 | +``` |
| 210 | + |
| 211 | +### Create a protection container mapping between the primary and recovery protection container |
| 212 | + |
| 213 | +A protection container mapping maps the primary protection container with a recovery protection container and a replication policy. Create one mapping for each replication policy that you'll use to replicate virtual machines between a protection container pair. |
| 214 | + |
| 215 | +```powershell |
| 216 | +#Create Protection container mapping between the Primary and Recovery Protection Containers with the Replication policy |
| 217 | +$TempASRJob = New-AzRecoveryServicesAsrProtectionContainerMapping -Name "A2APrimaryToRecovery" -Policy $ReplicationPolicy -PrimaryProtectionContainer $PrimaryProtContainer -RecoveryProtectionContainer $RecoveryProtContainer |
| 218 | +
|
| 219 | +#Track Job status to check for completion |
| 220 | +while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){ |
| 221 | + sleep 10; |
| 222 | + $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob |
| 223 | +} |
| 224 | +
|
| 225 | +#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded" |
| 226 | +Write-Output $TempASRJob.State |
| 227 | +
|
| 228 | +$EusToWusPCMapping = Get-AzRecoveryServicesAsrProtectionContainerMapping -ProtectionContainer $PrimaryProtContainer -Name "A2APrimaryToRecovery" |
| 229 | +``` |
| 230 | + |
| 231 | +#### Protection container mapping creation when enabling zone to zone replication |
| 232 | + |
| 233 | +When enabling zone to zone replication, use the below command to create protection container mapping. Assuming that the region is West Europe, the command will be - |
| 234 | + |
| 235 | +```powershell |
| 236 | +$protContainerMapping = Get-AzRecoveryServicesAsrProtectionContainerMapping -ProtectionContainer $PrimprotectionContainer -Name "westeurope-westeurope-24-hour-retention-policy-s" |
| 237 | +``` |
| 238 | + |
| 239 | +### Create a protection container mapping for failback (reverse replication after a failover) |
| 240 | + |
| 241 | +After a failover, when you're ready to bring the failed over virtual machine back to the original Azure region, you do a failback. To fail back, the failed over virtual machine is reverse replicated from the failed over region to the original region. For reverse replication the roles of the original region and the recovery region switch. The original region now becomes the new recovery region, and what was originally the recovery region now becomes the primary region. The protection container mapping for reverse replication represents the switched roles of the original and recovery regions. |
| 242 | + |
| 243 | +```powershell |
| 244 | +#Create Protection container mapping (for fail back) between the Recovery and Primary Protection Containers with the Replication policy |
| 245 | +$TempASRJob = New-AzRecoveryServicesAsrProtectionContainerMapping -Name "A2ARecoveryToPrimary" -Policy $ReplicationPolicy -PrimaryProtectionContainer $RecoveryProtContainer -RecoveryProtectionContainer $PrimaryProtContainer |
| 246 | +
|
| 247 | +#Track Job status to check for completion |
| 248 | +while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){ |
| 249 | + sleep 10; |
| 250 | + $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob |
| 251 | +} |
| 252 | +
|
| 253 | +#Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded" |
| 254 | +Write-Output $TempASRJob.State |
| 255 | +
|
| 256 | +$WusToEusPCMapping = Get-AzRecoveryServicesAsrProtectionContainerMapping -ProtectionContainer $RecoveryProtContainer -Name "A2ARecoveryToPrimary" |
| 257 | +``` |
| 258 | + |
| 259 | +## Create cache storage account and target storage account |
| 260 | + |
| 261 | +A cache storage account is a standard storage account in the same Azure region as the virtual machine being replicated. The cache storage account is used to hold replication changes temporarily, before the changes are moved to the recovery Azure region. High churn support is also available in Azure Site Recovery to get higher churn limits. To use this feature, please create a Premium Block Blob type of storage accounts and then use it as the cache storage account. You can choose to, but it's not necessary, to specify different cache storage accounts for the different disks of a virtual machine. If you use different cache storage accounts, ensure they are of the same type (Standard or Premium Block Blobs). For more information, see [Azure VM Disaster Recovery - High Churn Support](./concepts-azure-to-azure-high-churn-support.md). |
| 262 | + |
| 263 | +```powershell |
| 264 | +#Create Cache storage account for replication logs in the primary region |
| 265 | +$EastUSCacheStorageAccount = New-AzStorageAccount -Name "a2acachestorage" -ResourceGroupName "A2AdemoRG" -Location 'East US' -SkuName Standard_LRS -Kind Storage |
| 266 | +``` |
| 267 | + |
| 268 | +For virtual machines **not using managed disks**, the target storage account is the storage account in the recovery region to which disks of the virtual machine are replicated. The target storage account can be either a standard storage account or a premium storage account. Select the kind of storage account required based on the data change rate (IO write rate) for the disks and the Azure Site Recovery supported churn limits for the storage type. |
| 269 | + |
| 270 | +```powershell |
| 271 | +#Create Target storage account in the recovery region. In this case a Standard Storage account |
| 272 | +$WestUSTargetStorageAccount = New-AzStorageAccount -Name "a2atargetstorage" -ResourceGroupName "a2ademorecoveryrg" -Location 'West US 2' -SkuName Standard_LRS -Kind Storage |
| 273 | +``` |
| 274 | + |
| 275 | +## Create network mappings |
| 276 | + |
| 277 | +A network mapping maps virtual networks in the primary region to virtual networks in the recovery region. The network mapping specifies the Azure virtual network in the recovery region, that a virtual machine in the primary virtual network should fail over to. One Azure virtual network can be mapped to only a single Azure virtual network in a recovery region. |
| 278 | + |
| 279 | +- Create an Azure virtual network in the recovery region to fail over to: |
| 280 | + |
| 281 | + ```powershell |
| 282 | + #Create a Recovery Network in the recovery region |
| 283 | + $WestUSRecoveryVnet = New-AzVirtualNetwork -Name "a2arecoveryvnet" -ResourceGroupName "a2ademorecoveryrg" -Location 'West US 2' -AddressPrefix "10.0.0.0/16" |
| 284 | +
|
| 285 | + Add-AzVirtualNetworkSubnetConfig -Name "default" -VirtualNetwork $WestUSRecoveryVnet -AddressPrefix "10.0.0.0/20" | Set-AzVirtualNetwork |
| 286 | +
|
| 287 | + $WestUSRecoveryNetwork = $WestUSRecoveryVnet.Id |
| 288 | + ``` |
| 289 | + |
| 290 | +- Retrieve the primary virtual network. The VNet that the virtual machine is connected to: |
| 291 | + |
| 292 | + ```powershell |
| 293 | + #Retrieve the virtual network that the virtual machine is connected to |
| 294 | +
|
| 295 | + #Get first network interface card(nic) of the virtual machine |
| 296 | + $SplitNicArmId = $VM.NetworkProfile.NetworkInterfaces[0].Id.split("/") |
| 297 | +
|
| 298 | + #Extract resource group name from the ResourceId of the nic |
| 299 | + $NICRG = $SplitNicArmId[4] |
| 300 | +
|
| 301 | + #Extract resource name from the ResourceId of the nic |
| 302 | + $NICname = $SplitNicArmId[-1] |
| 303 | +
|
| 304 | + #Get network interface details using the extracted resource group name and resource name |
| 305 | + $NIC = Get-AzNetworkInterface -ResourceGroupName $NICRG -Name $NICname |
| 306 | +
|
| 307 | + #Get the subnet ID of the subnet that the nic is connected to |
| 308 | + $PrimarySubnet = $NIC.IpConfigurations[0].Subnet |
| 309 | +
|
| 310 | + # Extract the resource ID of the Azure virtual network the nic is connected to from the subnet ID |
| 311 | + $EastUSPrimaryNetwork = (Split-Path(Split-Path($PrimarySubnet.Id))).Replace("\","/") |
| 312 | + ``` |
| 313 | + |
| 314 | +- Create network mapping between the primary virtual network and the recovery virtual network: |
| 315 | + |
| 316 | + ```powershell |
| 317 | + #Create an ASR network mapping between the primary Azure virtual network and the recovery Azure virtual network |
| 318 | + $TempASRJob = New-AzRecoveryServicesAsrNetworkMapping -AzureToAzure -Name "A2AEusToWusNWMapping" -PrimaryFabric $PrimaryFabric -PrimaryAzureNetworkId $EastUSPrimaryNetwork -RecoveryFabric $RecoveryFabric -RecoveryAzureNetworkId $WestUSRecoveryNetwork |
| 319 | +
|
| 320 | + #Track Job status to check for completion |
| 321 | + while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){ |
| 322 | + sleep 10; |
| 323 | + $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob |
| 324 | + } |
| 325 | +
|
| 326 | + #Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded" |
| 327 | + Write-Output $TempASRJob.State |
| 328 | + ``` |
| 329 | + |
| 330 | +- Create network mapping for the reverse direction (fail back): |
| 331 | + |
| 332 | + ```powershell |
| 333 | + #Create an ASR network mapping for fail back between the recovery Azure virtual network and the primary Azure virtual network |
| 334 | + $TempASRJob = New-AzRecoveryServicesAsrNetworkMapping -AzureToAzure -Name "A2AWusToEusNWMapping" -PrimaryFabric $RecoveryFabric -PrimaryAzureNetworkId $WestUSRecoveryNetwork -RecoveryFabric $PrimaryFabric -RecoveryAzureNetworkId $EastUSPrimaryNetwork |
| 335 | +
|
| 336 | + #Track Job status to check for completion |
| 337 | + while (($TempASRJob.State -eq "InProgress") -or ($TempASRJob.State -eq "NotStarted")){ |
| 338 | + sleep 10; |
| 339 | + $TempASRJob = Get-AzRecoveryServicesAsrJob -Job $TempASRJob |
| 340 | + } |
| 341 | +
|
| 342 | + #Check if the Job completed successfully. The updated job state of a successfully completed job should be "Succeeded" |
| 343 | + Write-Output $TempASRJob.State |
| 344 | +
|
| 345 | +
|
| 346 | +
|
| 347 | +
|
| 348 | +
|
| 349 | +
|
| 350 | +
|
| 351 | +
|
| 352 | +## Next steps |
| 353 | +
|
| 354 | +View the Azure Site Recovery PowerShell reference to learn how you can do other tasks such as creating recovery plans and testing failover of recovery plans with PowerShell. |
0 commit comments