Created
November 25, 2013 18:20
-
-
Save flyte/7645963 to your computer and use it in GitHub Desktop.
tar.gz a directory and push it to Glacier.
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
from boto import glacier | |
from datetime import datetime | |
from os.path import isdir | |
import argparse | |
import tarfile | |
try: | |
# Python 2.7 | |
from collections import OrderedDict | |
except ImportError: | |
# Python 2.6 (after "pip install ordereddict") | |
from ordereddict import OrderedDict | |
params = OrderedDict([ | |
("access_key", {}), | |
("secret_key", {}), | |
("target", {"help": "The file or directory you want to tar.gz up and push to Glacier."}), | |
("vault", {"help": "The name of the vault to push to. Will be created if it doesn't exist."}), | |
("--region", {"default": "eu-west-1"}), | |
("--tar_gz", {"action": "store_true", "default": False}) | |
]) | |
def get_glacier_connection(region, access_key, secret_key): | |
return glacier.connect_to_region( | |
region, | |
aws_access_key_id=access_key, | |
aws_secret_access_key=secret_key) | |
def create_tar_gz(name, target): | |
try: | |
tar = tarfile.open(name, "w:gz") | |
tar.add(target) | |
finally: | |
tar.close() | |
def get_or_create_vault(name, region): | |
print "Listing vaults..." | |
vault = None | |
for v in region.list_vaults(): | |
if v.name == name: | |
print "Existing vault %s found!" % name | |
vault = v | |
break | |
if vault is None: | |
print "No existing vault was found. Creating one..." | |
vault = region.create_vault(name) | |
return vault | |
def main(): | |
a = argparse.ArgumentParser() | |
for key, value in params.items(): | |
a.add_argument(key, **value) | |
args = a.parse_args() | |
print "Connecting to %s region..." % args.region | |
region = get_glacier_connection(args.region, args.access_key, args.secret_key) | |
vault = get_or_create_vault(args.vault, region) | |
print "Uploading archive..." | |
archive_id = vault.upload_archive( | |
args.target, | |
description=str(datetime.now())) | |
print "Succesfully uploaded archive ID: %s" % archive_id | |
print "DONE" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment