postgresql backup using python

Solutions on MaxInterview for postgresql backup using python by the best coders in the world

showing results for - "postgresql backup using python"
Laura
14 Nov 2018
1import os
2import sys
3import subprocess
4from optparse import OptionParser
5from datetime import datetime
6
7import boto
8from boto.s3.key import Key
9
10
11DB_USER = 'databaseuser'
12DB_NAME = 'databasename'
13
14BACKUP_PATH = r'/webapps/myapp/db_backups'
15
16FILENAME_PREFIX = 'myapp.backup'
17
18# Amazon S3 settings.
19AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
20AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
21AWS_BUCKET_NAME = 'myapp-db-backups'
22
23
24def main():
25    parser = OptionParser()
26    parser.add_option('-t', '--type', dest='backup_type',
27                      help="Specify either 'hourly' or 'daily'.")
28
29    now = datetime.now()
30
31    filename = None
32    (options, args) = parser.parse_args()
33    if options.backup_type == 'hourly':
34        hour = str(now.hour).zfill(2)
35        filename = '%s.h%s' % (FILENAME_PREFIX, hour)
36    elif options.backup_type == 'daily':
37        day_of_year = str(now.timetuple().tm_yday).zfill(3)
38        filename = '%s.d%s' % (FILENAME_PREFIX, day_of_year)
39    else:
40        parser.error('Invalid argument.')
41        sys.exit(1)
42
43    destination = r'%s/%s' % (BACKUP_PATH, filename)
44
45    print 'Backing up %s database to %s' % (DB_NAME, destination)
46    ps = subprocess.Popen(
47        ['pg_dump', '-U', DB_USER, '-Fc', DB_NAME, '-f', destination],
48        stdout=subprocess.PIPE
49    )
50    output = ps.communicate()[0]
51    for line in output.splitlines():
52        print line
53
54    print 'Uploading %s to Amazon S3...' % filename
55    upload_to_s3(destination, filename)
56
57
58def upload_to_s3(source_path, destination_filename):
59    """
60    Upload a file to an AWS S3 bucket.
61    """
62    conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
63    bucket = conn.get_bucket(AWS_BUCKET_NAME)
64    k = Key(bucket)
65    k.key = destination_filename
66    k.set_contents_from_filename(source_path)
67
68
69if __name__ == '__main__':
70    main()