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
- . Create a Recovery Services Vault
- Sign in to the Azure portal.
- Navigate to Recovery Services vaults and select Add.
- 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 - Enable Replication for VMs
- In the Recovery Services vault, go to the Site Recovery section and select Enable replication.
- 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
onreplication_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...!