我被告知移动我的 bash 脚本,该脚本报告 VM 备份状态,还报告未备份到 Azure 自动化帐户的 VM。我选择了 python,因为 Automation Account 没有 bash,而且我之前出于系统管理目的已经完成了 python 脚本。我不是 python 开发人员,我需要帮助浏览 Azure python SDK 类。
我需要从 python SDK 模块之一的门户中找到“备份项”,以从备份库中检索 VM 信息。我试过 azure.mgmt.recoveryservices 和 azure.mgmt.recoveryservicesbackup。我可以从 azure.mgmt.recoveryservices 获取保管库信息,我可以使用它来查询有关保管库的更多信息,希望是 VM 信息。我猜是 azure.mgmt.recoveryservicesbackup。但我迷失在 azure.mgmt.recoveryservicesbackup.jobs.models 中。我无法在数百个班级中分辨出哪个班级会提供该信息。
我想使用 vault backup 的输出来对照 VM 列表,找出哪些没有被备份。
我看过:
您可以使用以下代码片段获取 VM 备份详细信息。
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
import requests
SUBSCRIPTION_ID = '<Your Subscription ID>'
VM_NAME = '<VM Name>'
credentials = ServicePrincipalCredentials(
client_id='<Client id>',
secret='<Client Secret>',
tenant='<Your Tenent Id>'
)
# Creating base URL
BASE_API_URL = "https://management.azure.com/Subscriptions/<Subscription>/resourceGroups/<Resourece group name>/providers/Microsoft.RecoveryServices/vaults/your_vault_name/backupProtectedItems?api-version=2019-05-13&"
# Add the required filter to fetch the Exact details
customFilter="$filter=backupManagementType eq 'AzureIaasVM' and itemType eq 'VM' and policyName eq 'DailyPolicy'"
#Adding the Base API url with custom filter
BASE_URL = BASE_API_URL + customFilter
header = {
"Authorization": 'Bearer '+ credentials.token["access_token"]
}
response = requests.get(BASE_URL, headers=header)
# here you can handle the response to know the details of backup
print(response.content)
...
参考here使用Azure cli实现
我要找的是 RecoveryServicesBackupClient 构造函数中的“backup_protected_item”,这里是示例代码。
from azure.mgmt.recoveryservicesbackup import RecoveryServicesBackupClient
backup_items = backup_client.backup_protected_items.list(resource_group_name='rg_xxx', vault_name=var_vault)
print(backup_items.properties)