Cron Job for Postgres Backups with Dokku

Found a script that backs up Postgres data for a given Dokku app.

Note: This script uses the Dokku PG plugin.

Contents of the script (dokku-pg-backup):

#! /bin/bash

# directory to save backups in, must be rwx by postgres user
BASE_DIR="/var/backups/postgres"
YMD=$(date "+%Y-%m-%d")
DIR="$BASE_DIR/$YMD"
mkdir -p $DIR
cd $DIR

# make database backup
dokku postgresql:dump $1 | gzip -9 > "$DIR/db.out.gz"

# delete backup files older than 7 days
OLD=$(find $BASE_DIR -type d -mtime +7)
if [ -n "$OLD" ] ; then
        echo deleting old backup files: $OLD
        echo $OLD | xargs rm -rfv
fi

You can call the script for the app:

./dokku-pg-backup appname

To perform a daily backup, I placed the script in the /etc/cron.daily.

Note: I'm using Ubuntu, and per the CronHowto docs, the script file cannot not accept a file name containing a period. The cron job will silently fail.

My fork of the gist.

Stay Updated

Get weekly insights on AI, engineering leadership, and startups.