get_all_pgmetadata/get_all_pgmetadata.sh

313 lines
7.4 KiB
Bash
Raw Permalink Normal View History

2022-02-25 09:26:58 +00:00
#!/bin/bash
# Exit codes:
# 1 :
# 2 :
# 3 :
# 4 :
########################################################################
# INIT
########################################################################
2022-07-26 14:59:28 +00:00
# moved to config
#DEBUGME="${1:2}"
#if [[ "${DEBUGME,,}" = "debug" ]] ; then
# DEBUG=0
#else
# DEBUG=1
#fi
2022-02-25 09:26:58 +00:00
CONFIGFILE="$(dirname $(readlink -f $0))/$(basename $(readlink -f $0) .sh).config"
########################################################################
#/INIT
########################################################################
########################################################################
#
# CONSTANTS
#
########################################################################
# colors
BOLD="\e[1m"
GREEN="\e[32m"
LIGHTGREEN="${BOLD}${GREEN}"
RED="\033[1;31m"
LIGHTRED="\033[1;31m"
BLUE="\e[34m"
LIGHTBLUE="${BOLD}${BLUE}"
YELLOW="\e[33m"
LIGHTYELLOW="${BOLD}${YELLOW}"
WHITE="\033[0;37m"
RESET="\033[0;00m"
########################################################################
#
# / CONSTANTS
#
########################################################################
########################################################################
#
# VARIABLES
#
########################################################################
########################################################################
#
# / VARIABLES
#
########################################################################
########################################################################
#
# FUNCTIONS
#
########################################################################
2024-08-16 13:54:53 +00:00
printmsg()
{
echo -e "$*"
}
2022-02-25 09:26:58 +00:00
2024-08-16 13:54:53 +00:00
abort_message()
{
local ABORTCODE=1
[[ ${EXITCODE} -ne 0 ]] && ABORTCODE=${EXITCODE}
printmsg "${LIGHTRED}ERROR${RESET}: $*"
exit ${ABORTCODE}
}
2022-02-25 09:26:58 +00:00
2024-08-16 13:54:53 +00:00
# debug_me uses variable ${DEBUG}
2022-02-25 09:26:58 +00:00
debug_me()
{
2024-08-16 13:54:53 +00:00
if [[ "${DEBUG}" && ${DEBUG} -eq 0 ]] ; then
printmsg "${LIGHTBLUE}[DEBUG]${RESET}: $*"
2022-02-25 09:26:58 +00:00
fi
}
2024-08-16 13:54:53 +00:00
warning_message()
{
printmsg "${LIGHTYELLOW}[WARNING]${RESET}: $*"
}
ok_message()
{
printmsg "${LIGHTGREEN}[OK]${RESET}: $*"
}
2022-02-25 09:26:58 +00:00
usage()
{
printf "%s${LIGHTRED}USAGE:${RESET}
$0 VAR1 VAR2
Where VAR1 allowed: ${LIGHTGREEN}BLA${RESET}
And VAR2 allowed: ${LIGHTGREEN}$BLA${RESET}\n"
# VERY INITIAL CHECKS
}
2024-08-16 13:54:53 +00:00
nice_countdown()
{
local WAIT_TIME=$1
local DEFAULT_WAIT_TIME=10
if [[ ! ${WAIT_TIME} ]] ; then
WAIT_TIME=${DEFAULT_WAIT_TIME}
fi
local WARNING=$((WAIT_TIME*2/3))
local CRITICAL=$((WAIT_TIME/3))
for ((x=WAIT_TIME; x>0; x--)) ; do
if [[ ${x} -gt ${WARNING} ]] ; then
MSG_COLOR=${LIGHTGREEN}
elif [[ ${x} -gt ${CRITICAL} ]] ; then
MSG_COLOR=${LIGHTYELLOW}
else
MSG_COLOR=${LIGHTRED}
fi
printf "\r${MSG_COLOR}%s${RESET} .." "${x}"
#sleep 1
read -r -t1
done
echo
}
2022-02-25 09:26:58 +00:00
get_dbname()
{
2024-08-16 13:54:53 +00:00
DATABASES="$(echo "select datname from pg_database where datname not in ('postgres', 'template0', 'template1', 'rdsadmin');" | ${PSQL} -q | grep -Ev "^($|\(.*\)$|-{1,}$)|datname"| awk '{print $1}')"
#echo "select datname from pg_database where datname not in ('postgres', 'template0', 'template1');" | ${PSQL} -q | grep -Ev "^($|\(.*\)$|-{1,}$)|datname"| awk '{print $1}'
debug_me "DATABASES=${DATABASES}"
2022-02-25 09:26:58 +00:00
}
generate_full_metadata()
{
2024-08-16 13:54:53 +00:00
#debug_me "${PGDUMP} -s ${DBNAME} > ${FULLMETADATA}"
2022-02-25 09:26:58 +00:00
${PGDUMP} -s ${DBNAME} > ${FULLMETADATA}
}
generate_split_metadata()
{
local OBJECT=""
local SCHEMA=""
local TYPE=""
local OUTPUTDIR=""
local OUTPUTFILE=""
# reading from &3 to allow debug mode to stop the loop
while IFS='' read LINE <&3 ; do
if [[ "${LINE}" =~ ^--\ Name:\ (.*)\;\ Type:\ (.*)\;\ Schema:\ (.*)\;\ Owner:\ (.*) ]] ; then
OBJECT="${BASH_REMATCH[1]}"
SCHEMA="${BASH_REMATCH[3]}"
TYPE="${BASH_REMATCH[2]}"
#debug_me "${LINE}"
if [[ "${OBJECT}" && "${SCHEMA}" ]] ; then
if [[ "${SCHEMA}" = "-" ]] ; then
SCHEMA="postgres"
fi
OUTPUTDIR="${BACKUPDIR}/${DBNAME}/${SCHEMA// /_}/${TYPE// /_}"
2024-08-16 13:54:53 +00:00
#debug_me "OUTPUTDIR=${OUTPUTDIR}"
2022-07-22 16:56:02 +00:00
# PATCH for very long file names
OUTPUTFILE="${OBJECT::240}"
OUTPUTFILE="${OUTPUTFILE// /_}"
if [[ -f ${OUTPUTDIR}/${OUTPUTFILE}.sql ]] ; then
2024-08-16 13:54:53 +00:00
for ((x=1; x<20 ;x++)) ; do
2022-07-22 16:56:02 +00:00
if [[ ! -f ${OUTPUTDIR}/${OUTPUTFILE}_${x}.sql ]] ; then
OUTPUTFILE="${OUTPUTFILE}_${x}"
fi
done
fi
OUTPUTFILE="${OUTPUTFILE}.sql"
2024-08-16 13:54:53 +00:00
#debug_me "OUTPUTFILE=${OUTPUTFILE}"
2022-07-22 16:56:02 +00:00
2022-02-25 09:26:58 +00:00
mkdir -p "${OUTPUTDIR}"
2024-08-16 13:54:53 +00:00
#echo -e "${OUTPUTDIR}/${OUTPUTFILE}"
2022-02-25 09:26:58 +00:00
echo -e "${LINE}" > "${OUTPUTDIR}/${OUTPUTFILE}"
fi
fi
if [[ -f "${OUTPUTDIR}/${OUTPUTFILE}" ]] ; then
if [[ ! "${LINE}" =~ ^--.*$ ]] ; then
# echo "Skipping line comment"
# else
echo -e "${LINE}" >> "${OUTPUTDIR}/${OUTPUTFILE}"
fi
fi
done 3< ${FULLMETADATA}
}
2022-07-26 14:59:28 +00:00
dump_all_roles()
{
if [[ ${DUMP_ROLE_PASSWORDS} -eq 0 ]] ; then
2024-08-16 13:54:53 +00:00
${PGDUMPALL} --roles-only > ${BACKUPDIR}/dump_roles.sql
2022-07-26 14:59:28 +00:00
else
2024-08-16 13:54:53 +00:00
${PGDUMPALL} --roles-only --no-role-passwords > ${BACKUPDIR}/dump_roles.sql
2022-07-26 14:59:28 +00:00
fi
}
2022-02-25 09:26:58 +00:00
init_staff()
{
[[ ! -d ${BACKUPDIR} ]] && mkdir -p ${BACKUPDIR}
cd ${BACKUPDIR}
}
upload_to_git()
{
2024-08-16 13:54:53 +00:00
#[[ ${DEBUG} -eq 0 ]] && set -x
2022-02-25 09:26:58 +00:00
2024-08-16 13:54:53 +00:00
local RES
RES=0
2022-02-25 09:26:58 +00:00
2024-08-16 13:54:53 +00:00
debug_me "upload_to_git()"
2022-02-25 09:26:58 +00:00
cd ${BACKUPDIR}
# git version patch
git pull ${NOEDIT} -q -f
git add *
git commit -a -m "$(date) release"
RES=$?
# Updating repo
[[ ${RES} -eq 0 ]] && git push && RES=$?
return ${RES}
}
########################################################################
#
# / FUNCTIONS
#
########################################################################
########################################################################
#
# MAIN
#
########################################################################
[[ ! -f ${CONFIGFILE} ]] && echo -e "${LIGHTRED} CONFIGFILE ${CONFIGFILE} NOT FOUND${RESET}" && exit 1
. ${CONFIGFILE}
2024-08-16 13:54:53 +00:00
## DETECTING if the script is run by cron
#if [[ "$(tty)" = "not a tty" ]] ; then
# [[ ${DEBUG} -eq 0 ]] && set -x
# exec > ${SCRIPTLOG}
# exec 2> ${SCRIPTLOGERR}
#elif [[ ${DEBUG} -eq 0 ]] ; then
# echo -e "${BLUE}DEBUGMODE${RESET} is on"
# #echo -e "\t SCRIPTLOG will be ${SCRIPTLOG}"
# #echo -e "\t SCRIPTLOGERR will be ${SCRIPTLOGERR}"
# #set -x
# #exec > ${SCRIPTLOG}
# #exec 2> ${SCRIPTLOGERR}
#fi
2022-02-25 09:26:58 +00:00
[[ ${DEBUG} -eq 0 ]] && DEBUGME="bash -x"
2024-08-16 13:54:53 +00:00
debug_me "init_staff"
2022-02-25 09:26:58 +00:00
init_staff
2024-08-16 13:54:53 +00:00
debug_me "get_dbname"
2022-02-25 09:26:58 +00:00
get_dbname
2024-08-16 13:54:53 +00:00
debug_me "main loop over dbs"
2022-02-25 09:26:58 +00:00
# multi-database support (auto mode)
for DBNAME in ${DATABASES} ; do
FULLMETADATA="${BACKUPDIR}/FULL_${DBNAME}_METADATA.sql"
2024-08-16 13:54:53 +00:00
debug_me "Database=${YELLOW}${DBNAME}${RESET}"
2022-02-25 09:26:58 +00:00
generate_full_metadata
2022-07-26 14:59:28 +00:00
if [[ ${SPLIPT_METADATA} -eq 0 ]] ; then
generate_split_metadata
fi
2024-08-16 13:54:53 +00:00
#nice_countdown 6
2022-02-25 09:26:58 +00:00
done
2024-08-16 13:54:53 +00:00
#debug_me "dump_all_roles"
#dump_all_roles
2022-02-25 09:26:58 +00:00
if [[ ${ISGITREPO} -eq 0 ]] ; then
2022-07-26 14:59:28 +00:00
debug_me "upload_to_git"
2022-02-25 09:26:58 +00:00
upload_to_git
fi
exit ${EXITCODE}
########################################################################
#
# / MAIN
#
########################################################################