Published at ·  

Postgres Backup

Postgres Backup is a personal project that allows you to backup PostgreSQL databases to a S3 compatible storage.
Baptiste Bronsin

Baptiste Bronsin

@baptiste_bronsin

A database backup solution for PostgreSQL

I work on many projects that use PostgreSQL and I needed a simple way to back up my databases. Initially, I connected manually to each database and used the pg_dump command to create backup files. This process quickly became tedious and error-prone, especially when managing multiple databases on a recurring schedule. Sometimes I even forgot to run the backups because I had other things to do on my Sundays ☀️.

Features

This project is designed to run in containerized environments (docker compose and kubernetes) and can be automated using cron jobs. It allows you to backup your PostgreSQL databases to a S3 compatible storage, such as MinIO, AWS S3, Cloudflare R2, etc.

To be used in every project, this script is designed to be flexible and easy to configure. You can specify the databases you want to backup, the compression algorithm and the storage location.

Arguments

VariableDescriptionRequiredExample
DB_HOSTThe host of the databaseyespostgres
DB_PORTThe port of the databaseyes5432
DB_USERThe user of the databaseyespostgres
DB_PASSWORDThe password of the databaseyespostgres
DB_NAMEThe name of the databaseyesplannify
---------
BACKUP_DIRThe directory of the backupno’daily’, ‘weekly’, ‘monthly’
BACKUP_MAX_BEFORE_DELETEThe maximum number of backup before deleting the oldest oneno7
BACKUP_COMPRESSIONThe compression method to use for the backup file (no compression by default)nogzip, xz
---------
S3_ENDPOINTThe bucket endpointyeshttps://…
S3_ACCESS_TOKENThe access token of your provider accountyes1234567890
S3_SECRET_ACCESS_TOKENThe secret access token of your provider accountyes1234567890
S3_BUCKETThe S3 bucket of your accountyesplannify

Technologies Used

This project is built using basic Bash scripting and Docker to create a containerized backup solution that can easily run in any environment.

Dockerfile

The Dockerfile is based on the official Postgres image and includes all necessary dependencies to run the backup script.

The container runs as a specific user, backupuser, who has the required permissions to access the database and perform backups securely.

CI

A basic GitHub Action CI pipeline is configured to build the Docker image on each tag push.