Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pg_dump and upload to S3 using s3cmd
import argparse
import subprocess
import shlex
import datetime
import os
parser = argparse.ArgumentParser(description='pg_dump and upload to S3')
parser.add_argument('database', nargs=1)
parser.add_argument('-U', '--user')
parser.add_argument('--pg_options', default='-Fc -O -x')
parser.add_argument('-f', '--filename', default='{database} {date.year}-{date.month}-{date.day}.backup')
parser.add_argument('--nocleanup', action='store_false', dest='cleanup')
parser.add_argument('--s3_options', default='--acl-private')
parser.add_argument('-b', '--bucket', required=True)
#parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator')
#parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=max, help='sum the integers (default: find the max)')
args = parser.parse_args()
files = []
pg_dump = ['pg_dump']
if args.user:
pg_dump.extend(['-U', args.user])
if args.pg_options:
pg_dump.extend(shlex.split(args.pg_options))
if args.filename:
filename = args.filename.format(database=args.database[0], date=datetime.datetime.now())
files.append(filename)
pg_dump.extend(['-f', filename])
pg_dump.extend(args.database)
print 'Dumping {database}'.format(database=args.database)
subprocess.check_output(pg_dump, stderr=subprocess.STDOUT)
print 'Done'
s3cmd = ['s3cmd']
if args.s3_options:
s3cmd.extend(shlex.split(args.s3_options))
s3cmd.append('put')
s3cmd.extend(files)
s3cmd.append(args.bucket)
print 'Uploading to S3'
print ' '.join(s3cmd)
subprocess.check_output(s3cmd)
print 'Done'
if args.cleanup:
print 'Cleaning up'
for f in files:
print 'Removing {}'.format(f)
os.remove(f)
print 'Done'
#FILENAME="RD Production `date +\%Y-\%m-\%d`.backup"
#pg_dump -U donations -Fc -O -x -f "$FILENAME" donations
#s3cmd --acl-private put "$FILENAME" s3://rhino-pgbackups && rm "$FILENAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.