Skip to content

Instantly share code, notes, and snippets.

@audy
Last active March 29, 2022 16:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save audy/52215fc35ac163006f42 to your computer and use it in GitHub Desktop.
Save audy/52215fc35ac163006f42 to your computer and use it in GitHub Desktop.
dokku postgresql rsync nightly backup

dokku postgresql rsync nightly backup

This is a rough guide to automating a nightly 7-day rolling remote backup of your PostgreSQL database(s) for your dokku app(s). There is a slight difference if you are using an older version of Dokku.

This will save the comprssed output of pg_dump as well as the contents of the /home directory on your dokku machine (just in case).

You will need to create a shell script in /root and edit the cron file.

Optionally, you can use Pushover to send a push notification to your phone when backups succeed.

On your Dokku box:

  1. create a backup script. I put mine in /root/backup:
# /root/backup script

#
# dump postgres databases
#
echo "dumping postgresql database"
date >> /home/backup_history.txt
daynum=$(date +%w)

# for audy's fork of https://github.com/jeffutter/dokku-postgresql-plugin
# (see https://github.com/jeffutter/dokku-postgresql-plugin/pull/18)
/usr/local/bin/dokku postgresql:dump_all | gzip > "/home/dokku/psql-dump-day-${daynum}.sql.gz"

# for Kloadut's dokku-pg-plugin (now default in Dokku)
# change $app_name for your app. Repeat for each app.
/usr/local/bin/dokku postgresql:dump $app_name | gzip > "/home/dokku/psql-dump-day-${app_name}-${daynum}.sql.gz"

#
# rsync /home to a remote location
# (make sure you've shared/accepted keys ahead of time stuff)
#

echo "syncing with remote"
rsync \
        --compress \
        --archive \
        --delete \
        --progress \
        /home user@example.com:dokku-backup
        
# (optional) wake yourself up in the middle of the night
# when backup is successful.
# (won't execute if anything breaks because of set -x)
/usr/local/bin/pushover "backup completed on ${HOST}"

Make sure to chmod +x /root/backup

  1. edit your crontab:

sudo crontab -e

# midnight backup
0 0 * * * /root/backup
@fomcl
Copy link

fomcl commented Mar 29, 2022

Nice. Maybe use gzip's "--rsyncable" option?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment