Python script to backup up a postgres database to s3

leroykayanda

leroykayanda

Posted on February 5, 2023

Python script to backup up a postgres database to s3

This script backups up a postgres db to S3. It sends any errors that occur to slack. Successful backups are logged in cloudwatch. DB credentials are retrieved from AWS secrets manager.

Use this cron to schedule the script to run every hour.

* * * * /usr/bin/python3 /path/to/script/script.py <app_name> <db_name> <slack webhook url> <secrets manager ARN> <s3 bucket name> <cloudwatch loggroup> <cloudwatch logstream>

These libraries need to be installed.

amazon-linux-extras install postgresql14
pip3 install urllib3
pip3 install boto3
Enter fullscreen mode Exit fullscreen mode

AWS Permissions Needed

s3:PutObject 
secretsmanager:GetSecretValue
logs:PutLogEvents
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
leroykayanda
leroykayanda

Posted on February 5, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related