Skip to content

Instantly share code, notes, and snippets.

@godber
Forked from tyrm/mastodon_db_backup.sh
Created November 23, 2022 11:28
Show Gist options
  • Save godber/7646e3561986d72776d1027e0d11e120 to your computer and use it in GitHub Desktop.
Save godber/7646e3561986d72776d1027e0d11e120 to your computer and use it in GitHub Desktop.
DB Backup
#!/bin/bash
set -e
. /etc/default/backup
INSTANCE=$1
case $INSTANCE in
ft-relay)
PG_DB_NAME=ft_relay
PG_USERNAME=ft_relay
;;
ptzo-masto)
PG_DB_NAME=mastodon_production
PG_USERNAME=mastodon
;;
walkman-masto)
PG_DB_NAME=walkman_production
PG_USERNAME=walkman
;;
*)
echo Instance code $INSTANCE unknown.
exit 1
;;
esac
S3PATH=ph/backups/db/$(hostname)/$(date +%Y)/$(date +%m)/$(date +%d)
TARGETFILE=$INSTANCE.sql.gz
WORKDIR=/var/lib/backup
rm -f $WORKDIR/$TARGETFILE
rm -f $WORKDIR/$TARGETFILE.gpg
echo Dumping Database
pg_dump -h localhost -U $PG_USERNAME -p 5432 $PG_DB_NAME | nice gzip > $WORKDIR/$TARGETFILE
echo Encrpyting File
gpg --yes --batch --passphrase=$GPG_PASSWORD -c $WORKDIR/$TARGETFILE
echo Uploading to Cloud
/usr/local/bin/mc cp $WORKDIR/$TARGETFILE.gpg $S3PATH/$TARGETFILE.gpg
echo Vacuuming Database
/usr/bin/psql -h localhost -U vacuum -p 5432 $PG_DB_NAME -c 'VACUUM FULL VERBOSE;'
rm -f $WORKDIR/$TARGETFILE
rm -f $WORKDIR/$TARGETFILE.gpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment