From 36f510b60aa4f307449290dc638bab4261c89801 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Mon, 19 Dec 2022 22:41:50 +0100 Subject: [PATCH] postgres - step setup to install from repository postgres - modernize and add simple status step --- seqs/postgres.sh | 166 ++++++++++++++++++++++++++++------------------- 1 file changed, 100 insertions(+), 66 deletions(-) diff --git a/seqs/postgres.sh b/seqs/postgres.sh index dfd2775..2e2cb8d 100755 --- a/seqs/postgres.sh +++ b/seqs/postgres.sh @@ -1,48 +1,74 @@ #!/bin/bash -toolName=postgres -toolDeps=postgresql -toolUser=postgres +readonly toolName=postgres +readonly toolDeps=postgresql +readonly toolUser=postgres + +sq_repoUrl="http://apt.postgresql.org/pub/repos/apt" +sq_keyUrl="https://www.postgresql.org/media/keys/ACCC4CF8.asc" +sq_aptOpt= # Needed for different steps postgresDb="" postgresUser="" postgresPass="" -# Get script working directory -# (when called from a different directory) -WDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >>/dev/null 2>&1 && pwd )" -CONFIG=0 -CONFIG_FILE_NAME="postgres.cfg" -CONFIG_FILE_TEMPLATE="$WDIR/${CONFIG_FILE_NAME}.example" - seq_config() { - initSeqConfig "$CONFIG_FILE_NAME" "$CONFIG_FILE_TEMPLATE" - if [ $? -eq 0 ] ; then - CONFIG=1 + if ! initSeqConfig "${seq_configName:?}" "${seq_configTemplate:?}" ; then + # End if no configuration file exists + dry || return 1 fi + + ## Apt cmdline option to suppress user interaction + interactive || sq_aptOpt="-y" + + return 0 } -step_1_info() { echo "Installing $toolName dependencies"; } -step_1_alias() { echo "install"; } +step_1_info() { echo "Status"; } +step_1_alias() { echo "status"; } step_1() { - local aptOption= + exe apt policy postgresql +} +step_10_info() { + echo "Setup latest apt source list for ${toolName}:" + echoinfo "$sq_repoUrl" +} +step_10_alias() { echo "setup"; } +step_10() { + local lArch= + case $(uname -m) in + aarch64) + lArch=arm64;; + esac + if [[ -n ${lArch:-} ]] ; then + info "Detected processor architecture: ${lArch}" + lArch="[arch=${lArch}]" + fi + + info "Installing custom repository prerequisites:" + exe apt update + exe apt install apt-transport-https lsb-release ca-certificates curl ${sq_aptOpt} + info "Setup postgresql repository including gpg key" + exep curl -fsSL ${sq_keyUrl:?} "|" gpg --dearmor -o "/etc/apt/trusted.gpg.d/postgresql-keyring.gpg" + addConf -c "deb ${lArch:-} ${sq_repoUrl:?} $(lsb_release -cs)-pgdg main" "/etc/apt/sources.list.d/postgresql.list" + addConf -es "Package: *\nPin: origin apt.postgresql.org\nPin: release c=main\nPin-Priority: 900" \ + /etc/apt/preferences.d/99postgresql + exe apt update +} + +step_11_info() { echo "Installing $toolName dependencies"; } +step_11_alias() { echo "install"; } +step_11() { exe apt update endReturn -o $? "Updating apt repositories failed" - - if quiet ; then - aptOption="-y" - else - aptOption="" - fi - - exe apt install $toolDeps $aptOption + exe apt install $toolDeps ${sq_aptOpt} } -step_2_info() { echo "Create postgres database"; } -step_2_alias() { echo "createdb"; } -step_2() { +step_20_info() { echo "Create postgres database"; } +step_20_alias() { echo "createdb"; } +step_20() { readDatabaseInfos exe cd ~postgres @@ -51,13 +77,13 @@ step_2() { exe su ${toolUser} -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE \"${postgresDb}\" to ${postgresUser};\"" } -step_4_info() { echo "Drop a postgres database"; } -step_4_options() { echo "[DATABASE]"; } -step_4_alias() { echo "dropdb"; } -step_4() { +step_22_info() { echo "Drop a postgres database"; } +step_22_options() { echo "[DATABASE]"; } +step_22_alias() { echo "dropdb"; } +step_22() { shift local dbname=$1 - if [ -z $dbname ]; then + if [ -z "$dbname" ]; then readDatabaseInfos dbname=$postgresDb fi @@ -65,13 +91,13 @@ step_4() { exe su ${toolUser} -c "psql -c \"DROP DATABASE ${dbname};\"" } -step_6_info() { echo "Size of a postgres database"; } -step_6_options() { echo "[DATABASE]"; } -step_6_alias() { echo "sizedb"; } -step_6() { +step_24_info() { echo "Size of a postgres database"; } +step_24_options() { echo "[DATABASE]"; } +step_24_alias() { echo "sizedb"; } +step_24() { shift local dbname=$1 - if [ -z $dbname ]; then + if [ -z "$dbname" ]; then readDatabaseInfos dbname=$postgresDb fi @@ -79,20 +105,20 @@ step_6() { exe su ${toolUser} -c "psql -c \"SELECT pg_size_pretty( pg_database_size('$dbname') );\"" } -step_8_info() { echo "List available databases"; } -step_8_alias() { echo "listdb"; } -step_8() { +step_26_info() { echo "List available databases"; } +step_26_alias() { echo "listdb"; } +step_26() { exe cd ~postgres exe su ${toolUser} -c "psql -c '\l'" } -step_10_info() { echo "List all tables of a postgres database"; } -step_10_options() { echo "[DATABASE]"; } -step_10_alias() { echo "listtables"; } -step_10() { +step_28_info() { echo "List all tables of a postgres database"; } +step_28_options() { echo "[DATABASE]"; } +step_28_alias() { echo "listtables"; } +step_28() { shift local dbname=$1 - if [ -z $dbname ]; then + if [ -z "$dbname" ]; then readDatabaseInfos dbname=$postgresDb fi @@ -100,15 +126,15 @@ step_10() { exe su ${toolUser} -c "psql -d $dbname -c \"\\dt\"" } -step_20_info() { echo "Backup ${toolName} database"; } -step_20_options() { echo "[DATABASE]"; } -step_20_alias() { echo "backupdb"; } -step_20() { +step_40_info() { echo "Backup ${toolName} database"; } +step_40_options() { echo "[DATABASE]"; } +step_40_alias() { echo "backupdb"; } +step_40() { shift - local dbname=$1 - if [ -z $dbname ]; then + local dbname="$1" + if [ -z "$dbname" ]; then readDatabaseInfos - dbname=$postgresDb + dbname="$postgresDb" fi #if [ ! -s ~/.pgpass ] ; then # echo " [I] For unattended backup please define ~/.pgpass containing credentials" @@ -118,12 +144,12 @@ step_20() { #exep "pg_dump -h 127.0.0.1 -U ${postgresUser} -Fc synapse | bzip2 -c > ${toolDbBackupFolder}/`date +%Y-%m-%d\"_\"%H-%M-%S`.backup.bz2" exe mkdir -p "$POSTGRES_BACKUP_DIR" exe cd ~postgres - exep "su ${toolUser} -c \"pg_dump -Fc $dbname\" | bzip2 -c > ${POSTGRES_BACKUP_DIR}/`date +%Y-%m-%d\"_\"%H-%M-%S`_${dbname}.backup.bz2" + exep "su ${toolUser} -c \"pg_dump -Fc $dbname\" | bzip2 -c > ${POSTGRES_BACKUP_DIR}/$(date +%Y-%m-%d\"_\"%H-%M-%S)_${dbname}.backup.bz2" } -step_22_info() { echo "Postgres database restore"; } -step_22_alias() { echo "restoredb"; } -step_22() { +step_42_info() { echo "Postgres database restore"; } +step_42_alias() { echo "restoredb"; } +step_42() { echo " [I] Postgres database restore procedure" echo "1. Create a empty postgres database first (step 4)" echo "2. psql -h -U -d -W -f " @@ -139,39 +165,47 @@ step_22() { exe su ${toolUser} -c "psql -c '\du'" } -step_24_info() { +step_44_info() { local DELYEAR=$(($(date +%Y)-2)) echo "Clean all ${DELYEAR} backups of a database"; } -step_24_options() { echo "[DATABASE]"; } -step_24_alias() { echo "backupclean"; } -step_24() { +step_44_options() { echo "[DATABASE]"; } +step_44_alias() { echo "backupclean"; } +step_44() { shift local DELYEAR=$(($(date +%Y)-2)) local dbname=$1 - if [ -z $dbname ]; then + if [ -z "$dbname" ]; then readDatabaseInfos - dbname=$postgresDb + dbname="$postgresDb" fi - exe rm -f ${POSTGRES_BACKUP_DIR}/${DELYEAR}*${dbname}* + exe rm -f "${POSTGRES_BACKUP_DIR}"/"${DELYEAR}"*"${dbname}"* } # Read postgres database information dbname/user/pass if empty readDatabaseInfos() { if [ "$postgresDb" == "" ] ; then - read -p "Enter postgres database name: " postgresDb + postgresDb=$(ask "Enter postgres database name: ") endIfEmpty postgresDb "database" fi if [ "$postgresUser" == "" ] ; then - read -p "Enter postgres user name: " postgresUser + postgresUser=$(ask "Enter postgres user name: ") endIfEmpty postgresUser "user name" fi if [ "$postgresPass" == "" ] ; then - read -s -p "Enter postgres password: " postgresPass + postgresPass=$(ask -s "Enter postgres password: "); echo endIfEmpty postgresPass "password" + if [[ ! ${postgresPass} == "$(ask -s "Repeat postgres password: ")" ]] ; then + echo + fatal -e "Passwords don't match" + else + echo + fi fi echo } +# shellcheck disable=SC2034 # Appears unused readonly sqr_minVersion=16 +# shellcheck disable=SC1091 # Don't follow this source . /usr/local/bin/sequencer.sh