Azure Recovery Services Management
The Microsoft Azure Recoveryservices Management Client Library for Python provides programmatic access to manage Azure Recovery Services vaults, backup policies, protected items, and disaster recovery configurations. It allows for automation of tasks like creating and configuring backup vaults, setting up replication, and managing backups. The current version is 4.0.0, and Azure SDKs generally follow an independent and frequent release cadence.
Warnings
- breaking The `RecoveryServicesClient` constructor now requires `subscription_id` as the second argument.
- breaking There are significant API model changes and updates to operation signatures in various sub-clients (e.g., `BackupVaultOperationResults.get`, `RecoveryServices.get_operation_result`).
- gotcha Authentication with Azure services requires the `azure-identity` library and proper credential setup. `DefaultAzureCredential` attempts various methods (environment variables, managed identity, Azure CLI, etc.).
- gotcha Many Azure Management operations are scoped to a specific resource group or subscription. Omitting or providing an incorrect scope will result in errors (e.g., 'ResourceNotFound' or 'Forbidden').
Install
-
pip install azure-mgmt-recoveryservices azure-identity
Imports
- RecoveryServicesClient
from azure.mgmt.recoveryservices import RecoveryServicesClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.recoveryservices import RecoveryServicesClient
# For local development, set environment variables:
# AZURE_SUBSCRIPTION_ID, AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET
# Alternatively, use `az login` via Azure CLI for interactive authentication.
# Acquire a credential object
credential = DefaultAzureCredential()
# Retrieve subscription ID from environment variable
# It is crucial to provide a valid subscription ID for client instantiation.
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "")
if not subscription_id:
print("WARNING: AZURE_SUBSCRIPTION_ID environment variable not set. Please set it or ensure 'az login' is active.")
# Create the Recovery Services client
client = RecoveryServicesClient(credential, subscription_id)
print(f"Successfully created RecoveryServicesClient for subscription: {subscription_id}")
# Example: List up to 5 Recovery Services vaults in the subscription
try:
print("\nListing up to 5 Recovery Services vaults in the subscription...")
vault_count = 0
for vault in client.vaults.list_by_subscription():
print(f" - Vault Name: {vault.name}, Location: {vault.location}")
vault_count += 1
if vault_count >= 5:
break
if vault_count == 0:
print(" No Recovery Services vaults found in this subscription (or none in the first 5 pages).")
except Exception as e:
print(f"\nError listing vaults: {e}")
print(" Ensure you have 'Reader' or 'Recovery Services Contributor' permissions and AZURE_SUBSCRIPTION_ID is correct.")