Clean up code for pg_db_dump_file
Fixes for wrong empty settings Fix log path not set correct if given as parameter Fix all shell linting problems
This commit is contained in:
@@ -56,7 +56,7 @@ OPTARG_REGEX="^-";
|
||||
# log path
|
||||
LOG_PATH='';
|
||||
# base path for PostgreSQL binary
|
||||
DBPATH_BASE='';
|
||||
# DBPATH_BASE='';
|
||||
# defaults
|
||||
_BACKUPDIR='/mnt/backup/db_dumps_fc/';
|
||||
_DB_VERSION=$(pgv=$(pg_dump --version| grep "pg_dump" | cut -d " " -f 3); if [[ $(echo "${pgv}" | cut -d "." -f 1) -ge 10 ]]; then echo "${pgv}" | cut -d "." -f 1; else echo "${pgv}" | cut -d "." -f 1,2; fi );
|
||||
@@ -68,7 +68,7 @@ _EXCLUDE=''; # space separated list of database names
|
||||
_INCLUDE=''; # space seperated list of database names
|
||||
REDHAT=0;
|
||||
AMAZON=0;
|
||||
CONN_DB_HOST='';
|
||||
# CONN_DB_HOST='';
|
||||
ERROR=0;
|
||||
|
||||
# set options
|
||||
@@ -125,79 +125,97 @@ while getopts ":ctsgnk:b:i:d:e:u:h:p:l:L:ram" opt; do
|
||||
fi;
|
||||
# set options
|
||||
case ${opt} in
|
||||
t|test)
|
||||
# t|test)
|
||||
t)
|
||||
TEST=1;
|
||||
;;
|
||||
g|globals)
|
||||
# g|globals)
|
||||
g)
|
||||
GLOBALS=0;
|
||||
;;
|
||||
c|clean-up-before)
|
||||
# c|clean-up-before)
|
||||
c)
|
||||
PRE_RUN_CLEAN_UP=1;
|
||||
;;
|
||||
s|sslmode)
|
||||
# s|sslmode)
|
||||
s)
|
||||
SSLMODE=enable;
|
||||
;;
|
||||
k|keep)
|
||||
# k|keep)
|
||||
k)
|
||||
KEEP=${OPTARG};
|
||||
;;
|
||||
n|number-keep)
|
||||
# n|number-keep)
|
||||
n)
|
||||
CLEAN_NUMBER=1;
|
||||
;;
|
||||
b|backuppath)
|
||||
# b|backuppath)
|
||||
b)
|
||||
if [ -z "${BACKUPDIR}" ]; then
|
||||
BACKUPDIR="${OPTARG}";
|
||||
fi;
|
||||
;;
|
||||
i|ident)
|
||||
# i|ident)
|
||||
i)
|
||||
if [ -z "${DB_VERSION}" ]; then
|
||||
DB_VERSION=${OPTARG};
|
||||
SET_IDENT=1;
|
||||
fi;
|
||||
;;
|
||||
u|user)
|
||||
# u|user)
|
||||
u)
|
||||
if [ -z "${DB_USER}" ]; then
|
||||
DB_USER=${OPTARG};
|
||||
fi;
|
||||
;;
|
||||
h|hostname)
|
||||
# h|hostname)
|
||||
h)
|
||||
if [ -z "${DB_HOST}" ]; then
|
||||
DB_HOST=${OPTARG};
|
||||
fi;
|
||||
;;
|
||||
p|port)
|
||||
# p|port)
|
||||
p)
|
||||
if [ -z "${DB_PORT}" ]; then
|
||||
DB_PORT=${OPTARG};
|
||||
fi;
|
||||
;;
|
||||
l|login)
|
||||
# l|login)
|
||||
l)
|
||||
if [ -z "${DB_PASSWD}" ]; then
|
||||
DB_PASSWD=${OPTARG};
|
||||
fi;
|
||||
;;
|
||||
d|database)
|
||||
if [ ! -z "${INCLUDE}" ]; then
|
||||
# d|database)
|
||||
d)
|
||||
if [ -z "${INCLUDE}" ]; then
|
||||
INCLUDE=${INCLUDE}" ";
|
||||
fi;
|
||||
INCLUDE=${INCLUDE}${OPTARG};
|
||||
;;
|
||||
e|exclude)
|
||||
if [ ! -z "${EXCLUDE}" ]; then
|
||||
# e|exclude)
|
||||
e)
|
||||
if [ -z "${EXCLUDE}" ]; then
|
||||
EXCLUDE=${EXCLUDE}" ";
|
||||
fi;
|
||||
EXCLUDE=${EXCLUDE}${OPTARG};
|
||||
;;
|
||||
r|redhat)
|
||||
# r|redhat)
|
||||
r)
|
||||
REDHAT=1;
|
||||
;;
|
||||
a|amazon)
|
||||
# a|amazon)
|
||||
a)
|
||||
AMAZON=1;
|
||||
;;
|
||||
L|logpath)
|
||||
if [ ! -z "{$LOG_PATH}" ]; then
|
||||
# L|logpath)
|
||||
L)
|
||||
if [ -z "${LOG_PATH}" ]; then
|
||||
LOG_PATH="${OPTARG}";
|
||||
fi;
|
||||
;;
|
||||
m|manual)
|
||||
# m|manual)
|
||||
m)
|
||||
usage;
|
||||
exit 0;
|
||||
;;
|
||||
@@ -222,7 +240,7 @@ if [ "${REDHAT}" -eq 1 ] && [ "${AMAZON}" -eq 1 ]; then
|
||||
fi;
|
||||
|
||||
# if we have numeric keep and keep number is set to 0 abort
|
||||
if [ ${CLEAN_NUMBER} -eq 1 ] && [ ${KEEP} -lt 1 ]; then
|
||||
if [ "${CLEAN_NUMBER}" -eq 1 ] && [ "${KEEP}" -lt 1 ]; then
|
||||
echo "If keep in numbers is on, keep must be at least 1 or higher";
|
||||
exit 0;
|
||||
fi;
|
||||
@@ -233,7 +251,7 @@ for name in BACKUPDIR DB_VERSION DB_USER DB_PASSWD DB_HOST DB_PORT EXCLUDE INCLU
|
||||
if [ -z "${!name}" ]; then
|
||||
# add the _ for the default name
|
||||
default="_"${name};
|
||||
eval ${name}=\${!default};
|
||||
declare $name=${!default}
|
||||
fi;
|
||||
done;
|
||||
|
||||
@@ -273,7 +291,7 @@ exec &> >(tee -a "${LOG}");
|
||||
|
||||
# check DB port is valid number
|
||||
if ! [[ "${DB_PORT}" =~ ${PORT_REGEX} ]]; then
|
||||
echo "The port needs to be a valid number: ${_port}";
|
||||
echo "The port needs to be a valid number: ${DB_PORT}";
|
||||
exit 0;
|
||||
fi;
|
||||
|
||||
@@ -284,13 +302,19 @@ else
|
||||
BC_OK=0;
|
||||
fi;
|
||||
|
||||
# the default pg params
|
||||
PG_PARAMS=("-U" "${DB_USER}" "-p" "${DB_PORT}")
|
||||
# if DB_HOST is set, we need to add -h to the command line
|
||||
# if nothing is set, DB_HOST is set to local so we know this is a "port" connection for later automatic restore
|
||||
if [ -z "${DB_HOST}" ]; then
|
||||
DB_HOST='local';
|
||||
else
|
||||
CONN_DB_HOST='-h '${DB_HOST};
|
||||
# CONN_DB_HOST='-h '${DB_HOST};
|
||||
PG_PARAMS+=("-h" "${DB_HOST}");
|
||||
fi;
|
||||
# copy to select
|
||||
PG_PARAMS_SELECT=("${PG_PARAMS[@]}");
|
||||
PG_PARAMS_SELECT+=("-d" "template1" "-t" "-A" "-F" "," "-X" "-q" "-c");
|
||||
|
||||
# set the binaries we need
|
||||
PG_PATH=${PG_BASE_PATH}${DB_VERSION}'/bin/';
|
||||
@@ -301,7 +325,7 @@ DB_TYPE='pgsql';
|
||||
db='';
|
||||
|
||||
# core abort if no core files found
|
||||
if [ ! -f ${PG_PSQL} ] || [ ! -f ${PG_DUMP} ] || [ ! -f ${PG_DUMPALL} ]; then
|
||||
if [ ! -f "${PG_PSQL}" ] || [ ! -f "${PG_DUMP}" ] || [ ! -f "${PG_DUMPALL}" ]; then
|
||||
echo "One of the core binaries (psql, pg_dump, pg_dumpall) could not be found.";
|
||||
echo "Search Path: ${PG_PATH}";
|
||||
echo "Perhaps manual ident set with -i is necessary";
|
||||
@@ -309,26 +333,28 @@ if [ ! -f ${PG_PSQL} ] || [ ! -f ${PG_DUMP} ] || [ ! -f ${PG_DUMPALL} ]; then
|
||||
exit 0;
|
||||
fi;
|
||||
|
||||
if [ ! -d ${BACKUPDIR} ] ; then
|
||||
if ! mkdir ${BACKUPDIR} ; then
|
||||
if [ ! -d "${BACKUPDIR}" ] ; then
|
||||
if ! mkdir "${BACKUPDIR}" ; then
|
||||
echo "Cannot create backup directory: ${BACKUPDIR}"
|
||||
exit 0;
|
||||
fi
|
||||
fi
|
||||
# check if we can write into that folder
|
||||
touch ${BACKUPDIR}/tmpfile || echo "[!] touch failed";
|
||||
if [ ! -f ${BACKUPDIR}/tmpfile ]; then
|
||||
touch "${BACKUPDIR}/tmpfile" || echo "[!] touch failed";
|
||||
if [ ! -f "${BACKUPDIR}/tmpfile" ]; then
|
||||
echo "Cannot write to ${BACKUPDIR}";
|
||||
exit 0;
|
||||
else
|
||||
rm -f ${BACKUPDIR}/tmpfile;
|
||||
rm -f "${BACKUPDIR}/tmpfile";
|
||||
fi;
|
||||
# if backupdir is "." rewrite to pwd
|
||||
if [ "${BACKUPDIR}" == '.' ]; then
|
||||
BACKUPDIR=$(pwd);
|
||||
fi;
|
||||
# check if we can connect to template1 table, if not we abort here
|
||||
connect=$(${PG_PSQL} -U "${DB_USER}" ${CONN_DB_HOST} -p ${DB_PORT} -d template1 -t -A -F "," -X -q -c "SELECT datname FROM pg_catalog.pg_database WHERE datname = 'template1';") || echo "[!] pgsql connect error";
|
||||
_PG_PARAMS_SELECT=("${PG_PARAMS_SELECT[@]}");
|
||||
_PG_PARAMS_SELECT+=("SELECT datname FROM pg_catalog.pg_database WHERE datname = 'template1';");
|
||||
connect=$(${PG_PSQL} "${_PG_PARAMS_SELECT[@]}") || echo "[!] pgsql connect error";
|
||||
if [ "${connect}" != "template1" ]; then
|
||||
echo "Failed to connect to template1 with user '${DB_USER}' at host '${DB_HOST}' on port '${DB_PORT}'";
|
||||
exit 0;
|
||||
@@ -336,7 +362,7 @@ fi;
|
||||
|
||||
# if we have an ident override set, set a different DUMP VERSION here than the automatic one
|
||||
if [ "${SET_IDENT}" -eq 1 ]; then
|
||||
DUMP_DB_VERSION=$(pgv=$(${PG_PATH}/pg_dump --version| grep "pg_dump" | cut -d " " -f 3); if [[ $(echo "${pgv}" | cut -d "." -f 1) -ge 10 ]]; then echo "${pgv}" | cut -d "." -f 1; else echo "${pgv}" | cut -d "." -f 1,2; fi );
|
||||
DUMP_DB_VERSION=$(pgv=$("${PG_PATH}/pg_dump" --version | grep "pg_dump" | cut -d " " -f 3); if [[ $(echo "${pgv}" | cut -d "." -f 1) -ge 10 ]]; then echo "${pgv}" | cut -d "." -f 1; else echo "${pgv}" | cut -d "." -f 1,2; fi );
|
||||
else
|
||||
DUMP_DB_VERSION=${DB_VERSION};
|
||||
fi;
|
||||
@@ -355,15 +381,15 @@ function convert_time
|
||||
{
|
||||
timestamp=${1};
|
||||
# round to four digits for ms
|
||||
timestamp=$(printf "%1.4f" $timestamp);
|
||||
timestamp=$(printf "%1.4f" "$timestamp");
|
||||
# get the ms part and remove any leading 0
|
||||
ms=$(echo ${timestamp} | cut -d "." -f 2 | sed -e 's/^0*//');
|
||||
timestamp=$(echo ${timestamp} | cut -d "." -f 1);
|
||||
ms=$(echo "${timestamp}" | cut -d "." -f 2 | sed -e 's/^0*//');
|
||||
timestamp=$(echo "${timestamp}" | cut -d "." -f 1);
|
||||
timegroups=(86400 3600 60 1); # day, hour, min, sec
|
||||
timenames=("d" "h" "m" "s"); # day, hour, min, sec
|
||||
output=( );
|
||||
time_string='';
|
||||
for timeslice in ${timegroups[@]}; do
|
||||
for timeslice in "${timegroups[@]}"; do
|
||||
# floor for the division, push to output
|
||||
if [ ${BC_OK} -eq 1 ]; then
|
||||
output[${#output[*]}]=$(echo "${timestamp}/${timeslice}" | bc);
|
||||
@@ -375,8 +401,8 @@ function convert_time
|
||||
done;
|
||||
|
||||
for ((i=0; i<${#output[@]}; i++)); do
|
||||
if [ ${output[$i]} -gt 0 ] || [ ! -z "$time_string" ]; then
|
||||
if [ ! -z "${time_string}" ]; then
|
||||
if [ "${output[$i]}" -gt 0 ] || [ -n "$time_string" ]; then
|
||||
if [ -n "${time_string}" ]; then
|
||||
time_string=${time_string}" ";
|
||||
fi;
|
||||
time_string=${time_string}${output[$i]}${timenames[$i]};
|
||||
@@ -384,9 +410,9 @@ function convert_time
|
||||
done;
|
||||
# milliseconds must be filled, but we also check that they are non "nan" string
|
||||
# that can appear in the original value
|
||||
if [ ! -z ${ms} ] && [ "${ms}" != "nan" ]; then
|
||||
if [ ${ms} -gt 0 ]; then
|
||||
time_string=${time_string}" "${ms}"ms";
|
||||
if [ -n "${ms}" ] && [ "${ms}" != "nan" ]; then
|
||||
if [ "${ms}" -gt 0 ]; then
|
||||
time_string="${time_string} ${ms}ms";
|
||||
fi;
|
||||
fi;
|
||||
# just in case the time is 0
|
||||
@@ -405,15 +431,15 @@ function convert_bytes
|
||||
{
|
||||
bytes=${1};
|
||||
# use awk to calc it
|
||||
echo -n $(echo ${bytes} | awk 'function human(x) {
|
||||
s=" B KB MB GB TB EB PB YB ZB"
|
||||
while (x>=1024 && length(s)>1)
|
||||
{x/=1024; s=substr(s,4)}
|
||||
s=substr(s,1,4)
|
||||
xf=(s==" B ")?"%d ":"%.2f"
|
||||
return sprintf( xf"%s\n", x, s)
|
||||
echo -n "$(echo "${bytes}" | awk 'function human(x) {
|
||||
s=" B KB MB GB TB EB PB YB ZB"
|
||||
while (x>=1024 && length(s)>1)
|
||||
{x/=1024; s=substr(s,4)}
|
||||
s=substr(s,1,4)
|
||||
xf=(s==" B ")?"%d ":"%.2f"
|
||||
return sprintf( xf"%s\n", x, s)
|
||||
}
|
||||
{gsub(/^[0-9]+/, human($1)); print}');
|
||||
{gsub(/^[0-9]+/, human($1)); print}')";
|
||||
}
|
||||
|
||||
# METHOD: get_dump_file_name
|
||||
@@ -424,22 +450,20 @@ function convert_bytes
|
||||
function get_dump_file_name
|
||||
{
|
||||
# set base search for the files
|
||||
sequence=*;
|
||||
if [ ${db} ]; then
|
||||
db_name=${db}"."${owner}"."${encoding}".";
|
||||
if [ "${db}" ]; then
|
||||
db_name="${db}.${owner}.${encoding}.";
|
||||
else
|
||||
db_name="pg_globals."${DB_USER}".NONE.";
|
||||
db_name="pg_globals.${DB_USER}.NONE.";
|
||||
fi;
|
||||
file=${BACKUPDIR}"/"${db_name}${DB_TYPE}"-"${DUMP_DB_VERSION}"_"${DB_HOST}"_"${DB_PORT}"_"$(date +%Y%m%d)"_"$(date +%H%M)"_"${sequence}".c.sql";
|
||||
seq='';
|
||||
# we need to find the next sequence number
|
||||
for i in $(ls -1 ${file} 2>/dev/null); do
|
||||
for i in "${BACKUPDIR}/${db_name}${DB_TYPE}-${DUMP_DB_VERSION}_${DB_HOST}_${DB_PORT}_$(date +%Y%m%d)_$(date +%H%M)"*.c.sql; do
|
||||
# get the last sequence and cut any leading 0 so we can run +1 on it
|
||||
seq=$(echo $i | cut -d "." -f 3 | cut -d "_" -f 4 | sed -e "s/^0//g");
|
||||
seq=$(echo "$i" | cut -d "." -f 4 | cut -d "_" -f 6 | sed -e "s/^0//g");
|
||||
done;
|
||||
if [ ! -z ${seq} ]; then
|
||||
if [ -n "${seq}" ]; then
|
||||
# add +1 and if < 10 prefix with 0
|
||||
let seq=${seq}+1;
|
||||
seq=$((seq+1));
|
||||
if [ ${seq} -lt 10 ]; then
|
||||
sequence="0"${seq};
|
||||
else
|
||||
@@ -449,7 +473,7 @@ function get_dump_file_name
|
||||
sequence="01";
|
||||
fi;
|
||||
# now build correct file name
|
||||
filename=${BACKUPDIR}"/"${db_name}${DB_TYPE}"-"${DUMP_DB_VERSION}"_"${DB_HOST}"_"${DB_PORT}"_"$(date +%Y%m%d)"_"$(date +%H%M)"_"${sequence}".c.sql";
|
||||
filename="${BACKUPDIR}/${db_name}${DB_TYPE}-${DUMP_DB_VERSION}_${DB_HOST}_${DB_PORT}_$(date +%Y%m%d)_$(date +%H%M)_${sequence}.c.sql";
|
||||
echo "${filename}";
|
||||
}
|
||||
|
||||
@@ -466,8 +490,10 @@ function get_dump_databases
|
||||
if [ ${GLOBALS} -eq 1 ]; then
|
||||
search_names+=("pg_globals.*");
|
||||
fi;
|
||||
for owner_db in $(${PG_PSQL} -U ${DB_USER} ${CONN_DB_HOST} -p ${DB_PORT} -d template1 -t -A -F "," -X -q -c "SELECT pg_catalog.pg_get_userbyid(datdba) AS owner, datname, pg_catalog.pg_encoding_to_char(encoding) FROM pg_catalog.pg_database WHERE datname "\!"~ 'template(0|1)';"); do
|
||||
db=$(echo ${owner_db} | cut -d "," -f 2);
|
||||
_PG_PARAMS_SELECT=("${PG_PARAMS_SELECT[@]}");
|
||||
_PG_PARAMS_SELECT+=("SELECT pg_catalog.pg_get_userbyid(datdba) AS owner, datname, pg_catalog.pg_encoding_to_char(encoding) FROM pg_catalog.pg_database WHERE datname !~ 'template(0|1)';");
|
||||
for owner_db in $(${PG_PSQL} "${_PG_PARAMS_SELECT[@]}"); do
|
||||
db=$(echo "${owner_db}" | cut -d "," -f 2);
|
||||
# check if we exclude this db
|
||||
exclude=0;
|
||||
include=0;
|
||||
@@ -477,7 +503,7 @@ function get_dump_databases
|
||||
break;
|
||||
fi;
|
||||
done;
|
||||
if [ ! -z "${INCLUDE}" ]; then
|
||||
if [ -n "${INCLUDE}" ]; then
|
||||
for incl_db in ${INCLUDE}; do
|
||||
if [ "${db}" = "${incl_db}" ]; then
|
||||
include=1;
|
||||
@@ -500,7 +526,7 @@ function get_dump_databases
|
||||
# DESC : checks for older files than given keep time/amount and removes them
|
||||
function clean_up
|
||||
{
|
||||
if [ -d ${BACKUPDIR} ]; then
|
||||
if [ -d "${BACKUPDIR}" ]; then
|
||||
if [ ${CLEAN_NUMBER} -eq 0 ]; then
|
||||
echo "Cleanup older than ${KEEP} days backup in ${BACKUPDIR}";
|
||||
else
|
||||
@@ -508,41 +534,43 @@ function clean_up
|
||||
# if we run clean before backup, we need to clean up +1
|
||||
# so if we keep one, we must remove all data before running new
|
||||
if [ ${PRE_RUN_CLEAN_UP} -eq 1 ]; then
|
||||
let KEEP=${KEEP}-1;
|
||||
# let KEEP=${KEEP}-1;
|
||||
KEEP=$((KEEP-1));
|
||||
fi;
|
||||
fi;
|
||||
# build the find string based on the search names patter
|
||||
find_string='';
|
||||
find_params=("${BACKUPDIR}");
|
||||
for name in "${search_names[@]}"; do
|
||||
# for not number based, we build the find string here
|
||||
# else we do the delete here already
|
||||
if [ ${CLEAN_NUMBER} -eq 0 ]; then
|
||||
if [ ! -z "${find_string}" ]; then
|
||||
find_string=${find_string}' -o ';
|
||||
if [ ${#find_params[@]} -gt 1 ]; then
|
||||
find_params+=("-o");
|
||||
fi;
|
||||
find_string=${find_string}"-mtime +${KEEP} -name "${name}${DB_TYPE}*.sql" -type f -delete -print";
|
||||
find_params+=("-mtime" "+${KEEP}" "-name" "${name}${DB_TYPE}*.sql" "-type" "f" "-delete" "-print");
|
||||
echo "- Remove old backups for '${name}'";
|
||||
else
|
||||
# if we do number based delete of old data, but only if the number of
|
||||
# files is bigger than the keep number or equal if we do PRE_RUN_CLEAN_UP
|
||||
# this can be error, but we allow it -> script should not abort here
|
||||
# note we have a wildcard in the name, so we can't put that into ""
|
||||
count=$(ls "${BACKUPDIR}/"${name}${DB_TYPE}*.sql 2>/dev/null | wc -l) || true;
|
||||
count=$(find "${BACKUPDIR}" -name "${name}${DB_TYPE}*.sql" -type f -print | wc -l);
|
||||
if [ ${PRE_RUN_CLEAN_UP} -eq 1 ]; then
|
||||
let count=${count}+1;
|
||||
# let count=${count}+1;
|
||||
count=$((count+1));
|
||||
fi;
|
||||
if [ ${count} -gt ${KEEP} ]; then
|
||||
if [ "${count}" -gt "${KEEP}" ]; then
|
||||
# calculate the amount to delete
|
||||
# eg if we want to keep 1, and we have 3 files then we need to delete 2
|
||||
# keep is always +1 (include the to backup count).
|
||||
# count is +1 if we do a pre-run cleanup
|
||||
# grouped by db name, db type
|
||||
let TO_DELETE=${count}-${KEEP};
|
||||
TO_DELETE=$((count-KEEP));
|
||||
echo "- Remove old backups for '${name}', found ${count}, will delete ${TO_DELETE}";
|
||||
if [ ${TEST} -eq 0 ]; then
|
||||
ls -tr "${BACKUPDIR}/"${name}${DB_TYPE}*.sql 2>/dev/null | head -n ${TO_DELETE} | xargs rm;
|
||||
find "${BACKUPDIR}" -name "${name}${DB_TYPE}*.sql" -type f -printf "%Ts\t%p\n" | sort -nr | head -n ${TO_DELETE} | xargs rm;
|
||||
else
|
||||
echo "ls -tr ${BACKUPDIR}/${name}${DB_TYPE}*.sql 2>/dev/null | head -n ${TO_DELETE} | xargs rm";
|
||||
print "find \"${BACKUPDIR}\" -name \"${name}${DB_TYPE}*.sql\" -type f -printf \"%Ts\\t%p\\n\" | sort -nr | head -n ${TO_DELETE} | xargs rm";
|
||||
fi;
|
||||
fi;
|
||||
fi;
|
||||
@@ -550,15 +578,15 @@ function clean_up
|
||||
# if we do find (day based) delete of old data
|
||||
if [ ${CLEAN_NUMBER} -eq 0 ]; then
|
||||
if [ ${TEST} -eq 0 ]; then
|
||||
find ${BACKUPDIR} ${find_string};
|
||||
find "${find_params[@]}";
|
||||
else
|
||||
echo "find ${BACKUPDIR} ${find_string}";
|
||||
echo "find ${find_params[*]}";
|
||||
fi;
|
||||
fi;
|
||||
fi
|
||||
}
|
||||
|
||||
if [ ! -z "${DB_PASSWD}" ]; then
|
||||
if [ -n "${DB_PASSWD}" ]; then
|
||||
export PGPASSWORD=${DB_PASSWD};
|
||||
fi;
|
||||
START=$(date "+%s");
|
||||
@@ -580,11 +608,15 @@ if [ ${GLOBALS} -eq 1 ]; then
|
||||
# reset any previous set db name from deletes so the correct global file name is set
|
||||
db='';
|
||||
filename=$(get_dump_file_name);
|
||||
search_names+=("pg_globals.*"); # this is used for the find/delete part
|
||||
# this is used for the find/delete part
|
||||
search_names+=("pg_globals.*");
|
||||
# build dump parms
|
||||
_PG_PARAMS_DUMP=("${PG_PARAMS[@]}");
|
||||
_PG_PARAMS_DUMP+=("--globals-only")
|
||||
if [ ${TEST} -eq 0 ]; then
|
||||
${PG_DUMPALL} -U ${DB_USER} ${CONN_DB_HOST} -p ${DB_PORT} --globals-only > "${filename}";
|
||||
${PG_DUMPALL} "${_PG_PARAMS_DUMP[@]}" > "${filename}";
|
||||
else
|
||||
echo "${PG_DUMPALL} -U ${DB_USER} ${CONN_DB_HOST} -p ${DB_PORT} --globals-only > ${filename}";
|
||||
echo "${PG_DUMPALL} ${_PG_PARAMS_DUMP[*]} > ${filename}";
|
||||
fi;
|
||||
echo "done";
|
||||
else
|
||||
@@ -595,21 +627,23 @@ echo -n "(+) Dump databases: ";
|
||||
if [ -z "${INCLUDE}" ]; then
|
||||
echo "All";
|
||||
else
|
||||
echo ${INCLUDE};
|
||||
echo "${INCLUDE}";
|
||||
fi;
|
||||
echo -n "(-) Exclude databases: ";
|
||||
if [ -z "${EXCLUDE}" ]; then
|
||||
echo "None";
|
||||
else
|
||||
echo ${EXCLUDE};
|
||||
echo "${EXCLUDE}";
|
||||
fi;
|
||||
|
||||
filesize_sum=0;
|
||||
for owner_db in $(${PG_PSQL} -U ${DB_USER} ${CONN_DB_HOST} -p ${DB_PORT} -d template1 -t -A -F "," -X -q -c "SELECT pg_catalog.pg_get_userbyid(datdba) AS owner, datname, pg_catalog.pg_encoding_to_char(encoding) AS encoding FROM pg_catalog.pg_database WHERE datname "\!"~ 'template(0|1)' ORDER BY datname;"); do
|
||||
_PG_PARAMS_SELECT=("${PG_PARAMS_SELECT[@]}");
|
||||
_PG_PARAMS_SELECT+=("SELECT pg_catalog.pg_get_userbyid(datdba) AS owner, datname, pg_catalog.pg_encoding_to_char(encoding) AS encoding FROM pg_catalog.pg_database WHERE datname !~ 'template(0|1)' ORDER BY datname;");
|
||||
for owner_db in $(${PG_PSQL} "${_PG_PARAMS_SELECT[@]}"); do
|
||||
# get the user who owns the DB too
|
||||
owner=$(echo ${owner_db} | cut -d "," -f 1);
|
||||
db=$(echo ${owner_db} | cut -d "," -f 2);
|
||||
encoding=$(echo ${owner_db} | cut -d "," -f 3);
|
||||
owner=$(echo "${owner_db}" | cut -d "," -f 1);
|
||||
db=$(echo "${owner_db}" | cut -d "," -f 2);
|
||||
encoding=$(echo "${owner_db}" | cut -d "," -f 3);
|
||||
# check if we exclude this db
|
||||
exclude=0;
|
||||
include=0;
|
||||
@@ -619,7 +653,7 @@ for owner_db in $(${PG_PSQL} -U ${DB_USER} ${CONN_DB_HOST} -p ${DB_PORT} -d temp
|
||||
break;
|
||||
fi;
|
||||
done;
|
||||
if [ ! -z "${INCLUDE}" ]; then
|
||||
if [ -n "${INCLUDE}" ]; then
|
||||
for incl_db in ${INCLUDE}; do
|
||||
if [ "${db}" = "${incl_db}" ]; then
|
||||
include=1;
|
||||
@@ -634,25 +668,28 @@ for owner_db in $(${PG_PSQL} -U ${DB_USER} ${CONN_DB_HOST} -p ${DB_PORT} -d temp
|
||||
filename=$(get_dump_file_name);
|
||||
search_names+=("${db}.*");
|
||||
SUBSTART=$(date "+%s");
|
||||
# build dump parms
|
||||
_PG_PARAMS_DUMP=("${PG_PARAMS[@]}");
|
||||
_PG_PARAMS_DUMP+=("-c" "--format=c" "${db}")
|
||||
if [ ${TEST} -eq 0 ]; then
|
||||
${PG_DUMP} -U ${DB_USER} ${CONN_DB_HOST} -p ${DB_PORT} -c --format=c ${db} > "${filename}";
|
||||
${PG_DUMP} "${_PG_PARAMS_DUMP[@]}" > "${filename}";
|
||||
else
|
||||
echo "${PG_DUMP} -U ${DB_USER} ${CONN_DB_HOST} -p ${DB_PORT} -c --format=c ${db} > ${filename}";
|
||||
echo "${PG_DUMP} ${_PG_PARAMS_DUMP[*]} > ${filename}";
|
||||
fi;
|
||||
# get the file size for the dumped file and convert it to a human readable format
|
||||
filesize=0;
|
||||
if [ -f "${filename}" ]; then
|
||||
filesize=$(wc -c "${filename}" | cut -f 1 -d ' ');
|
||||
filesize_sum=$[$filesize+$filesize_sum];
|
||||
filesize_sum=$((filesize+filesize_sum));
|
||||
fi;
|
||||
DURATION=$[$(date "+%s")-${SUBSTART}];
|
||||
printf "done (%s and %s)\n" "$(convert_time ${DURATION})" "$(convert_bytes ${filesize})";
|
||||
DURATION=$(($(date "+%s")-SUBSTART));
|
||||
printf "done (%s and %s)\n" "$(convert_time ${DURATION})" "$(convert_bytes "${filesize}")";
|
||||
else
|
||||
printf -- "- Exclude database: %35s\n" "${db}";
|
||||
fi;
|
||||
done
|
||||
printf "Backup ended at %s\n" "$(date '+%Y-%m-%d %H:%M:%S')";
|
||||
if [ ! -z "${DB_PASSWD}" ]; then
|
||||
if [ -n "${DB_PASSWD}" ]; then
|
||||
unset DB_PASSWD;
|
||||
fi;
|
||||
|
||||
@@ -660,7 +697,7 @@ if [ ${PRE_RUN_CLEAN_UP} -eq 0 ]; then
|
||||
clean_up;
|
||||
fi;
|
||||
|
||||
DURATION=$[$(date "+%s")-${START}];
|
||||
DURATION=$(($(date "+%s")-START));
|
||||
printf "Cleanup ended at %s\n" "$(date '+%Y-%m-%d %H:%M:%S')";
|
||||
printf "Finished backup in %s with %s\n" "$(convert_time ${DURATION})" "$(convert_bytes ${filesize_sum})";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user