From bb7349a0adb4120d40b664cc7497d1bcdf28fc91 Mon Sep 17 00:00:00 2001 From: Jorge Holgado Date: Fri, 25 Feb 2022 10:26:58 +0100 Subject: [PATCH] changes --- get_all_pgmetadata.config | 48 ++++++++ get_all_pgmetadata.sh | 244 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 292 insertions(+) create mode 100644 get_all_pgmetadata.config create mode 100755 get_all_pgmetadata.sh diff --git a/get_all_pgmetadata.config b/get_all_pgmetadata.config new file mode 100644 index 0000000..335f085 --- /dev/null +++ b/get_all_pgmetadata.config @@ -0,0 +1,48 @@ +#!/bin/bash + +#################################### +# cron vars +#################################### +PGDATA=/var/lib/pgsql/12/data +PGDUMP="$(which pg_dump)" +PGDUMP="${PGDUMP} -h 127.0.0.1" + +TODAY="$(date +%Y%m%d)" +NOW="$(date +%Y%m%d%H%M%S)" + +# For debugging +WAITSEC=10 + +# multi-db support, this is auto +#DBNAME="epayments" + +BACKUPDIR="/home/backup/postgre/metadata/${HOSTNAME,,}" +LOGDIR="${BACKUPDIR}/logs/" + +# multi-db support, this is auto +#FULLMETADATA="${BACKUPDIR}/FULL_${DBNAME}_METADATA.sql" +# multi-db support, this is auto +#DIFFILE="${BACKUPDIR}/FULL_${DBNAME}_METADATA.sql" + +PSQL="$(which psql) -h 127.0.0.1" + +CHANGEFILE=${BACKUPDIR}/$(basename $0 .sh)_changefile_${NOW}.log +DIFFILE=${BACKUPDIR}/$(basename $0 .sh)_changefile_${NOW}.log + +LOGFILE=${LOGDIR}/$(basename $0 .sh)_${NOW}.log +SCRIPTLOG="${LOGFILE}" +ERRFILE=${LOGDIR}/$(basename $0 .sh)_${NOW}.err +SCRIPTLOGERR="${ERRFILE}" + +#################################### +# git usage +#################################### +# for git usage you'll have to create a git repo on the git server +# then import it into the ${BACKUPDIR} so authentication & config is done +# then this script will make `git push` automatically if ISGITREPO=0 + +# set to 0 if git is in place +ISGITREPO=0 +ISGITREPO=1 + + diff --git a/get_all_pgmetadata.sh b/get_all_pgmetadata.sh new file mode 100755 index 0000000..9094ca6 --- /dev/null +++ b/get_all_pgmetadata.sh @@ -0,0 +1,244 @@ +#!/bin/bash + +# Exit codes: +# 1 : +# 2 : +# 3 : +# 4 : + + + +######################################################################## +# INIT +######################################################################## +DEBUGME="${1:2}" +if [[ "${DEBUGME,,}" = "debug" ]] ; then + DEBUG=0 +else + DEBUG=1 +fi + +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 +# +######################################################################## + + + +debug_me() +{ + if [[ ${DEBUG} -eq 0 ]] ; then + [[ "$*" ]] && echo -e "$*" + echo -e "${BLUE}DEBUGMODE${RESET} Press enter to continue" + read -t ${WAITSEC} + fi +} + + +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 +} + +get_dbname() +{ + DATABASES="$(echo "select datname from pg_database where datname not in ('postgres', 'template0', 'template1');" | ${PSQL} -q | egrep -v "^($|\(.*\)$|\-{1,}$)|datname"| awk '{print $1}')" +} + + +generate_full_metadata() +{ + ${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// /_}" + OUTPUTFILE="${OBJECT}.sql" + mkdir -p "${OUTPUTDIR}" + echo -e "${OUTPUTDIR}/${OUTPUTFILE}" + 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} +} + +init_staff() +{ + [[ ! -d ${BACKUPDIR} ]] && mkdir -p ${BACKUPDIR} + cd ${BACKUPDIR} +} + +upload_to_git() +{ + [[ ${DEBUG} -eq 0 ]] && set -x + + local let RES=0 + + debug_me + + cd ${BACKUPDIR} + + GITVERSION=$(git --version | awk '{print $3}') + if [[ ${GITVERSION} =~ ^1.*$ ]] ; then + NOEDIT="" + else + NOEDIT="--no-edit" + fi + + + # 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=$? + + debug_me + + return ${RES} +} + + + +######################################################################## +# +# / FUNCTIONS +# +######################################################################## + +######################################################################## +# +# MAIN +# +######################################################################## + +[[ ! -f ${CONFIGFILE} ]] && echo -e "${LIGHTRED} CONFIGFILE ${CONFIGFILE} NOT FOUND${RESET}" && exit 1 +. ${CONFIGFILE} + +# 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 + +[[ ${DEBUG} -eq 0 ]] && DEBUGME="bash -x" + +init_staff +debug_me +get_dbname +debug_me +# multi-database support (auto mode) +for DBNAME in ${DATABASES} ; do + FULLMETADATA="${BACKUPDIR}/FULL_${DBNAME}_METADATA.sql" + + generate_full_metadata + debug_me + generate_split_metadata + debug_me +done + +if [[ ${ISGITREPO} -eq 0 ]] ; then + upload_to_git +fi + +exit ${EXITCODE} + +######################################################################## +# +# / MAIN +# +######################################################################## +