Skip to content

Instantly share code, notes, and snippets.

@telekosmos
Forked from tomconte/snapshot_utility.py
Created July 29, 2019 15:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telekosmos/ae95fdd3c5cecb6cd8624751e44b0b44 to your computer and use it in GitHub Desktop.
Save telekosmos/ae95fdd3c5cecb6cd8624751e44b0b44 to your computer and use it in GitHub Desktop.
Sample Python script to manage Azure Blob Storage snapshots: list snapshots, take a snapshot, delete a snapshot, copy a snapshot to a new Blob.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment