I´m trying to do a backup with the Azure SDK in Python.
Example with az-cli
az backup protection backup-now \
--resource-group myResourceGroup \
--vault-name myRecoveryServicesVault \
--container-name myVM \
--item-name myVM \
--retain-until 18-10-2017
how can I do it in a simple way (like in the az CLI) with Azure SDK from Python easy and clean?
Example with 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
I work many times with boto3 (AWS) to do things in the cloud with Python, but I´m very surprised with the Azure SDK options...
Actually, I´m searching examples with azure-mgmt-recoveryservicesbackup (https://learn.microsoft.com/en-us/python/api/azure-mgmt-recoveryservicesbackup/azure.mgmt.recoveryservicesbackup.recoveryservicesbackupclient?view=azure-python)
What is the best way to do VM backups with Azure SDK in Python?