DEV Community

leroykayanda
leroykayanda

Posted on • Edited on

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

Top comments (0)