【发布时间】:2021-08-03 21:03:29
【问题描述】:
我正在尝试使用 Python 中的 Azure SDK 进行备份。
az-cli 示例
az backup protection backup-now \
--resource-group myResourceGroup \
--vault-name myRecoveryServicesVault \
--container-name myVM \
--item-name myVM \
--retain-until 18-10-2017
如何使用 Python 中的 Azure SDK 以简单的方式(如在 az CLI 中)轻松而干净地做到这一点?
SDK 示例:
#!/usr/bin/python
from azure.storage import BlobService
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("container", help="the blob container")
parser.add_argument("blob", help="the blob name")
parser.add_argument("-s", "--snapshot", help="take a new snapshot",
action="store_true")
parser.add_argument("-d", "--delete", help="delete a snapshot")
parser.add_argument("-c", "--copy", help="copy a snapshot")
args = parser.parse_args()
# To use the storage services, you need to set the AZURE_STORAGE_ACCOUNT
# and the AZURE_STORAGE_ACCESS_KEY environment variables to the storage
# account name and primary access key you obtain from the Azure Portal.
AZURE_STORAGE_ACCOUNT='mystorage'
AZURE_STORAGE_ACCESS_KEY='supercalifragilisticexpialidocious'
blob_service = BlobService(AZURE_STORAGE_ACCOUNT, AZURE_STORAGE_ACCESS_KEY)
if args.snapshot == True:
print '# Taking new snapshot...'
blob_service.snapshot_blob(args.container, args.blob)
print 'OK.'
if args.delete:
print '# Deleting snapshot...'
blob_service.delete_blob(args.container, args.blob, snapshot=args.delete)
print "Deleted", args.delete
if args.copy:
print '# Copying snapshot...'
src = "https://" + AZURE_STORAGE_ACCOUNT + ".blob.core.windows.net/" + args.container + "/" + args.blob + "?snapshot=" + args.copy
dst = args.blob + "_restore"
blob_service.copy_blob(args.container, dst, src)
print "Copied", src, "to", dst
print '# List of snapshots:'
for blob in blob_service.list_blobs(args.container, include='snapshots'):
if blob.name == args.blob:
print blob.name, blob.snapshot
我多次使用 boto3 (AWS) 使用 Python 在云中做事,但我对 Azure SDK 选项感到非常惊讶...
实际上,我正在使用 azure-mgmt-recoveryservicesbackup (https://docs.microsoft.com/en-us/python/api/azure-mgmt-recoveryservicesbackup/azure.mgmt.recoveryservicesbackup.recoveryservicesbackupclient?view=azure-python) 搜索示例
在 Python 中使用 Azure SDK 进行 VM 备份的最佳方法是什么?
【问题讨论】: