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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay