azure python SDK 从 recoverservices(backup) 检索备份项目

azure python SDK retrieve backup items from recoverservices(backup)

我被告知移动我的 bash 脚本,该脚本报告 VM 备份状态,还报告未备份到 Azure 自动化帐户的 VM。我选择了 python,因为自动化帐户没有 bash,而且我之前出于系统管理目的已经完成了 python 脚本。我不是 python 开发人员,我需要帮助导航 Azure python SDK 类.

我需要从 python SDK 模块之一的门户中找到“备份项”,以从备份库中检索 VM 信息。我已经尝试 azure.mgmt.recoveryservices 和 azure.mgmt.recoveryservices 备份。我可以从 azure.mgmt.recoveryservices 获取保管库信息,我可以使用它来查询有关保管库的更多信息,希望是 VM 信息。我的猜测是 azure.mgmt.recoveryservices 备份。但我迷失在 azure.mgmt.recoveryservicesbackup.jobs.models 中。我无法在数百个 类 中分辨出哪个会提供该信息。

我想使用保管库备份的输出来对照 VM 列表,找出哪些没有被备份。

我看过:
https://docs.microsoft.com/en-us/python/api/azure-mgmt-recoveryservicesbackup/azure.mgmt.recoveryservicesbackup.activestamp?view=azure-python
https://azure.github.io/azure-sdk/releases/latest/all/python.html,
https://www.programcreek.com/python/?ClassName=azure.mgmt.recoveryservicesbackup&submit=Search.

任何帮助将不胜感激!

谢谢。

使用python获取虚拟机的备份

您可以使用以下代码片段获取 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)