#!/bin/sh #----------------------------------------------------------------------------- # /var/install/bin/postgresql-common-browser-helpers # # Creation: 2019-03-30 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 # --------------------------------------------------------------------------- # show a single page of databases found # @param ${1} is the page title # @param ${2} is the page subtitle # @param ${3} starting index # @param ${4} max number of entreis # @param ${5} max number of lines to be used # --------------------------------------------------------------------------- show_databases_page() { local title=${1} local subtitle=${2} local startidx=${3} local max_entries=${4} local length=${5} clrhome # check for max. index if [ "$((${startidx} + ${length} - 1))" -gt "${max_entries}" ] then length=$((${max_entries} - ${startidx} + 1)) fi local endidx=$((${startidx} + ${length} - 1)) # print table heading mecho --info "${title}" echo mecho --info "${subtitle}" echo techo --begin '3 3r 1 15 1 10 1 10' techo --row "" --info "No" "" --info "Name" "" --info "Owner" "" --info "Encoding" local idx="${startidx}" while [ ${idx} -le ${endidx} ] do eval local dbname='${DATABASE_'${idx}'_NAME}' eval local dbowner='${DATABASE_'${idx}'_OWNER}' eval local dbencode='${DATABASE_'${idx}'_ENCODING}' techo --row "" "${idx}" "" "${dbname}" "" "${dbowner}" "" "${dbencode}" idx=$((${idx} + 1)) done techo --end gotoxy 1 $((${_EISLIB_SCREENSIZE_Y} - 5)) echo } # --------------------------------------------------------------------------- # browser list of databases (use paging to fit entries on screen) # @param ${1} is the page title # @param ${2} is the page subtitle # @param ${3} enable selection of "create" "yes" / "no" # @param ${4} name of output variable # @return 0 on success, 1 if used did abort, 127 if user wants to exit menu # --------------------------------------------------------------------------- browse_databases() { local title=${1} local subtitle=${2} local enable_create=${3} local output=${4} # get preselection (if any) local preselect eval preselect='${'${output}'}' # get database for backup local result=$(run_query "SELECT d.datname , u.usename , pg_catalog.pg_encoding_to_char(d.encoding) FROM pg_catalog.pg_database d LEFT JOIN pg_catalog.pg_user u ON d.datdba = u.usesysid ORDER BY 1;") # parse result local line local count=0 local old_ifs=${IFS} local sel_index IFS=$'\x0A' for line in ${result} do count=$((${count} + 1)) local dbname=$(echo ${line} | cut -d\| -f1) local dbowner=$(echo ${line} | cut -d\| -f2) local dbencode=$(echo ${line} | cut -d\| -f3) # preselect user specified on command line if [ "${dbname}" = "${preselect}" ] then sel_index="${count}" fi eval 'DATABASE_'${count}'_NAME'="${dbname}" eval 'DATABASE_'${count}'_OWNER'="${dbowner}" eval 'DATABASE_'${count}'_ENCODING'="${dbencode}" done IFS=${old_ifs} # show table and query user selection local index=1 while true do # get max. number of packages per page local linespp=$((${_EISLIB_SCREENSIZE_Y} - 9)) # show page content show_databases_page "${title}" "${subtitle}" "${index}" "${count}" "${linespp}" # wait for user selection local ask_tmpfile=$(/usr/bin/mktemp -t XXXXXXXXXXXXX) if [ ${index} -gt 1 ] then if [ "${enable_create}" = "yes" ] then if [ $(($index + $linespp)) -gt ${count} ] then /var/install/bin/ask 'Select' "${sel_index}" "1-${count}" \ 'c=create' 'p=previous' '^$=Return' '0=Exit' >${ask_tmpfile} else /var/install/bin/ask 'Select' "${sel_index}" "1-${count}" \ 'c=create' 'p=previous' 'n=next page' '^$=Return' '0=Exit' >${ask_tmpfile} fi else if [ $(($index + $linespp)) -gt ${count} ] then /var/install/bin/ask 'Select' "${sel_index}" "1-${count}" \ 'p=previous' '^$=Return' '0=Exit' >${ask_tmpfile} else /var/install/bin/ask 'Select' "${sel_index}" "1-${count}" \ 'p=previous' 'n=next page' '^$=Return' '0=Exit' >${ask_tmpfile} fi fi else if [ "${enable_create}" = "yes" ] then if [ $(($idx + $linespp)) -gt ${count} ] then /var/install/bin/ask 'Select' "${sel_index}" "1-${count}" \ 'c=create' '^$=Return' '0=Exit' >${ask_tmpfile} else /var/install/bin/ask 'Select' "${sel_index}" "1-${count}" \ 'c=create' 'n=next page' '^$=Return' '0=Exit' >${ask_tmpfile} fi else if [ $(($idx + $linespp)) -gt ${count} ] then /var/install/bin/ask 'Select' "${sel_index}" "1-${count}" \ '^$=Return' '0=Exit' >${ask_tmpfile} else /var/install/bin/ask 'Select' "${sel_index}" "1-${count}" \ 'n=next page' '^$=Return' '0=Exit' >${ask_tmpfile} fi fi fi # fetch user selection local rc="${?}" local answer read answer < ${ask_tmpfile} if [ ${rc} = 255 ] then answer=0 fi rm -f ${ask_tmpfile} # evaluate user selection case "${answer}" in '') return 1 ;; 0) return 127 ;; 'n') index=$(($index + $linespp)) ;; 'p') index=$(($index - $linespp)) if [ ${index} -lt 1 ] then idx=1 fi ;; 'c') eval ${output}="!create!" return 0 ;; *) eval ${output}='${DATABASE_'${answer}'_NAME}' return 0 ;; esac done return 0 } # --------------------------------------------------------------------------- # show a single page of backup files # @param ${1} is the page title # @param ${2} is the page subtitle # @param ${3} starting index # @param ${4} max number of entreis # @param ${5} max number of lines to be used # --------------------------------------------------------------------------- show_files_page() { local title=${1} local subtitle=${2} local startidx=${3} local max_entries=${4} local length=${5} clrhome # check for max. index if [ "$((${startidx} + ${length} - 1))" -gt "${max_entries}" ] then length=$((${max_entries} - ${startidx} + 1)) fi local endidx=$((${startidx} + ${length} - 1)) # print table heading mecho --info "${title}" echo mecho --info "${subtitle}" echo if [ "${max_entries}" -gt "0" ] then techo --begin '3 3r 1 52 1 17' techo --row "" --info "No" "" --info "Filename" "" --info "Type" local idx="${startidx}" while [ ${idx} -le ${endidx} ] do eval local file='${BACKUPFILE_'${idx}'_NAME}' eval local type="File" if [ "${file: -7}" = ".backup" ] then type="Database Backup" fi if [ "${file: -11}" = ".dumpall.gz" ] then type="Cluster Backup" fi techo --row "" "${idx}" "" "${file:-7}" "" "${type}" idx=$((${idx} + 1)) done techo --end else echo "" echo "No backup files found!" echo "" fi gotoxy 1 $((${_EISLIB_SCREENSIZE_Y} - 3)) echo } # --------------------------------------------------------------------------- # browser list of backup files (use paging to fit entries on screen) # @param ${1} is the page title # @param ${2} is the page subtitle # @param ${3} path and file name pattern # @param ${4} name of output variable # @return 0 on success, 1 if used did abort, 127 if user wants to exit menu # --------------------------------------------------------------------------- browse_backup_files() { local title=${1} local subtitle=${2} local path_pattern=${3} local output=${4} # get database for backup eval local result='$(/bin/ls -t -1 '"${path_pattern}"' 2>/dev/null)' # parse result local line local count=0 local old_ifs=${IFS} IFS=$'\x0A' for line in ${result} do count=$((${count} + 1)) eval 'BACKUPFILE_'${count}'_NAME'="$(basename ${line})" done IFS=${old_ifs} # show table and query user selection local index=1 while true do # get max. number of packages per page local linespp=$((${_EISLIB_SCREENSIZE_Y} - 9)) # show page content show_files_page "${title}" "${subtitle}" "${index}" "${count}" "${linespp}" # wait for user selection local ask_tmpfile=$(/usr/bin/mktemp -t XXXXXXXXXXXXX) if [ ${index} -gt 1 ] then if [ $(($index + $linespp)) -gt ${count} ] then /var/install/bin/ask 'Select' "" "1-${count}" \ 'p=previous' '^$=Return' '0=Exit' >${ask_tmpfile} else /var/install/bin/ask 'Select' "" "1-${count}" \ 'p=previous' 'n=next page' '^$=Return' '0=Exit' >${ask_tmpfile} fi else if [ $(($idx + $linespp)) -gt ${count} ] then /var/install/bin/ask 'Select' "" "1-${count}" \ '^$=Return' '0=Exit' >${ask_tmpfile} else /var/install/bin/ask 'Select' "" "1-${count}" \ 'n=next page' '^$=Return' '0=Exit' >${ask_tmpfile} fi fi # fetch user selection local rc="${?}" local answer read answer < ${ask_tmpfile} if [ ${rc} = 255 ] then answer=0 fi rm -f ${ask_tmpfile} # evaluate user selection case "${answer}" in '') return 1 ;; 0) return 127 ;; 'n') index=$(($index + $linespp)) ;; 'p') index=$(($index - $linespp)) if [ ${index} -lt 1 ] then idx=1 fi ;; *) eval ${output}='${BACKUPFILE_'${answer}'_NAME}' return 0 ;; esac done return 0 } # --------------------------------------------------------------------------- # show a single page of database users # @param ${1} is the page title # @param ${2} is the page subtitle # @param ${3} starting index # @param ${4} max number of entreis # @param ${5} max number of lines to be used # --------------------------------------------------------------------------- show_users_page() { local title=${1} local subtitle=${2} local startidx=${3} local max_entries=${4} local length=${5} clrhome # check for max. index if [ "$((${startidx} + ${length} - 1))" -gt "${max_entries}" ] then length=$((${max_entries} - ${startidx} + 1)) fi local endidx=$((${startidx} + ${length} - 1)) # print table heading mecho --info "${title}" echo mecho --info "${subtitle}" echo techo --begin '3 3r 1 15 1 10 1 10' techo --row "" --info "No" "" --info "Name" "" --info "Superuser" "" --info "Create DB" local idx="${startidx}" while [ ${idx} -le ${endidx} ] do eval local dbuser='${USER_'${idx}'_NAME}' eval local dbissuper='${USER_'${idx}'_ISSUPER}' eval local dbcreate='${USER_'${idx}'_CREATEDB}' techo --row "" "${idx}" "" "${dbuser}" "" "${dbissuper}" "" "${dbcreate}" idx=$((${idx} + 1)) done techo --end gotoxy 1 $((${_EISLIB_SCREENSIZE_Y} - 5)) echo } # --------------------------------------------------------------------------- # browser list of database users (use paging to fit entries on screen) # @param $1 is the page title # @param $2 is the page subtitle # @param $3 name of output variable # @return 0 on success, 1 if used did abort, 127 if user wants to exit menu # --------------------------------------------------------------------------- browse_users() { local title=${1} local subtitle=${2} local output=${3} # get preselection (if any) local preselect eval preselect='${'${output}'}' # get users local result=$(run_query "SELECT rolname, CASE (rolsuper) WHEN 't' THEN 'yes' ELSE 'no' END, CASE (rolcreatedb) WHEN 't' THEN 'yes' ELSE 'no' END FROM pg_roles WHERE rolcanlogin = 't';") # parse result local line local count=0 local old_ifs=${IFS} local sel_index IFS=$'\x0A' for line in ${result} do count=$((${count} + 1)) local dbuser=$(echo ${line} | cut -d\| -f1) local dbissuper=$(echo ${line} | cut -d\| -f2) local dbcreate=$(echo ${line} | cut -d\| -f3) # preselect user specified on command line if [ "${dbuser}" = "${preselect}" ] then sel_index="${count}" fi eval 'USER_'${count}'_NAME'="${dbuser}" eval 'USER_'${count}'_ISSUPER'="${dbissuper}" eval 'USER_'${count}'_CREATEDB'="${dbcreate}" done IFS=${old_ifs} # show table and query user selection local index=1 while true do # get max. number of packages per page local linespp=$((${_EISLIB_SCREENSIZE_Y} - 9)) # show page content show_users_page "${title}" "${subtitle}" "${index}" "${count}" "${linespp}" # wait for user selection local ask_tmpfile=$(/usr/bin/mktemp -t XXXXXXXXXXXXX) if [ ${index} -gt 1 ] then if [ $(($index + $linespp)) -gt ${count} ] then /var/install/bin/ask "Select" "${sel_index}" "1-${count}" \ 'p=previous' '^$=Return' '0=Exit' >${ask_tmpfile} else /var/install/bin/ask "Select" "${sel_index}" "1-${count}" \ 'p=previous' 'n=next page' '^$=Return' '0=Exit' >${ask_tmpfile} fi else if [ $(($idx + $linespp)) -gt ${count} ] then /var/install/bin/ask "Select" "${sel_index}" "1-${count}" \ '^$=Return' '0=Exit' >${ask_tmpfile} else /var/install/bin/ask "Select" "${sel_index}" "1-${count}" \ 'n=next page' '^$=Return' '0=Exit' >${ask_tmpfile} fi fi # fetch user selection local rc="${?}" local answer read answer < ${ask_tmpfile} if [ ${rc} = 255 ] then answer=0 fi rm -f ${ask_tmpfile} # evaluate user selection case "${answer}" in '') return 1 ;; 0) return 127 ;; 'n') index=$(($index + $linespp)) ;; 'p') index=$(($index - $linespp)) if [ ${index} -lt 1 ] then idx=1 fi ;; *) eval ${output}='${USER_'${answer}'_NAME}' return 0 ;; esac done return 0 }