Last active
December 11, 2021 05:08
-
-
Save dbrgn/cf30e2524ea21d80c853 to your computer and use it in GitHub Desktop.
PostgreSQL Backup Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| TIMESTAMP=$(date +\%Y\%m\%d_\%H\%M\%S) | |
| DEST=/var/backups/db/ | |
| OWNER=backuppc | |
| GROUP=backuppc | |
| echo -n "Creating temp dir..." | |
| TMPDIR=$(mktemp -d) | |
| echo " Done." | |
| echo -n "Backup globals..." | |
| pg_dumpall -U postgres --globals-only | gzip -c > $TMPDIR/pg.globals.$TIMESTAMP.sql.gz || exit 1 | |
| echo " Done." | |
| echo "Backup databases" | |
| sql="SELECT datname FROM pg_database WHERE NOT datistemplate AND datname <> 'postgres'" | |
| databases=$(psql -U postgres --tuples-only -P format=unaligned -c "$sql") | |
| for db in $databases; do | |
| echo -n "- $db..." | |
| pg_dump -U postgres --format=custom --blobs $db --file=$TMPDIR/pg.$db.$TIMESTAMP.pgdump || exit 1 | |
| echo " Done." | |
| done | |
| echo -n "Update permissions..." | |
| chown $OWNER:$GROUP $TMPDIR/* | |
| chmod 600 $TMPDIR/* | |
| echo " Done." | |
| echo -n "Move backups..." | |
| mv $TMPDIR/* $DEST/ | |
| rm -r $TMPDIR | |
| echo " Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment