#!/bin/bash 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="" seq_config() { 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 "Status"; } step_1_alias() { echo "status"; } step_1() { 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" exe apt install $toolDeps ${sq_aptOpt} } step_20_info() { echo "Create postgres database"; } step_20_alias() { echo "createdb"; } step_20() { readDatabaseInfos exe cd ~postgres exe su ${toolUser} -c "psql -c \"CREATE USER ${postgresUser} WITH ENCRYPTED password '${postgresPass}';\"" exe su ${toolUser} -c "psql -c \"CREATE DATABASE ${postgresDb} ENCODING \"UTF8\" LC_COLLATE='C' LC_CTYPE='C' template=template0 OWNER ${postgresUser};\"" exe su ${toolUser} -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE \"${postgresDb}\" to ${postgresUser};\"" } 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 readDatabaseInfos dbname=$postgresDb fi exe cd ~postgres exe su ${toolUser} -c "psql -c \"DROP DATABASE ${dbname};\"" } 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 readDatabaseInfos dbname=$postgresDb fi exe cd ~postgres exe su ${toolUser} -c "psql -c \"SELECT pg_size_pretty( pg_database_size('$dbname') );\"" } 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_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 readDatabaseInfos dbname=$postgresDb fi exe cd ~postgres exe su ${toolUser} -c "psql -d $dbname -c \"\\dt\"" } 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 readDatabaseInfos dbname="$postgresDb" fi #if [ ! -s ~/.pgpass ] ; then # echo " [I] For unattended backup please define ~/.pgpass containing credentials" # echo " e.g. localhost:5432:database:user:pass" #fi # -Fc custom format #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" } 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 " echo " e.g. psql -h 127.0.0.1 -U synapse -d synapse -W -f 2018-06-07_18-10-56.sql" echo "or" echo "3. Custom postgres format dump restore:" echo " pg_restore -h localhost -p 5432 -U synapse -d new_db -v \"10.70.0.61.backup\"" echo echo "Available postgresql databases:" exe cd ~postgres exe su ${toolUser} -c "psql -c '\l'" echo "Available postgresql user:" exe su ${toolUser} -c "psql -c '\du'" } step_44_info() { local DELYEAR=$(($(date +%Y)-2)) echo "Clean all ${DELYEAR} backups of a database"; } 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 readDatabaseInfos dbname="$postgresDb" fi exe rm -f "${POSTGRES_BACKUP_DIR}"/"${DELYEAR}"*"${dbname}"* } # Read postgres database information dbname/user/pass if empty readDatabaseInfos() { if [ "$postgresDb" == "" ] ; then postgresDb=$(ask "Enter postgres database name: ") endIfEmpty postgresDb "database" fi if [ "$postgresUser" == "" ] ; then postgresUser=$(ask "Enter postgres user name: ") endIfEmpty postgresUser "user name" fi if [ "$postgresPass" == "" ] ; then 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