Skip to content

Commit

Permalink
Add PITR Replication Strategy Support & Fix Standby Mode (#37)
Browse files Browse the repository at this point in the history
Signed-off-by: souravbiswassanto <[email protected]>
  • Loading branch information
souravbiswassanto authored Nov 8, 2024
1 parent 3261c18 commit 9a56ab6
Show file tree
Hide file tree
Showing 30 changed files with 104 additions and 2 deletions.
2 changes: 2 additions & 0 deletions role_scripts/10/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ mv /tmp/pg_hba.conf "$PGDATA/pg_hba.conf"
touch /tmp/postgresql.conf
if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/10/standby/ha_backup_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi
if [[ "$STREAMING" == "synchronous" ]]; then
# setup synchronous streaming replication
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/10/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf
echo "max_replication_slots = 90" >>/tmp/postgresql.conf
if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi
if [[ "$STREAMING" == "synchronous" ]]; then
# setup synchronous streaming replication
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/11/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ mv /tmp/pg_hba.conf "$PGDATA/pg_hba.conf"
touch /tmp/postgresql.conf
if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/11/standby/ha_backup_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi
if [[ "$STREAMING" == "synchronous" ]]; then
# setup synchronous streaming replication
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/11/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi
if [[ "$STREAMING" == "synchronous" ]]; then
# setup synchronous streaming replication
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/12/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ mv /tmp/pg_hba.conf "$PGDATA/pg_hba.conf"
touch /tmp/postgresql.conf
if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/12/standby/ha_backup_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/12/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/13/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ mv /tmp/pg_hba.conf "$PGDATA/pg_hba.conf"
touch /tmp/postgresql.conf
if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi
if [[ "$STREAMING" == "synchronous" ]]; then
# setup synchronous streaming replication
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/13/standby/ha_backup_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/13/standby/remote-replica.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/13/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/14/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ mv /tmp/pg_hba.conf "$PGDATA/pg_hba.conf"
touch /tmp/postgresql.conf
if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/14/standby/ha_backup_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/14/standby/remote-replica.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/14/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/15/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ mv /tmp/pg_hba.conf "$PGDATA/pg_hba.conf"
touch /tmp/postgresql.conf
if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/15/standby/ha_backup_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/15/standby/remote-replica.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/15/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
3 changes: 2 additions & 1 deletion role_scripts/16/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,9 @@ mv /tmp/pg_hba.conf "$PGDATA/pg_hba.conf"
touch /tmp/postgresql.conf
if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
# setup synchronous streaming replication
echo "synchronous_commit = remote_write" >>/tmp/postgresql.conf
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/16/standby/ha_backup_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/16/standby/remote-replica.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/16/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/9/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ mv /tmp/pg_hba.conf "$PGDATA/pg_hba.conf"
touch /tmp/postgresql.conf
if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi

if [[ "$STREAMING" == "synchronous" ]]; then
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/9/standby/ha_backup_job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi
if [[ "$STREAMING" == "synchronous" ]]; then
# setup synchronous streaming replication
Expand Down
2 changes: 2 additions & 0 deletions role_scripts/9/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
echo "hot_standby = on" >>/tmp/postgresql.conf
else
echo "hot_standby = off" >>/tmp/postgresql.conf
fi
if [[ "$STREAMING" == "synchronous" ]]; then
# setup synchronous streaming replication
Expand Down
32 changes: 32 additions & 0 deletions scripts/copy-data.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

ROOT_DIR="$ROOT_DIR"
TOTAL_DIR_TO_COPY="$TOTAL_DIR_TO_COPY"
DATA_DIR="$DATA_DIR"

if [ -z "$ROOT_DIR" ] || [ -z "$TOTAL_DIR_TO_COPY" ]; then
echo "ROOT_DIR and TOTAL_DIR_TO_COPY must be set."
exit 1
fi
Size1=$(du -s "$ROOT_DIR/$DATA_DIR" | cut -f1)
echo "DATA DIRECTORY SIZE: ", $Size1
for (( i = 1; i <= $TOTAL_DIR_TO_COPY; i++ ));do
if [[ -d "$ROOT_DIR$i/$DATA_DIR" ]];then
Size2=$(du -s "$ROOT_DIR$i/$DATA_DIR" | cut -f1)
echo $Size1, " ", $Size2
if [[ "$Size1" == "$Size2" ]];then
continue
fi
fi
# not deleting any data
# because the sole purpose of this script is to copy the data
# rm -rf "$ROOT_DIR$i"/*
cp -rvL "$ROOT_DIR/"* "$ROOT_DIR$i"/
if [[ $? -ne 0 ]]; then
echo "Error occurred while copying to $ROOT_DIR$i"
exit 1
fi
done

exit 0

17 changes: 16 additions & 1 deletion scripts/run.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
#!/usr/bin/env bash

RECOVERY_DONE_FILE="/var/pv/"$PITR_UNIX_TIME"_recovery.done"
PITR_RS=${PITR_REPLICATION_STRATEGY:-none}
STOP=false
# don't restart postgres on SIGTERM (eg, pod deleted)
# ref: https://opensource.com/article/20/6/bash-trap
trap \
"{ STOP=true; }" \
SIGINT SIGTERM EXIT

if [[ "$PITR_RESTORE" == "true" ]]; then
while true; do
sleep 2
echo "Point In Time Recovery In Progress. Waiting for $RECOVERY_DONE_FILE file"
if [[ -e "$RECOVERY_DONE_FILE" ]]; then
echo "$RECOVERY_DONE_FILE found."
break
fi
done
fi

if [[ -d $PGDATA && "$PITR_RS" == "fscopy" ]];then
chmod 0700 $PGDATA
fi
#going to change this with the check of process id
rm -f "$PGDATA"/postmaster.pid
echo "waiting for the role to be decided ..."
Expand Down

0 comments on commit 9a56ab6

Please sign in to comment.