Skip to content

Instantly share code, notes, and snippets.

@tomconte
Created July 23, 2014 16:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tomconte/03216b95315caba9caba to your computer and use it in GitHub Desktop.
Save tomconte/03216b95315caba9caba 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
@reiven
Copy link

reiven commented Aug 20, 2014

Great utility, thanks!
It will be a good idea to have an extra argument to list all blobs in a container

And just FYI, i've found this on a post on your blog (http://hypernephelist.com/) but i cannot comment in your post! thats why i'm writing you here

@howardatwork
Copy link

Thanks for this utility. There is an open request to have this in Azure CLI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment