2018-03-21 20:53:39 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2020-04-14 17:00:36 +00:00
|
|
|
source /scripts/env-data.sh
|
2018-03-21 20:53:39 +00:00
|
|
|
|
|
|
|
# This script will setup slave instance to use standby replication
|
|
|
|
|
|
|
|
# Adapted from https://github.com/DanielDent/docker-postgres-replication
|
|
|
|
# To set up replication
|
2023-02-01 04:42:32 +00:00
|
|
|
if [[ ${RUN_AS_ROOT} =~ [Ff][Aa][Ll][Ss][Ee] ]];then
|
2023-08-31 15:37:28 +00:00
|
|
|
function START_COMMAND() {
|
|
|
|
PARAM=$1
|
2023-09-04 19:56:45 +00:00
|
|
|
gosu "${USER_NAME}" bash -c "$1"
|
2023-08-31 15:37:28 +00:00
|
|
|
}
|
2023-02-01 04:42:32 +00:00
|
|
|
else
|
2023-08-31 15:37:28 +00:00
|
|
|
function START_COMMAND() {
|
|
|
|
PARAM=$1
|
|
|
|
su postgres -c "$1"
|
|
|
|
}
|
2023-02-01 04:42:32 +00:00
|
|
|
fi
|
2018-03-21 20:53:39 +00:00
|
|
|
|
2023-08-20 21:27:11 +00:00
|
|
|
create_dir "${WAL_ARCHIVE}"
|
2018-03-21 20:53:39 +00:00
|
|
|
|
2020-10-09 06:09:56 +00:00
|
|
|
if [[ "$WAL_LEVEL" == 'replica' && "${REPLICATION}" =~ [Tt][Rr][Uu][Ee] ]]; then
|
2020-10-01 15:55:53 +00:00
|
|
|
# No content yet - but this is a slave database
|
|
|
|
if [ -z "${REPLICATE_FROM}" ]; then
|
|
|
|
echo "You have not set REPLICATE_FROM variable."
|
|
|
|
echo "Specify the master address/hostname in REPLICATE_FROM and REPLICATE_PORT variable."
|
2023-02-01 04:42:32 +00:00
|
|
|
exit 1
|
2019-10-22 09:31:59 +00:00
|
|
|
fi
|
2023-08-31 15:37:28 +00:00
|
|
|
|
2019-11-25 12:47:29 +00:00
|
|
|
|
2023-09-04 19:56:45 +00:00
|
|
|
if [[ "${PROMOTE_MASTER}" =~ [Ff][Aa][Ll][Ss][Ee] ]];then
|
|
|
|
|
|
|
|
until START_COMMAND "/usr/lib/postgresql/${POSTGRES_MAJOR_VERSION}/bin/pg_isready -h ${REPLICATE_FROM} -p ${REPLICATE_PORT}"
|
|
|
|
do
|
|
|
|
echo -e "[Entrypoint] \e[1;31m Waiting for master to ping... \033[0m"
|
|
|
|
sleep 1s
|
|
|
|
done
|
|
|
|
if [[ "$DESTROY_DATABASE_ON_RESTART" =~ [Tt][Rr][Uu][Ee] ]]; then
|
|
|
|
echo -e "[Entrypoint] \e[1;31m Get initial database from master \033[0m"
|
|
|
|
configure_replication_permissions
|
|
|
|
if [ -f "${DATADIR}/backup_label.old" ]; then
|
|
|
|
echo -e "[Entrypoint] \e[1;31m PG Basebackup already exists so proceed to start the DB \033[0m"
|
|
|
|
else
|
|
|
|
streaming_replication
|
|
|
|
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
else
|
|
|
|
if [ ! -f "${DATADIR}/backup_label.old" ]; then
|
|
|
|
echo "Streaming replication hasn't been started yet"
|
|
|
|
exit 1
|
2020-10-01 15:55:53 +00:00
|
|
|
else
|
2023-09-04 19:56:45 +00:00
|
|
|
if [[ ${RUN_AS_ROOT} =~ [Ff][Aa][Ll][Ss][Ee] ]];then
|
|
|
|
chown -R "${USER_NAME}":"${DB_GROUP_NAME}" /var/run/postgresql
|
|
|
|
START_COMMAND "/etc/init.d/postgresql start ${POSTGRES_MAJOR_VERSION}"
|
2023-02-01 04:42:32 +00:00
|
|
|
|
2023-09-04 19:56:45 +00:00
|
|
|
else
|
|
|
|
START_COMMAND "/etc/init.d/postgresql start ${POSTGRES_MAJOR_VERSION}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
STANDBY_MODE=$(START_COMMAND "${DATA_DIR_CONTROL} $DATADIR" | grep "Database cluster state:")
|
|
|
|
if [[ "$STANDBY_MODE" == *"in archive recovery"* ]]; then
|
|
|
|
START_COMMAND "${NODE_PROMOTION} promote -D ${DATADIR}"
|
|
|
|
fi
|
|
|
|
echo -e "\e[32m [Entrypoint] Replicant has been promoted to master, please shut down \e[1;31m pg-master \033[0m"
|
|
|
|
kill_postgres
|
2020-10-01 15:55:53 +00:00
|
|
|
fi
|
2023-09-04 19:56:45 +00:00
|
|
|
|
2023-02-01 04:42:32 +00:00
|
|
|
fi
|
2020-10-09 06:09:56 +00:00
|
|
|
|
2023-09-04 19:56:45 +00:00
|
|
|
#end main if
|
2020-10-09 06:09:56 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|