s3_backups/postgres_to_s3.py
def backup():
    """Backup Postgres to S3 using pg_dump"""

    key_name = S3_KEY_NAME
    if not key_name.endswith("/") and key_name != "":
        key_name = "%s/" % key_name

    # add the file name date suffix
    now = datetime.now()
    FILENAME_SUFFIX = "_%(year)d%(month)02d%(day)02d_%(hour)02d%(minute)02d%(second)02d" % {
        'year': now.year,
        'month': now.month,
        'day': now.day,
        'hour': now.hour,
        'minute': now.minute,
        'second': now.second
    }
    FILENAME = ARCHIVE_NAME + FILENAME_SUFFIX + ".tar.gz"

    log.info("Preparing " + FILENAME + " from the database dump ...")

    # create postgres databases dump
    with tempfile.NamedTemporaryFile() as t1:
        proc1 = subprocess.Popen(POSTGRES_DUMP_PATH, shell=True, universal_newlines=True, stdout=t1)
        proc1.wait()
        t1.flush()

        # create tar.gz for the above two files
        t2 = tempfile.NamedTemporaryFile()
        tar = tarfile.open(t2.name, "w|gz")
        tar.add(t1.name, ARCHIVE_NAME + ".sql")
        tar.close()

        log.info("Uploading the " + FILENAME + " file to Amazon S3 ...")

        # get bucket
        conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)

        try:
            bucket = conn.get_bucket(S3_BUCKET_NAME)
        except S3ResponseError:
            sys.stderr.write("There is no bucket with the name \"" + S3_BUCKET_NAME + "\" in your Amazon S3 account\n")
            sys.stderr.write("Error: Please enter an appropriate bucket name and re-run the script\n")
            t2.close()
            return

        # upload file to Amazon S3
        k = Key(bucket)
        k.key = key_name + FILENAME
        k.set_contents_from_filename(t2.name)
        t2.close()

        log.info("Sucessfully uploaded the archive to Amazon S3")
Backup Postgres to S3 using pg_dump
Backup Postgres to S3 using pg_dump
Backup Postgres to S3 using pg_dump
def backup():
    key_name = S3_KEY_NAME
    if not key_name.endswith("/") and key_name != "":
        key_name = "%s/" % key_name
    now = datetime.now()
    FILENAME_SUFFIX = "_%(year)d%(month)02d%(day)02d_%(hour)02d%(minute)02d%(second)02d" % {
        'year': now.year,
        'month': now.month,
        'day': now.day,
        'hour': now.hour,
        'minute': now.minute,
        'second': now.second
    }
    FILENAME = ARCHIVE_NAME + FILENAME_SUFFIX + ".tar.gz"
    log.info("Preparing " + FILENAME + " from the database dump ...")
    with tempfile.NamedTemporaryFile() as t1:
        proc1 = subprocess.Popen(POSTGRES_DUMP_PATH, shell=True, universal_newlines=True, stdout=t1)
        proc1.wait()
        t1.flush()
        t2 = tempfile.NamedTemporaryFile()
        tar = tarfile.open(t2.name, "w|gz")
        tar.add(t1.name, ARCHIVE_NAME + ".sql")
        tar.close()
        log.info("Uploading the " + FILENAME + " file to Amazon S3 ...")
        conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
        try:
            bucket = conn.get_bucket(S3_BUCKET_NAME)
        except S3ResponseError:
            sys.stderr.write("There is no bucket with the name \"" + S3_BUCKET_NAME + "\" in your Amazon S3 account\n")
            sys.stderr.write("Error: Please enter an appropriate bucket name and re-run the script\n")
            t2.close()
            return
        k = Key(bucket)
        k.key = key_name + FILENAME
        k.set_contents_from_filename(t2.name)
        t2.close()
        log.info("Sucessfully uploaded the archive to Amazon S3")
def backup ( ) : key_name = S3_KEY_NAME if not key_name . endswith ( "/" ) and key_name != "" : key_name = "%s/" % key_name now = datetime . now ( ) FILENAME_SUFFIX = "_%(year)d%(month)02d%(day)02d_%(hour)02d%(minute)02d%(second)02d" % { 'year' : now . year , 'month' : now . month , 'day' : now . day , 'hour' : now . hour , 'minute' : now . minute , 'second' : now . second } FILENAME = ARCHIVE_NAME + FILENAME_SUFFIX + ".tar.gz" log . info ( "Preparing " + FILENAME + " from the database dump ..." ) with tempfile . NamedTemporaryFile ( ) as t1 : proc1 = subprocess . Popen ( POSTGRES_DUMP_PATH , shell = True , universal_newlines = True , stdout = t1 ) proc1 . wait ( ) t1 . flush ( ) t2 = tempfile . NamedTemporaryFile ( ) tar = tarfile . open ( t2 . name , "w|gz" ) tar . add ( t1 . name , ARCHIVE_NAME + ".sql" ) tar . close ( ) log . info ( "Uploading the " + FILENAME + " file to Amazon S3 ..." ) conn = S3Connection ( AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY ) try : bucket = conn . get_bucket ( S3_BUCKET_NAME ) except S3ResponseError : sys . stderr . write ( "There is no bucket with the name \"" + S3_BUCKET_NAME + "\" in your Amazon S3 account\n" ) sys . stderr . write ( "Error: Please enter an appropriate bucket name and re-run the script\n" ) t2 . close ( ) return k = Key ( bucket ) k . key = key_name + FILENAME k . set_contents_from_filename ( t2 . name ) t2 . close ( ) log . info ( "Sucessfully uploaded the archive to Amazon S3" )
Backup Postgres to S3 using pg_dump
Backup Postgres to S3 using pg_dump
"""Backup Postgres to S3 using pg_dump""" # add the file name date suffix # create postgres databases dump # create tar.gz for the above two files # get bucket # upload file to Amazon S3