#!/bin/sh #----------------------------------------------------------------------------- # /var/install/bin/postgresql-common-restore-db # # Creation: 2019-03-24 dv # Last Update: 2023-07-23 09:11:10 # # Copyright (c) 2024 the eisfair team, team(at)eisfair(dot)org # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #----------------------------------------------------------------------------- . /var/install/include/eislib . /var/install/bin/postgresql-common-browser-helpers #----------------------------------------------------------------------------- # show help on arguments and possible options # @param n/a # @return n/a #----------------------------------------------------------------------------- show_help() { cat < database user [postgres] --mount= action to perform before restore --umount= action to perform after restore --folder= output folder [/tmp] --port= database service port [5432] --bin-dir= path where postgresql tools reside [/usr/bin] EOF } # --------------------------------------------------------------------------- # move the cursor to the specified screen position # @param $1 X cursor position # @param $2 Y cursor position # @return n/a # --------------------------------------------------------------------------- gotoxy() { echo -e "\033[$2;$1H\c"; } # --------------------------------------------------------------------------- # execute database query # @param query to execute # --------------------------------------------------------------------------- run_query() { echo "${1}" | \ ${bin_dir}/psql -p "${port}" template1 postgres -t -A } # --------------------------------------------------------------------------- # connect to server and query version # @return 0 on success, 1 on failure # --------------------------------------------------------------------------- identify_server() { local result=$(run_query "SELECT version();") if [ "${?}" -eq "0" ] then server=$(echo ${result} | cut -d\| -f1) server=$(echo ${server} | cut -d\, -f1) return 0 fi return 1 } # --------------------------------------------------------------------------- # wait for user to acknowledge settings done by previous steps # @return 0 on success, 1 if used did abort, 127 if user wants to exit menu # --------------------------------------------------------------------------- user_acknowledge() { clrhome # print table heading mecho --info "DATABASE RESTORE - ${server}" echo mecho --info "STEP 4: Final Acknowledgement:" echo echo "If you proceed, the following settings will be used" echo "to restore the database:" echo # print summary of settings mecho --info -n "Backup File: " echo "${filename}" mecho --info -n "Backup Folder: " echo "${folder}" mecho --info -n "Database: " echo "${database}" mecho --info -n "Database User: " echo "${user}" echo if [ "${re_create}" = "yes" ] then mecho --warn "WARNING!" mecho "You chose to (re)create the database as named in" mecho "the backup file. Be aware that you will lose all" mecho "data of a database that might already exist under" mecho "the same name." echo fi # get final acknowledge local ask_file=$(mktemp -t .XXXXXXXXX) local rc local ok_to_backup /var/install/bin/ask "Continue" "y" >${ask_file} rc=${?} ok_to_backup=$(cat ${ask_file}) rm -f ${ask_file} if [ ${rc} = 255 ] then return 127 fi if [ "${ok_to_backup}" = "yes" ] then return 0 else return 1 fi } # --------------------------------------------------------------------------- # perform database restoration # @return 0 on success, 1 in case of error # --------------------------------------------------------------------------- perform_restore() { clrhome # print table heading mecho --info "DATABASE RESTORE - ${server}" echo mecho --info "STEP 5: Restoration of database:" echo # perform restoration if [ "${re_create}" = "yes" ] then ${bin_dir}/pg_restore \ --port "${port}" \ --username "${user}" \ --dbname "postgres" \ --create \ --if-exists \ --clean \ -v \ "${folder}/${filename}" else ${bin_dir}/pg_restore \ --port "${port}" \ --username "${user}" \ --dbname "${database}" \ --if-exists \ --clean \ --single-transaction \ -v \ "${folder}/${filename}" fi if [ "${?}" = "0" ] then mecho mecho --info "Restoration succeeded!" mecho return 0 else mecho mecho --error "Restoration failed!" mecho return 1 fi } # --------------------------------------------------------------------------- # perform optional mount operation # @return 0 on success, 1 in case of error # --------------------------------------------------------------------------- perform_mount() { if [ -n "${mount_cmd}" ] then eval ${mount_cmd} 2>&1 if [ "$?" -ne "0" ] then echo "error: failed to mount backup volume!" >&2 return 1 fi fi return 0 } # --------------------------------------------------------------------------- # perform optional unmount operation # @return 0 on success, 1 in case of error # --------------------------------------------------------------------------- perform_umount() { # do umount step if required if [ -n "${umount_cmd}" ] then eval ${umount_cmd} 2>&1 if [ "$?" -ne "0" ] then echo "error: failed to unmount backup volume!" >&2 return 1 fi fi } # --------------------------------------------------------------------------- # main routine # --------------------------------------------------------------------------- main() { port="5432" user="postgres" folder="/tmp" bin_dir="/usr/bin" re_create="no" # read option switches while [ "${1:0:2}" = "--" ] do case ${1} in --help) show_help exit 0 ;; --bin-dir) shift bin_dir="${1}" shift ;; --bin-dir=*) bin_dir="${1:10}" shift ;; --user) shift user="${1}" shift ;; --user=*) user="${1:7}" shift ;; --mount) shift mount_cmd="${1}" shift ;; --mount=*) mount_cmd="${1:8}" shift ;; --umount) shift umount_cmd="${1}" shift ;; --umount=*) umount_cmd="${1:9}" shift ;; --folder) shift folder="${1}" shift ;; --folder=*) folder="${1:9}" shift ;; --port) shift port="${1}" shift ;; --port=*) port="${1:7}" shift ;; *) echo "error: invalid option switch \"$1\"!" >&2 exit 1 ;; esac done if [ "$#" -gt "0" ] then echo "error: invalid option switch \"$1\"!" >&2 exit 1 fi # fetch version from server if ! identify_server then exit 1 fi # do mount step if required if ! perform_mount then exit 1 fi # create backup folder (if not already present) mkdir -p "${folder}" 2>&1 >/dev/null # select backup to restore browse_backup_files \ "DATABASE RESTORE - ${server}" \ "STEP 1: Select file to restore from \"${folder}\":" \ "${folder}/*.backup" \ "filename" rc="${?}" if [ "${rc}" -ne "0" ] then perform_umount exit ${rc} fi # select database to restore to browse_databases \ "DATABASE RESTORE - ${server}" \ "STEP 2: Select database to restore to or select \"create\" to (re)create the database as named in the backup file." \ "yes" \ "database" rc="${?}" if [ "${rc}" -ne "0" ] then perform_umount exit ${rc} fi if [ "${database}" = "!create!" ] then re_create="yes" fi # select user to perform the operation browse_users \ "DATABASE RESTORE - ${server}" \ "STEP 3: Select database user to perform the restoration:" \ "user" rc="${?}" if [ "${rc}" -ne "0" ] then perform_umount exit ${rc} fi # wait for user acknowledgement if ! user_acknowledge then rc=${?} perform_umount exit ${rc} fi # perform restoration if ! perform_restore then perform_umount exit 1 fi # do umount step if required if ! perform_umount then exit 1 fi } # --------------------------------------------------------------------------- # call function main # --------------------------------------------------------------------------- main "${@}" exit 0 # --------------------------------------------------------------------------- # end # ---------------------------------------------------------------------------