postgres - step setup to install from repository

postgres - modernize and add simple status step
This commit is contained in:
2022-12-19 22:41:50 +01:00
parent 351a712d0e
commit 36f510b60a

View File

@@ -1,48 +1,74 @@
#!/bin/bash #!/bin/bash
toolName=postgres readonly toolName=postgres
toolDeps=postgresql readonly toolDeps=postgresql
toolUser=postgres 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 # Needed for different steps
postgresDb="" postgresDb=""
postgresUser="" postgresUser=""
postgresPass="" 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() { seq_config() {
initSeqConfig "$CONFIG_FILE_NAME" "$CONFIG_FILE_TEMPLATE" if ! initSeqConfig "${seq_configName:?}" "${seq_configTemplate:?}" ; then
if [ $? -eq 0 ] ; then # End if no configuration file exists
CONFIG=1 dry || return 1
fi fi
## Apt cmdline option to suppress user interaction
interactive || sq_aptOpt="-y"
return 0
} }
step_1_info() { echo "Installing $toolName dependencies"; } step_1_info() { echo "Status"; }
step_1_alias() { echo "install"; } step_1_alias() { echo "status"; }
step_1() { 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 exe apt update
endReturn -o $? "Updating apt repositories failed" endReturn -o $? "Updating apt repositories failed"
exe apt install $toolDeps ${sq_aptOpt}
if quiet ; then
aptOption="-y"
else
aptOption=""
fi
exe apt install $toolDeps $aptOption
} }
step_2_info() { echo "Create postgres database"; } step_20_info() { echo "Create postgres database"; }
step_2_alias() { echo "createdb"; } step_20_alias() { echo "createdb"; }
step_2() { step_20() {
readDatabaseInfos readDatabaseInfos
exe cd ~postgres exe cd ~postgres
@@ -51,13 +77,13 @@ step_2() {
exe su ${toolUser} -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE \"${postgresDb}\" to ${postgresUser};\"" exe su ${toolUser} -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE \"${postgresDb}\" to ${postgresUser};\""
} }
step_4_info() { echo "Drop a postgres database"; } step_22_info() { echo "Drop a postgres database"; }
step_4_options() { echo "[DATABASE]"; } step_22_options() { echo "[DATABASE]"; }
step_4_alias() { echo "dropdb"; } step_22_alias() { echo "dropdb"; }
step_4() { step_22() {
shift shift
local dbname=$1 local dbname=$1
if [ -z $dbname ]; then if [ -z "$dbname" ]; then
readDatabaseInfos readDatabaseInfos
dbname=$postgresDb dbname=$postgresDb
fi fi
@@ -65,13 +91,13 @@ step_4() {
exe su ${toolUser} -c "psql -c \"DROP DATABASE ${dbname};\"" exe su ${toolUser} -c "psql -c \"DROP DATABASE ${dbname};\""
} }
step_6_info() { echo "Size of a postgres database"; } step_24_info() { echo "Size of a postgres database"; }
step_6_options() { echo "[DATABASE]"; } step_24_options() { echo "[DATABASE]"; }
step_6_alias() { echo "sizedb"; } step_24_alias() { echo "sizedb"; }
step_6() { step_24() {
shift shift
local dbname=$1 local dbname=$1
if [ -z $dbname ]; then if [ -z "$dbname" ]; then
readDatabaseInfos readDatabaseInfos
dbname=$postgresDb dbname=$postgresDb
fi fi
@@ -79,20 +105,20 @@ step_6() {
exe su ${toolUser} -c "psql -c \"SELECT pg_size_pretty( pg_database_size('$dbname') );\"" exe su ${toolUser} -c "psql -c \"SELECT pg_size_pretty( pg_database_size('$dbname') );\""
} }
step_8_info() { echo "List available databases"; } step_26_info() { echo "List available databases"; }
step_8_alias() { echo "listdb"; } step_26_alias() { echo "listdb"; }
step_8() { step_26() {
exe cd ~postgres exe cd ~postgres
exe su ${toolUser} -c "psql -c '\l'" exe su ${toolUser} -c "psql -c '\l'"
} }
step_10_info() { echo "List all tables of a postgres database"; } step_28_info() { echo "List all tables of a postgres database"; }
step_10_options() { echo "[DATABASE]"; } step_28_options() { echo "[DATABASE]"; }
step_10_alias() { echo "listtables"; } step_28_alias() { echo "listtables"; }
step_10() { step_28() {
shift shift
local dbname=$1 local dbname=$1
if [ -z $dbname ]; then if [ -z "$dbname" ]; then
readDatabaseInfos readDatabaseInfos
dbname=$postgresDb dbname=$postgresDb
fi fi
@@ -100,15 +126,15 @@ step_10() {
exe su ${toolUser} -c "psql -d $dbname -c \"\\dt\"" exe su ${toolUser} -c "psql -d $dbname -c \"\\dt\""
} }
step_20_info() { echo "Backup ${toolName} database"; } step_40_info() { echo "Backup ${toolName} database"; }
step_20_options() { echo "[DATABASE]"; } step_40_options() { echo "[DATABASE]"; }
step_20_alias() { echo "backupdb"; } step_40_alias() { echo "backupdb"; }
step_20() { step_40() {
shift shift
local dbname=$1 local dbname="$1"
if [ -z $dbname ]; then if [ -z "$dbname" ]; then
readDatabaseInfos readDatabaseInfos
dbname=$postgresDb dbname="$postgresDb"
fi fi
#if [ ! -s ~/.pgpass ] ; then #if [ ! -s ~/.pgpass ] ; then
# echo " [I] For unattended backup please define ~/.pgpass containing credentials" # 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" #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 mkdir -p "$POSTGRES_BACKUP_DIR"
exe cd ~postgres 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_42_info() { echo "Postgres database restore"; }
step_22_alias() { echo "restoredb"; } step_42_alias() { echo "restoredb"; }
step_22() { step_42() {
echo " [I] Postgres database restore procedure" echo " [I] Postgres database restore procedure"
echo "1. Create a empty postgres database first (step 4)" echo "1. Create a empty postgres database first (step 4)"
echo "2. psql -h <host> -U <database user> -d <database name> -W -f <sql dump file>" echo "2. psql -h <host> -U <database user> -d <database name> -W -f <sql dump file>"
@@ -139,39 +165,47 @@ step_22() {
exe su ${toolUser} -c "psql -c '\du'" exe su ${toolUser} -c "psql -c '\du'"
} }
step_24_info() { step_44_info() {
local DELYEAR=$(($(date +%Y)-2)) local DELYEAR=$(($(date +%Y)-2))
echo "Clean all ${DELYEAR} backups of a database"; echo "Clean all ${DELYEAR} backups of a database";
} }
step_24_options() { echo "[DATABASE]"; } step_44_options() { echo "[DATABASE]"; }
step_24_alias() { echo "backupclean"; } step_44_alias() { echo "backupclean"; }
step_24() { step_44() {
shift shift
local DELYEAR=$(($(date +%Y)-2)) local DELYEAR=$(($(date +%Y)-2))
local dbname=$1 local dbname=$1
if [ -z $dbname ]; then if [ -z "$dbname" ]; then
readDatabaseInfos readDatabaseInfos
dbname=$postgresDb dbname="$postgresDb"
fi 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 # Read postgres database information dbname/user/pass if empty
readDatabaseInfos() { readDatabaseInfos() {
if [ "$postgresDb" == "" ] ; then if [ "$postgresDb" == "" ] ; then
read -p "Enter postgres database name: " postgresDb postgresDb=$(ask "Enter postgres database name: ")
endIfEmpty postgresDb "database" endIfEmpty postgresDb "database"
fi fi
if [ "$postgresUser" == "" ] ; then if [ "$postgresUser" == "" ] ; then
read -p "Enter postgres user name: " postgresUser postgresUser=$(ask "Enter postgres user name: ")
endIfEmpty postgresUser "user name" endIfEmpty postgresUser "user name"
fi fi
if [ "$postgresPass" == "" ] ; then if [ "$postgresPass" == "" ] ; then
read -s -p "Enter postgres password: " postgresPass postgresPass=$(ask -s "Enter postgres password: "); echo
endIfEmpty postgresPass "password" endIfEmpty postgresPass "password"
if [[ ! ${postgresPass} == "$(ask -s "Repeat postgres password: ")" ]] ; then
echo
fatal -e "Passwords don't match"
else
echo
fi
fi fi
echo echo
} }
# shellcheck disable=SC2034 # Appears unused
readonly sqr_minVersion=16 readonly sqr_minVersion=16
# shellcheck disable=SC1091 # Don't follow this source
. /usr/local/bin/sequencer.sh . /usr/local/bin/sequencer.sh