Some more clean up for code

Fix ident get from file, they where subtly not correct
- wrong lookup for <10 postgresql version
- if version has a . inside the host/port lookups have to shift
- dry run notice for restore runs
This commit is contained in:
2025-04-15 09:40:06 +09:00
parent 01b434fca0
commit bb5c65818b
2 changed files with 28 additions and 28 deletions

View File

@@ -28,6 +28,7 @@ _host='local';
HOST='';
_encoding='UTF8';
set_encoding='';
ERROR=0;
REDHAT=0;
IMPORT_GLOBALS=1;
TEMPLATEDB='template0'; # truly empty for restore
@@ -282,6 +283,9 @@ if [ -z "${PORT}" ]; then
else
_PORT=${PORT};
fi;
if [ ${DRY_RUN} ]; then
echo "**** [DRY RUN] ****";
fi;
echo "= Will import $db_count databases from $_DUMP_FOLDER" | $LOGFILE;
echo "= into the DB server $_HOST:$_PORT" | $LOGFILE;
echo "= running $MAX_JOBS jobs" | $LOGFILE;
@@ -298,21 +302,22 @@ if [ "$IMPORT_GLOBALS" -eq 1 ]; then
# get the pg_globals file
echo "=[Globals Restore]=START=[$start_time]==================================================>" | $LOGFILE;
# get newest and only the first one
# file=$(ls -1t "$DUMP_FOLDER/pg_global"* | head -1);
file=$(find "$DUMP_FOLDER" -name "pg_global*" -type f -printf "%Ts\t%p\n" | sort -nr | head -1);
filename=$(basename "$file");
# the last _ is for version 10 or higher
# db version, without prefix of DB type
version=$(echo "$filename" | cut -d "." -f 4 | cut -d "-" -f 2 | cut -d "_" -f 1);
cut_pos=4;
# if this is < 10 then we need the second part too
if [ "${version}" -lt 10 ]; then
# db version, second part (after .)
version=$version'.'$(echo "$filename" | cut -d "." -f 5 | cut -d "_" -f 1);
cut_pos=5;
fi;
# hostname of original DB, can be used as target host too
__host=$(echo "$filename" | cut -d "." -f 5 | cut -d "_" -f 2);
__host=$(echo "$filename" | cut -d "." -f ${cut_pos} | cut -d "_" -f 2);
# port of original DB, can be used as target port too
__port=$(echo "$filename" | cut -d "." -f 5 | cut -d "_" -f 3);
__port=$(echo "$filename" | cut -d "." -f ${cut_pos} | cut -d "_" -f 3);
# override file port over given port if it differs and is valid
if [ -z "$_port" ] && [ "$__port" != "$_port" ] && [[ "$__port" =~ $PORT_REGEX ]] ; then
_port=$__port;
@@ -415,14 +420,12 @@ for file in "$DUMP_FOLDER/"*.sql; do
# for the call
_PG_PARAMS=("${_PG_PARAMS_ALL[@]}");
_PG_PARAMS+=("-A" "-F" "," "-t" "-q" "-X" "-c" "SELECT oid FROM pg_roles WHERE rolname = '$owner';" "template1");
# user_oid=$(echo "SELECT oid FROM pg_roles WHERE rolname = '$owner';" | $PSQL -U postgres $host $port -A -F "," -t -q -X template1);
user_oid=$("$PG_PSQL" "${_PG_PARAMS[@]}");
if [ -z "$user_oid" ]; then
echo "+ Create USER '$owner' for DB '$database' [$_host:$_port] @ $(date +"%F %T")" | $LOGFILE;
_PG_PARAMS=("${_PG_PARAMS_ALL[@]}");
_PG_PARAMS+=("-D" "-R" "-S" "$owner");
if [ ${DRY_RUN} -eq 0 ]; then
# "${PG_PATH}${PG_CREATEUSER}" -U postgres -D -R -S $host $port $owner;
"${PG_PATH}${PG_CREATEUSER}" "${_PG_PARAMS[@]}";
else
echo "${PG_PATH}${PG_CREATEUSER} ${_PG_PARAMS[*]}";
@@ -441,7 +444,6 @@ for file in "$DUMP_FOLDER/"*.sql; do
_PG_PARAMS=("${_PG_PARAMS_ALL[@]}");
_PG_PARAMS+=("-O" "$owner" "-E" "$set_encoding" "-T" "$TEMPLATEDB" "$database");
if [ ${DRY_RUN} -eq 0 ]; then
# "${PG_PATH}${PG_CREATEDB}" -U postgres -O $owner -E $set_encoding -T $TEMPLATEDB $host $port $database;
"${PG_PATH}${PG_CREATEDB}" "${_PG_PARAMS[@]}";
else
echo "${PG_PATH}${PG_CREATEDB} ${_PG_PARAMS[*]}";
@@ -451,7 +453,6 @@ for file in "$DUMP_FOLDER/"*.sql; do
_PG_PARAMS=("${_PG_PARAMS_ALL[@]}");
_PG_PARAMS+=("plpgsql" "$database");
if [ ${DRY_RUN} -eq 0 ]; then
# "${PG_PATH}${PG_CREATELANG}" -U postgres plpgsql $host $port $database;
"${PG_PATH}${PG_CREATELANG}" "${_PG_PARAMS[@]}";
else
echo "${PG_PATH}${PG_CREATELANG} ${_PG_PARAMS[*]}";
@@ -461,7 +462,6 @@ for file in "$DUMP_FOLDER/"*.sql; do
_PG_PARAMS=("${_PG_PARAMS_ALL[@]}");
_PG_PARAMS+=("-d" "$database" "-F" "c" "-v" "-c" "-j" "$MAX_JOBS" "$file");
if [ ${DRY_RUN} -eq 0 ]; then
# "${PG_PATH}${PG_RESTORE}" -U postgres -d $database -F c -v -c -j $MAX_JOBS $host $port $file 2>"$LOGS/errors.${database}.$(date +"%Y%m%d_%H%M%S").log";
"${PG_PATH}${PG_RESTORE}" "${_PG_PARAMS[@]}" 2>"$LOGS/errors.${database}.$(date +"%Y%m%d_%H%M%S").log";
else
echo "${PG_PATH}${PG_RESTORE} ${_PG_PARAMS[*]} 2>${LOGS}/errors.${database}.$(date +"%Y%m%d_%H%M%S").log";
@@ -469,10 +469,8 @@ for file in "$DUMP_FOLDER/"*.sql; do
# BUG FIX FOR POSTGRESQL 9.6.2 db_dump
# it does not dump the default public ACL so the owner of the DB cannot access the data,
# check if the ACL dump is missing and do a basic restore
if ! "$("${PG_PATH}${PG_RESTORE}" -l "$file" | grep -q -- "ACL - public postgres")"; then
if ! "${PG_PATH}${PG_RESTORE}" -l "$file" | grep -q -- "ACL - public postgres"; then
echo "? Fixing missing basic public schema ACLs from DB $database [$_host:$_port] @ $(date +"%F %T")";
# echo "GRANT USAGE ON SCHEMA public TO public;" | "${PG_PATH}${PG_PSQL}" -U postgres -Atq $host $port $database;
# echo "GRANT CREATE ON SCHEMA public TO public;" | "${PG_PATH}${PG_PSQL}" -U postgres -Atq $host $port $database;
# grant usage on schema public to public;
_PG_PARAMS=("${PG_PARAMS[@]}");
_PG_PARAMS+=("-Atq" "-c" "GRANT USAGE ON SCHEMA public TO public;" "${database}");