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:
- 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 [email protected]: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
- edit your crontab:
sudo crontab -e
# midnight backup
0 0 * * * /root/backup
Nice. Maybe use gzip's "--rsyncable" option?