Azure Recovery Services Management

4.0.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the `RecoveryServicesClient` using `DefaultAzureCredential` for authentication and then lists up to 5 Recovery Services vaults in the specified Azure subscription. Ensure `AZURE_SUBSCRIPTION_ID` is set as an environment variable or that `az login` has been executed.

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.")

view raw JSON →