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:
@@ -33,6 +33,7 @@ schema='';
|
||||
NO_ASK=0;
|
||||
TEMPLATEDB='template0';
|
||||
SCHEMA_ONLY=0;
|
||||
ERROR=0;
|
||||
REDHAT=0;
|
||||
DRY_RUN=0;
|
||||
BC='/usr/bin/bc';
|
||||
@@ -274,10 +275,18 @@ if [ -r "$file" ] && { [ ! "$owner" ] || [ ! "$database" ] || [ ! "$encoding" ];
|
||||
_owner=$(echo "${db_file}" | cut -d "." -f 2);
|
||||
__encoding=$(echo "${db_file}" | cut -d "." -f 3);
|
||||
# set the others as optional
|
||||
_ident=$(echo "${db_file}" | cut -d "." -f 4 | cut -d "-" -f 2); # db version first part
|
||||
_ident=$_ident'.'$(echo "${db_file}" | cut -d "." -f 5 | cut -d "_" -f 1); # db version, second part (after .)
|
||||
__host=$(echo "${db_file}" | cut -d "." -f 4 | cut -d "_" -f 2);
|
||||
__port=$(echo "${db_file}" | cut -d "." -f 4 | cut -d "_" -f 3);
|
||||
# the last _ is for version 10 or higher
|
||||
# db version, without prefix of DB type
|
||||
_ident=$(echo "${db_file}" | 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 [ "${_ident}" -lt 10 ]; then
|
||||
# db version, second part (after .)
|
||||
_ident=$_ident'.'$(echo "$db_file" | cut -d "." -f 5 | cut -d "_" -f 1);
|
||||
cut_pos=5;
|
||||
fi;
|
||||
__host=$(echo "${db_file}" | cut -d "." -f ${cut_pos} | cut -d "_" -f 2);
|
||||
__port=$(echo "${db_file}" | cut -d "." -f ${cut_pos} | cut -d "_" -f 3);
|
||||
# if any of those are not set, override by the file name settings
|
||||
if [ ! "$owner" ]; then
|
||||
owner=$_owner;
|
||||
@@ -371,14 +380,16 @@ fi;
|
||||
# if I cannot connect with user postgres to template1, the restore won't work
|
||||
_PG_PARAMS=("${PG_PARAMS[@]}");
|
||||
_PG_PARAMS+=("-U" "postgres" "template1" "-q" "-t" "-X" "-A" "-F" "," "-c" "SELECT version();");
|
||||
_output=$("${PG_PSQL}" "${PG_PARAMS[@]}" 2>&1);
|
||||
_output=$("${PG_PSQL}" "${_PG_PARAMS[@]}" 2>&1);
|
||||
found=$(echo "$_output" | grep "PostgreSQL");
|
||||
# if the output does not have the PG version string, we have an error and abort
|
||||
if [ -z "$found" ]; then
|
||||
echo "Cannot connect to the database: $_output";
|
||||
exit 1;
|
||||
fi;
|
||||
|
||||
if [ ${DRY_RUN} ]; then
|
||||
echo "**** [DRY RUN] ****";
|
||||
fi;
|
||||
echo "Will drop database '$database' on host '$_host:$_port' and load file '$file' with user '$owner', set encoding '$encoding' and use database version '$ident'";
|
||||
if [ $SCHEMA_ONLY -eq 1 ]; then
|
||||
echo "!!!!!!! WILL ONLY RESTORE SCHEMA, NO DATA !!!!!!!";
|
||||
@@ -400,7 +411,6 @@ else
|
||||
_PG_PARAMS=("${PG_PARAMS[@]}");
|
||||
_PG_PARAMS+=("-U" "postgres" "${database}");
|
||||
if [ $DRY_RUN -eq 0 ]; then
|
||||
# $PG_DROPDB -U postgres $host $port $database;
|
||||
"${PG_DROPDB}" "${PG_PARAMS[@]}";
|
||||
else
|
||||
echo "${PG_DROPDB} ${PG_PARAMS[*]}";
|
||||
@@ -410,7 +420,6 @@ else
|
||||
_PG_PARAMS=("${PG_PARAMS[@]}");
|
||||
_PG_PARAMS+=("-U" "postgres" "-O" "${owner}" "-E" "${encoding}" "-T" "${TEMPLATEDB}" "${database}");
|
||||
if [ $DRY_RUN -eq 0 ]; then
|
||||
# $PG_CREATEDB -U postgres -O $owner -E $encoding -T $TEMPLATEDB $host $port $database;
|
||||
"${PG_CREATEDB}" "${_PG_PARAMS[@]}";
|
||||
else
|
||||
echo "${PG_CREATEDB} ${_PG_PARAMS[*]}";
|
||||
@@ -421,7 +430,6 @@ else
|
||||
_PG_PARAMS+=("-U" "postgres" "plpgsql" "${database}");
|
||||
echo "Create plpgsql lang in DB $database on [$_host:$_port] @ $(date +"%F %T")";
|
||||
if [ $DRY_RUN -eq 0 ]; then
|
||||
# $PG_CREATELANG -U postgres plpgsql $host $port $database;
|
||||
"${PG_CREATELANG}" "${_PG_PARAMS[@]}";
|
||||
else
|
||||
echo "${PG_CREATELANG} ${_PG_PARAMS[*]}";
|
||||
@@ -433,18 +441,14 @@ else
|
||||
_PG_PARAMS+=("${PG_PARAM_ROLE[@]}")
|
||||
_PG_PARAMS+=("-U" "postgres" "-d" "${database}" "-F" "c" "-v" "-c" "${schema}" "-j" "${MAX_JOBS}" "${file}");
|
||||
if [ $DRY_RUN -eq 0 ]; then
|
||||
# $PG_RESTORE -U postgres -d $database -F c -v -c $schema -j $MAX_JOBS $host $port $role $file 2>restore_errors.$LOG_FILE_EXT;
|
||||
"${PG_RESTORE}" "${_PG_PARAMS[@]}" 2>"restore_errors.${LOG_FILE_EXT}";
|
||||
else
|
||||
echo "${PG_RESTORE} ${_PG_PARAMS[*]} 2>restore_errors.${LOG_FILE_EXT}";
|
||||
fi;
|
||||
# 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 [ -z "$("$PG_RESTORE" -l "$file" | grep -- "ACL - public postgres")" ]; then
|
||||
if ! "$("${PG_RESTORE}" -l "$file" | grep -q -- "ACL - public postgres")"; then
|
||||
if ! "${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_PSQL -U postgres -Atq $host $port $database;
|
||||
# echo "GRANT CREATE ON SCHEMA public TO public;" | $PG_PSQL -U postgres -Atq $host $port $database;
|
||||
# grant usage on schema public to public;
|
||||
_PG_PARAMS=("${PG_PARAMS[@]}");
|
||||
_PG_PARAMS+=("-U" "postgres" "-Atq" "-c" "GRANT USAGE ON SCHEMA public TO public;" "${database}");
|
||||
@@ -462,13 +466,11 @@ else
|
||||
_PG_PARAMS_OUT=("${PG_PARAMS[@]}");
|
||||
_PG_PARAMS_OUT+=("-U" "postgres" "-e" "-f" "${TEMP_FILE}" "${database}");
|
||||
if [ $DRY_RUN -eq 0 ]; then
|
||||
# echo "${reset_query}" | $PG_PSQL -U postgres -Atq $host $port -o $TEMP_FILE $database
|
||||
"${PG_PSQL}" "${_PG_PARAMS[@]}";
|
||||
# $PG_PSQL -U postgres $host $port -e -f $TEMP_FILE $database 1>output_sequence.$LOG_FILE_EXT 2>errors_sequence.$database.$LOG_FILE_EXT;
|
||||
"${PG_PSQL}" "${_PG_PARAMS_OUT[@]}" 1>"output_sequence.${LOG_FILE_EXT}" 2>"errors_sequence.${database}.${LOG_FILE_EXT}";
|
||||
rm "${TEMP_FILE}";
|
||||
else
|
||||
echo "${reset_query}";
|
||||
echo "${PG_PSQL} ${_PG_PARAMS[*]}";
|
||||
echo "${PG_PSQL} ${_PG_PARAMS_OUT[*]} 1>output_sequence.${LOG_FILE_EXT} 2>errors_sequence.${database}.${LOG_FILE_EXT}";
|
||||
fi;
|
||||
echo "Restore of data $file for DB $database [$_host:$_port] finished";
|
||||
|
||||
Reference in New Issue
Block a user