Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

postgresql backup using python

import os
import sys
import subprocess
from optparse import OptionParser
from datetime import datetime

import boto
from boto.s3.key import Key


DB_USER = 'databaseuser'
DB_NAME = 'databasename'

BACKUP_PATH = r'/webapps/myapp/db_backups'

FILENAME_PREFIX = 'myapp.backup'

# Amazon S3 settings.
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
AWS_BUCKET_NAME = 'myapp-db-backups'


def main():
    parser = OptionParser()
    parser.add_option('-t', '--type', dest='backup_type',
                      help="Specify either 'hourly' or 'daily'.")

    now = datetime.now()

    filename = None
    (options, args) = parser.parse_args()
    if options.backup_type == 'hourly':
        hour = str(now.hour).zfill(2)
        filename = '%s.h%s' % (FILENAME_PREFIX, hour)
    elif options.backup_type == 'daily':
        day_of_year = str(now.timetuple().tm_yday).zfill(3)
        filename = '%s.d%s' % (FILENAME_PREFIX, day_of_year)
    else:
        parser.error('Invalid argument.')
        sys.exit(1)

    destination = r'%s/%s' % (BACKUP_PATH, filename)

    print 'Backing up %s database to %s' % (DB_NAME, destination)
    ps = subprocess.Popen(
        ['pg_dump', '-U', DB_USER, '-Fc', DB_NAME, '-f', destination],
        stdout=subprocess.PIPE
    )
    output = ps.communicate()[0]
    for line in output.splitlines():
        print line

    print 'Uploading %s to Amazon S3...' % filename
    upload_to_s3(destination, filename)


def upload_to_s3(source_path, destination_filename):
    """
    Upload a file to an AWS S3 bucket.
    """
    conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    bucket = conn.get_bucket(AWS_BUCKET_NAME)
    k = Key(bucket)
    k.key = destination_filename
    k.set_contents_from_filename(source_path)


if __name__ == '__main__':
    main()
Comment

PREVIOUS NEXT
Code Example
Python :: python check array exists 
Python :: python: convert variable as character 
Python :: python how to add to a list 
Python :: selenium.common.exceptions.TimeoutException: Message: 
Python :: python show map with coordinates 
Python :: train dev test split sklearn 
Python :: logarithmic scale fitting python 
Python :: best fit line python log log scale 
Python :: how to rename columns in pandas dataframe 
Python :: string in list python 
Python :: install google cloud python 
Python :: python ide online 
Python :: hugging face change directory model 
Python :: install fastapi 
Python :: logarithms python 
Python :: f-string print 
Python :: Replace all the empty rows in the column with the value that you have identified 
Python :: python check if input() gives error 
Python :: clear list 
Python :: read image and resize 
Python :: def python 
Python :: django filter by category 
Python :: python pandas how to select range of data 
Python :: Modify a Python interpreter 
Python :: numpy put arrays in columns 
Python :: matlab filter in python 
Python :: Error: getaddrinfo ENOTFOUND www.python.org www.python.org:443 Downloading Python failed. Error: { Error: getaddrinfo ENOTFOUND www.python.org www.python.org:443 
Python :: find nan values in pandas 
Python :: pandas filter on two columns 
Python :: What does if __name_=="_main__": do? 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =