You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

#!/usr/bin/env sh
# Usage:
# postgres_backup.sh [man] //Manual backup
# postgres_backup.sh auto //Auto backup (separate backup location)
# postgres_backup.sh [auto|man] debug //Outputs success messages to STDOUT
# CONFIG
### Example Config File ############
###
### PORT=5432
### DIR=/var/lib/pgsql/backup
### COMPRESSION=zstd
### COMP_LEVEL=14
### THREADS=2
###
####################################
source ${HOME}/.postgres_backup.conf
# END CONFIG
## Check if automated, set appropriate directory
[ "$1" = "auto" ] && DIR=${DIR}/auto || DIR=${DIR}/manual
## Set date for timestamp
DATE=$(date +%Y%m%d-%H%M)
## Get list of DBs
list_db=$(psql -p${PORT} -U postgres -t -c 'select datname from pg_database;' | grep -v 'template0\|template1\|postgres' | sed '/^$/d')
case $COMPRESSION in
xz)
[ "$2" = "debug" ] && echo "Backed up globals using $COMPRESSION -${COMP_LEVEL}"
pg_dumpall -p${PORT} -g | xz -T${THREADS} -${COMP_LEVEL} - > ${DIR}/globals_${DATE}.dmp.xz
[ "$2" = "debug" ] && echo "Starting DB dump loop..."
for db in $list_db; do
pg_dump -O $db | xz -T${THREADS} -${COMP_LEVEL} - > ${DIR}/${db}-${DATE}.dmp.xz
[ "$2" = "debug" ] && echo "Backed up $db using $COMPRESSION -${COMP_LEVEL}"
done
;;
zstd)
[ "$2" = "debug" ] && echo "Backed up globals using $COMPRESSION -${COMP_LEVEL}"
pg_dumpall -p${PORT} -g | zstd -T${THREADS} -${COMP_LEVEL} -z - > ${DIR}/globals_${DATE}.dmp.zst
[ "$2" = "debug" ] && echo "Starting DB dump loop..."
for db in $list_db; do
pg_dump -O $db | zstd -T${THREADS} -${COMP_LEVEL} -z - > ${DIR}/${db}-${DATE}.dmp.zst
[ "$2" = "debug" ] && echo "Backed up $db using $COMPRESSION -${COMP_LEVEL}"
done
;;
*)
echo "ERROR 11: Compression not configured correctly!" >2
exit 11
;;
esac
exit 0