-
-
Save reiven/0bf168e900ad45f8ad25 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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: | |
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 = "".join([ | |
"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 and blob.snapshot: | |
print blob.name, blob.snapshot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment