|
|
|
@ -3,7 +3,7 @@
|
|
|
|
|
# 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
|
|
|
|
|
# postgres_backup.sh [auto|man] debug //Outputs status messages to STDOUT
|
|
|
|
|
|
|
|
|
|
# CONFIG
|
|
|
|
|
### Example Config File ############
|
|
|
|
@ -20,33 +20,34 @@ source ${HOME}/.postgres_backup.conf
|
|
|
|
|
# END CONFIG
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Check if debug mode is called, and define debug message function
|
|
|
|
|
[ "$2" = "debug" ] && DEBUG="true" || DEBUG="false"
|
|
|
|
|
msg_debug() {
|
|
|
|
|
[ "$DEBUG" = "true" ] && echo -e "$1"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Check if automated, set appropriate directory
|
|
|
|
|
[ "$1" = "auto" ] && DIR=${DIR}/auto || DIR=${DIR}/manual
|
|
|
|
|
msg_debug "Backup directory set to $(tput bold)${DIR}$(tput sgr0)"
|
|
|
|
|
|
|
|
|
|
## Set date for timestamp
|
|
|
|
|
DATE=$(date +%Y%m%d-%H%M)
|
|
|
|
|
msg_debug "Date set to $(tput bold)${DATE}$(tput sgr0)"
|
|
|
|
|
|
|
|
|
|
## 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
|
|
|
|
|
compress="xz -T${THREADS} -${COMP_LEVEL} -"
|
|
|
|
|
comp_ext="xz"
|
|
|
|
|
msg_debug "Compression set to $(tput bold)${COMPRESSION}$(tput sgr0)"
|
|
|
|
|
;;
|
|
|
|
|
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
|
|
|
|
|
compress="zstd -T${THREADS} -${COMP_LEVEL} -z -"
|
|
|
|
|
comp_ext="zst"
|
|
|
|
|
msg_debug "Compression set to $(tput bold)${COMPRESSION}$(tput sgr0)"
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
echo "ERROR 11: Compression not configured correctly!" >2
|
|
|
|
@ -54,5 +55,18 @@ case $COMPRESSION in
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
msg_debug "\nStarting backup procedures...\n"
|
|
|
|
|
|
|
|
|
|
## Backup globals
|
|
|
|
|
pg_dumpall -p${PORT} -g | eval $compress > ${DIR}/globals_${DATE}.dmp.${comp_ext}
|
|
|
|
|
msg_debug "Backed up $(tput setaf 2)$(tput bold)globals$(tput sgr0) as '$(tput setaf 3)${DIR}/globals_${DATE}.dmp.${comp_ext}$(tput sgr0)' using '$(tput bold)${compress}$(tput sgr0)'"
|
|
|
|
|
|
|
|
|
|
msg_debug "Starting DB dump loop..."
|
|
|
|
|
for db in $list_db; do
|
|
|
|
|
target="${DIR}/${db}_${DATE}.dmp.${comp_ext}"
|
|
|
|
|
pg_dump -O $db | eval $compress > $target
|
|
|
|
|
msg_debug "Backed up '$(tput setaf 2)$(tput bold)${db}$(tput sgr0)' as '$(tput setaf 3)${target}$(tput sgr0)' using '$(tput bold)${compress}$(tput sgr0)'"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
|
|
|
|
|
|