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
|
|
|
|
|
2020-10-01 15:55:53 +00:00
|
|
|
create_dir ${WAL_ARCHIVE}
|
|
|
|
chown -R postgres:postgres ${DATADIR} ${WAL_ARCHIVE}
|
|
|
|
chmod -R 750 ${DATADIR} ${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."
|
2019-10-22 09:31:59 +00:00
|
|
|
fi
|
2019-11-25 12:47:29 +00:00
|
|
|
|
2020-10-01 15:55:53 +00:00
|
|
|
until su - postgres -c "pg_isready -h ${REPLICATE_FROM} -p ${REPLICATE_PORT}"
|
|
|
|
do
|
|
|
|
echo "Waiting for master to ping..."
|
|
|
|
sleep 1s
|
|
|
|
done
|
|
|
|
if [[ "$DESTROY_DATABASE_ON_RESTART" =~ [Tt][Rr][Uu][Ee] ]]; then
|
|
|
|
echo "Get initial database from master"
|
|
|
|
configure_replication_permissions
|
|
|
|
if [ -f "${DATADIR}/backup_label.old" ]; then
|
|
|
|
echo "PG Basebackup already exists so proceed to start the DB"
|
|
|
|
else
|
|
|
|
streaming_replication
|
|
|
|
fi
|
|
|
|
fi
|
2020-10-09 06:09:56 +00:00
|
|
|
# Promote to master if desired
|
2019-01-30 19:06:28 +00:00
|
|
|
if [[ ! -z "${PROMOTE_MASTER}" ]]; then
|
2022-04-29 13:52:14 +00:00
|
|
|
su - postgres -c "${NODE_PROMOTION} promote -D ${DATADIR}"
|
2018-03-21 20:53:39 +00:00
|
|
|
fi
|
2020-10-09 06:09:56 +00:00
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|