convert debug messages to normal messages shown only when !auto

master
Aaron Johnon 4 years ago
parent 1c5cab6706
commit 207bb214ab

@ -1,9 +1,8 @@
#!/usr/bin/env sh #!/usr/bin/env sh
# Usage: # Usage:
# postgres_backup.sh [man] //Manual backup # postgres_backup.sh [man] //Manual backup, full output
# postgres_backup.sh auto //Auto backup (separate backup location) # postgres_backup.sh auto //Auto backup (separate backup location, only outputs STDERR messages)
# postgres_backup.sh [auto|man] debug //Outputs status messages to STDOUT
# CONFIG # CONFIG
### Example Config File ############ ### Example Config File ############
@ -15,25 +14,30 @@
### THREADS=2 ### THREADS=2
### ###
#################################### ####################################
#
# Load config file:
source ${HOME}/.postgres_backup.conf 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 ## Check if automated, set appropriate directory
[ "$1" = "auto" ] && DIR=${DIR}/auto || DIR=${DIR}/manual if [ "$1" = "auto" ]; then
msg_debug "Backup directory set to $(tput bold)${DIR}$(tput sgr0)" AUTO="true"
DIR=${DIR}/auto
else
AUTO="false"
DIR=${DIR}/manual
fi
## Define message command
msg_show() {
[ "$AUTO" = "false" ] && printf "$1"
}
msg_show "Backup directory set to $(tput bold)${DIR}$(tput sgr0)\n"
## Set date for timestamp ## Set date for timestamp
DATE=$(date +%Y%m%d-%H%M) DATE=$(date +%Y%m%d-%H%M)
msg_debug "Date set to $(tput bold)${DATE}$(tput sgr0)" msg_show "Timestamp set to $(tput bold)${DATE}$(tput sgr0)\n"
## Get list of DBs ## 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') list_db=$(psql -p${PORT} -U postgres -t -c 'select datname from pg_database;' | grep -v 'template0\|template1\|postgres' | sed '/^$/d')
@ -42,30 +46,29 @@ case $COMPRESSION in
xz) xz)
compress="xz -T${THREADS} -${COMP_LEVEL} -" compress="xz -T${THREADS} -${COMP_LEVEL} -"
comp_ext="xz" comp_ext="xz"
msg_debug "Compression set to $(tput bold)${COMPRESSION}$(tput sgr0)" msg_show "Compression set to $(tput bold)${COMPRESSION}$(tput sgr0)\n"
;; ;;
zstd) zstd)
compress="zstd -T${THREADS} -${COMP_LEVEL} -z -" compress="zstd -T${THREADS} -${COMP_LEVEL} -z -"
comp_ext="zst" comp_ext="zst"
msg_debug "Compression set to $(tput bold)${COMPRESSION}$(tput sgr0)" msg_show "Compression set to $(tput bold)${COMPRESSION}$(tput sgr0)\n"
;; ;;
*) *)
echo "ERROR 11: Compression not configured correctly!" >2 echo "ERROR 11: Compression not configured correctly!" >&2
exit 11 exit 11
;; ;;
esac esac
msg_debug "\nStarting backup procedures...\n" msg_show "\nStarting backup procedures...\n\n"
## Backup globals ## Backup globals
pg_dumpall -p${PORT} -g | eval $compress > ${DIR}/globals_${DATE}.dmp.${comp_ext} 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_show "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)\n"
msg_debug "Starting DB dump loop..."
for db in $list_db; do for db in $list_db; do
target="${DIR}/${db}_${DATE}.dmp.${comp_ext}" target="${DIR}/${db}_${DATE}.dmp.${comp_ext}"
pg_dump -O $db | eval $compress > $target 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)'" msg_show "Backed up $(tput setaf 2)$(tput bold)${db}$(tput sgr0) as $(tput setaf 3)${target}$(tput sgr0) using $(tput bold)${compress}$(tput sgr0)\n"
done done
exit 0 exit 0

Loading…
Cancel
Save