Skip to content

Instantly share code, notes, and snippets.

@zmf
Created November 10, 2014 23:07
Show Gist options
  • Save zmf/1f46c8cc4b3ee3489710 to your computer and use it in GitHub Desktop.
Save zmf/1f46c8cc4b3ee3489710 to your computer and use it in GitHub Desktop.
Point in time restores from S3 versioned bucket
#!/usr/bin/python
import os
import sys
import getopt
import boto
from datetime import datetime
from dateutil.tz import tzoffset
from dateutil.parser import parse as parse_date
from time import strftime
### START_OF_CONFIG
# replace aws_key with your AWS key
aws_key = ''
# replace aws_secret with your AWS secret
aws_secret = ''
# replace aws_bucket with your bucket name
aws_bucket = ''
# replace aws_match with the path you want to restore, or leave empty to restore whole bucket
aws_match = ''
# modify datetime_to_restore to match the point in time you want to restore to
datetime_to_restore = datetime(2014, 10, 17, 0, 0, 0, tzinfo=tzoffset('UTC', 0))
### END_OF_CONFIG
def assure_path_exists(path):
dir = os.path.dirname(path)
if not os.path.exists(dir):
os.makedirs(dir)
def main():
conn = boto.connect_s3(aws_access_key_id = aws_key, aws_secret_access_key = aws_secret)
recovery_list = { }
bucket = conn.lookup(aws_bucket)
for k in bucket.list_versions(aws_match):
modified_dt = parse_date(k.last_modified)
if(modified_dt < datetime_to_restore):
if k.name in recovery_list.keys():
setted = recovery_list[k.name]['date']
else:
recovery_list[k.name] = { }
setted = datetime(1970,1,1,0,0,0, tzinfo=tzoffset('UTC', 0))
if(setted < modified_dt):
recovery_list[k.name]['date'] = modified_dt
recovery_list[k.name]['version'] = k.version_id
restore_to = 's3_restores/' + strftime("%Y-%m-%d_%H:%M:%S")
for name, data in recovery_list.iteritems():
print ' - Restoring %s -> date:%s | version:%s' % (name, data['date'], data['version'])
try:
down_k = bucket.get_key(key_name=name, version_id=data['version'])
except:
print " \t! File not found (was probably deleted during the given point-in-time) and thus cannot be retrieved"
continue
assure_path_exists(restore_to + '/' + name)
down_k.get_contents_to_filename(restore_to + '/' + name)
print "\n -- Restore finished and can be found under " + restore_to + "\n"
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment