0
0
Azurecloud~5 mins

Disaster recovery strategies in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Disaster recovery strategies help you prepare for unexpected problems like data loss or system failures. They make sure your important apps and data can be quickly restored so your business keeps running smoothly.
When you want to protect your data from accidental deletion or corruption.
When you need to recover your applications quickly after a hardware failure.
When you want to keep your services running during a natural disaster or power outage.
When you want to meet legal or company rules about data backup and recovery.
When you want to test that your backup and recovery plans actually work.
Config File - main.tf
main.tf
provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East US"
}

resource "azurerm_recovery_services_vault" "example" {
  name                = "exampleRecoveryVault"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  sku                 = "Standard"
}

resource "azurerm_site_recovery_replication_policy" "example" {
  name                = "exampleReplicationPolicy"
  resource_group_name = azurerm_resource_group.example.name
  recovery_vault_name = azurerm_recovery_services_vault.example.name
  recovery_point_retention_in_minutes = 1440
  application_consistent_snapshot_frequency_in_minutes = 60
  multi_vm_sync_status = "Enabled"
}

resource "azurerm_site_recovery_protection_container_mapping" "example" {
  name                         = "exampleContainerMapping"
  resource_group_name          = azurerm_resource_group.example.name
  recovery_vault_name          = azurerm_recovery_services_vault.example.name
  source_protection_container_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.RecoveryServices/vaults/exampleRecoveryVault/replicationFabrics/exampleFabric/protectionContainers/exampleContainer"
  target_protection_container_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.RecoveryServices/vaults/exampleRecoveryVault/replicationFabrics/exampleFabric/protectionContainers/exampleTargetContainer"
  policy_id                    = azurerm_site_recovery_replication_policy.example.id
}

This Terraform file creates an Azure Recovery Services Vault to store backup and recovery data.

It defines a replication policy that controls how often recovery points are saved and how long they are kept.

It also sets up a protection container mapping to link source and target containers for disaster recovery replication.

Commands
This command initializes Terraform in the current directory. It downloads the Azure provider plugin needed to create resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/azurerm... - Installing hashicorp/azurerm v3.64.0... - Installed hashicorp/azurerm v3.64.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
This command applies the Terraform configuration to create the disaster recovery resources in Azure automatically without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
azurerm_resource_group.example: Creating... azurerm_resource_group.example: Creation complete after 2s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources] azurerm_recovery_services_vault.example: Creating... azurerm_recovery_services_vault.example: Creation complete after 15s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.RecoveryServices/vaults/exampleRecoveryVault] azurerm_site_recovery_replication_policy.example: Creating... azurerm_site_recovery_replication_policy.example: Creation complete after 5s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.RecoveryServices/vaults/exampleRecoveryVault/replicationPolicies/exampleReplicationPolicy] azurerm_site_recovery_protection_container_mapping.example: Creating... azurerm_site_recovery_protection_container_mapping.example: Creation complete after 4s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.RecoveryServices/vaults/exampleRecoveryVault/protectionContainerMappings/exampleContainerMapping] Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual approval to apply changes immediately
This Azure CLI command checks the details of the Recovery Services Vault to confirm it was created successfully.
Terminal
az backup vault show --name exampleRecoveryVault --resource-group example-resources
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.RecoveryServices/vaults/exampleRecoveryVault", "location": "East US", "name": "exampleRecoveryVault", "properties": { "provisioningState": "Succeeded" }, "resourceGroup": "example-resources", "sku": { "name": "Standard" }, "type": "Microsoft.RecoveryServices/vaults" }
--name - Specifies the vault name to show
--resource-group - Specifies the resource group of the vault
Key Concept

If you remember nothing else from this pattern, remember: setting up a Recovery Services Vault with replication policies ensures your data and apps can be restored quickly after a disaster.

Common Mistakes
Not specifying the correct resource group when running Azure CLI commands.
The command will fail or show no results because it looks in the wrong place.
Always include the --resource-group flag with the exact resource group name.
Skipping the terraform init step before applying configuration.
Terraform won't have the necessary provider plugins and will error out.
Run terraform init first to prepare the working directory.
Not defining a replication policy in the Recovery Services Vault.
Without a policy, backups and replication won't happen as expected.
Create and attach a replication policy to control backup frequency and retention.
Summary
Initialize Terraform to prepare Azure provider plugins.
Apply Terraform configuration to create Recovery Services Vault and replication policy.
Verify the vault creation using Azure CLI to ensure disaster recovery resources are ready.