Need to setup disaster recovery & enable Multiple VM Replication using azure automation runbook with python script

Satish B 70 Reputation points
2024-12-04T07:17:12.0366667+00:00

Need to setup disaster recovery & enable Multiple VM Replication using azure automation runbook with python script.

I have tried multiple ways to get reference MS DOC. But No luck.

Can some one please share if any manual available

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,278 questions
Azure Site Recovery
Azure Site Recovery
An Azure native disaster recovery service. Previously known as Microsoft Azure Hyper-V Recovery Manager.
737 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Pavan Minukuri 915 Reputation points Microsoft Vendor
    2024-12-06T01:03:05.26+00:00

    Hi Satish B,
    Thanks for replying back!
    To set up disaster recovery or multiple Azure VMs using Azure Automation with Python runbooks, please try with these steps

    1. . Create a Recovery Services Vault
    2. Sign in to the Azure portal.
    3. Navigate to Recovery Services vaults and select Add.
    4. Subscription: Choose your subscription.
      Resource Group: Select or create a new resource group. Vault Name: Provide a unique name for the vault. Region: Select a region different from where your VMs are located. Click Review + create, then Create to deploy the vault
    5. Enable Replication for VMs
    6. In the Recovery Services vault, go to the Site Recovery section and select Enable replication.
    7. Configure the following settings:
      8.Source Region: Select the region where your VMs are currently running. 9.Subscription: Choose the subscription containing your VMs. 10.Resource Group: Select the resource group that contains your VMs. 11.Virtual Machine Deployment Model: Choose Resource Manager (default). 12.Next, select the VMs you wish to replicate (up to 10 at a time) and proceed through the configuration options 13.Configure Replication Policy 14.Under replication settings, define your replication policy, which includes: Recovery Point Retention History App-consistent Snapshot Frequency 15.You can also create a replication group if you want multiple VMs to generate consistent recovery points together 16.Use Python Automation Runbook

    To automate this process using Python in an Azure Automation runbook, follow these coding guidelines:

    import azure.mgmt.recoveryservices
    from azure.identity import DefaultAzureCredential
    
    # Initialize credentials and client
    credential = DefaultAzureCredential()
    client = azure.mgmt.recoveryservices.RecoveryServicesClient(credential, subscription_id)
    
    # Specify parameters
    vault_name = "your_vault_name"
    resource_group_name = "your_resource_group"
    source_region = "source_region"
    target_region = "target_region"
    vm_ids = ["vm_id_1", "vm_id_2"]  # List of VM IDs to replicate
    
    # Enable replication for each VM
    for vm_id in vm_ids:
        client.replication_protected_items.create_or_update(
            resource_group_name,
            vault_name,
            vm_id,
            {
                'properties': {
                    'source': {
                        'region': source_region,
                        'resourceId': vm_id,
                    },
                    'target': {
                        'region': target_region,
                    }
                }
            }
        )
    
    

    Explanation of Code

    • The code initializes a client for Azure Recovery Services using Azure SDK for Python.
    • It specifies parameters such as the vault name, resource group, source and target regions, and VM IDs.
    • A loop goes through each VM ID and enables replication by calling create_or_update on replication_protected_items.

    Testing and Validation

    After enabling replication:

    Perform a test failover to ensure that your setup works as expected. This can be done via the Azure portal or programmatically using Azure SDKs

    • Monitor the replication status in the Recovery Services vault under Replicated items.

    By following these steps and utilizing Python automation, you can effectively set up disaster recovery for multiple Azure VMs.

    Please let me know if anything required...!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.