#!/bin/bash
# Backup a database *locally* to /backups/.

# Sleep a bit so we do not have a thundering herd on db hosts
sleep $[ ( $RANDOM % 7200 )  + 1 ]s

DB=$1

# Make our latest backup
# Make it use a limited number of threads because pxz will use all the
# cpus which causes pg_dump to starve which causes...

/usr/bin/pg_dump --exclude-table-data=sessions -C $DB | /usr/bin/pxz -T24 > /backups/$DB-$(date +%F).dump.xz

# Also, delete the backup from a few days ago.
rm -f /backups/$DB-$(date --date="1 days ago" +%F).dump.xz
