Skip to content

Commit

Permalink
add archive command (#16)
Browse files Browse the repository at this point in the history
Signed-off-by: Mehedi Hasan <[email protected]>
Co-authored-by: Emon46 [email protected]
Co-authored-by: [email protected]
  • Loading branch information
rakibulhossain committed Nov 29, 2023
1 parent 74232e9 commit 592781a
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 19 deletions.
23 changes: 17 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
FROM alpine
FROM golang:alpine AS builder

ARG TARGETOS
ARG TARGETARCH

RUN set -x \
&& apk add --update ca-certificates curl
ENV WALG_VERSION=kubedb-v2023.11.30

RUN curl -fsSL -o tini https://github.com/kubedb/tini/releases/download/v0.20.0/tini-static-${TARGETARCH} \
&& chmod +x tini
ENV _build_deps="wget cmake git build-base bash curl xz-dev lzo-dev"

RUN set -ex \
&& apk add --no-cache $_build_deps

RUN set -ex \
&& cd / \
&& curl -fsSL -o tini https://github.com/kubedb/tini/releases/download/v0.20.0/tini-static-${TARGETARCH} \
&& chmod +x tini

RUN set -x \
&& git clone https://github.com/kubedb/wal-g.git \
&& cd wal-g \
&& git checkout kubedb-v2023.11.30\
&& CGO_ENABLED=0 go build -v -o /wal-g ./main/pg/main.go

FROM alpine

Expand All @@ -19,7 +29,8 @@ RUN apk add --no-cache bash

COPY scripts /tmp/scripts
COPY init_scripts /init_scripts
COPY --from=0 /tini /tmp/scripts/tini
COPY --from=builder /tini /tmp/scripts/tini
COPY --from=builder /wal-g /tmp/scripts/wal-g
COPY role_scripts /tmp/role_scripts

ENTRYPOINT ["/init_scripts/run.sh"]
8 changes: 7 additions & 1 deletion role_scripts/11/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf

echo "wal_log_hints = on" >>/tmp/postgresql.conf
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf

if [[ "${WAL_BACKUP_TYPE:-0}" == "WALG" ]]; then
echo "archive_command = 'cp %p /var/pv/wal_archive/%f'" >>/tmp/postgresql.conf
else
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [[ "${SSL:-0}" == "ON" ]]; then
Expand Down
7 changes: 6 additions & 1 deletion role_scripts/11/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ echo "wal_keep_segments = 1024" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf

echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
if [[ "${WAL_BACKUP_TYPE:-0}" == "WALG" ]]; then
echo "archive_command = 'cp %p /var/pv/wal_archive/%f'" >>/tmp/postgresql.conf
else
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
Expand Down
7 changes: 6 additions & 1 deletion role_scripts/12/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ echo "wal_log_hints = on" >>/tmp/postgresql.conf

# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
if [[ "${WAL_BACKUP_TYPE:-0}" == "WALG" ]]; then
echo "archive_command = 'cp %p /var/pv/wal_archive/%f'" >>/tmp/postgresql.conf
else
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [[ "${SSL:-0}" == "ON" ]]; then
Expand Down
7 changes: 6 additions & 1 deletion role_scripts/12/standby/warm_stanby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ echo "wal_log_hints = on" >>/tmp/postgresql.conf

# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
if [[ "${WAL_BACKUP_TYPE:-0}" == "WALG" ]]; then
echo "archive_command = 'cp %p /var/pv/wal_archive/%f'" >>/tmp/postgresql.conf
else
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

echo "hot_standby = off" >>/tmp/postgresql.conf
Expand Down
7 changes: 6 additions & 1 deletion role_scripts/13/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ echo "wal_log_hints = on" >>/tmp/postgresql.conf

# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
if [[ "${WAL_BACKUP_TYPE:-0}" == "WALG" ]]; then
echo "archive_command = 'cp %p /var/pv/wal_archive/%f'" >>/tmp/postgresql.conf
else
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [[ "${SSL:-0}" == "ON" ]]; then
Expand Down
7 changes: 6 additions & 1 deletion role_scripts/13/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ echo "wal_log_hints = on" >>/tmp/postgresql.conf

# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
if [[ "${WAL_BACKUP_TYPE:-0}" == "WALG" ]]; then
echo "archive_command = 'cp %p /var/pv/wal_archive/%f'" >>/tmp/postgresql.conf
else
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

if [ "$STANDBY" == "hot" ]; then
Expand Down
7 changes: 5 additions & 2 deletions role_scripts/14/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ echo "wal_log_hints = on" >>/tmp/postgresql.conf

# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
if [[ "${WAL_BACKUP_TYPE:-0}" == "WALG" ]]; then
echo "archive_command = 'cp %p /var/pv/wal_archive/%f'" >>/tmp/postgresql.conf
else
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf


if [[ "${SSL:-0}" == "ON" ]]; then
echo "ssl =on" >>/tmp/postgresql.conf
echo "ssl_cert_file ='/tls/certs/server/server.crt'" >>/tmp/postgresql.conf
Expand Down
6 changes: 5 additions & 1 deletion role_scripts/14/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ echo "wal_log_hints = on" >>/tmp/postgresql.conf

# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
if [[ "${WAL_BACKUP_TYPE:-0}" == "WALG" ]]; then
echo "archive_command = 'cp %p /var/pv/wal_archive/%f'" >>/tmp/postgresql.conf
else
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

Expand Down
6 changes: 5 additions & 1 deletion role_scripts/15/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ echo "wal_log_hints = on" >>/tmp/postgresql.conf

# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
if [[ "${WAL_BACKUP_TYPE:-0}" == "WALG" ]]; then
echo "archive_command = 'cp %p /var/pv/wal_archive/%f'" >>/tmp/postgresql.conf
else
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

Expand Down
6 changes: 5 additions & 1 deletion role_scripts/15/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ echo "wal_log_hints = on" >>/tmp/postgresql.conf

# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
if [[ "${WAL_BACKUP_TYPE:-0}" == "WALG" ]]; then
echo "archive_command = 'cp %p /var/pv/wal_archive/%f'" >>/tmp/postgresql.conf
else
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

Expand Down
6 changes: 5 additions & 1 deletion role_scripts/16/primary/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ echo "wal_log_hints = on" >>/tmp/postgresql.conf

# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
if [[ "${WAL_BACKUP_TYPE:-0}" == "WALG" ]]; then
echo "archive_command = 'cp %p /var/pv/wal_archive/%f'" >>/tmp/postgresql.conf
else
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

Expand Down
6 changes: 5 additions & 1 deletion role_scripts/16/standby/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ echo "wal_log_hints = on" >>/tmp/postgresql.conf

# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
if [[ "${WAL_BACKUP_TYPE:-0}" == "WALG" ]]; then
echo "archive_command = 'cp %p /var/pv/wal_archive/%f'" >>/tmp/postgresql.conf
else
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf
fi

echo "shared_preload_libraries = 'pg_stat_statements'" >>/tmp/postgresql.conf

Expand Down
87 changes: 87 additions & 0 deletions scripts/restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/bash

mkdir -p $PGDATA
chmod 0700 "$PGDATA"

if [[ "${WALG_BASE_BACKUP_NAME:-0}" != "0" ]]; then
echo "starting to restore from basebackup $WALG_BASE_BACKUP_NAME ..."
wal-g backup-fetch $PGDATA $WALG_BASE_BACKUP_NAME
fi

## ****************** Recovery config 11 **************************
#touch /tmp/recovery.conf
#echo "restore_command = 'wal-g wal-fetch %f %p'" >>/tmp/recovery.conf
#echo "standby_mode = on" >>/tmp/recovery.conf
#echo "trigger_file = '/run_scripts/tmp/pg-failover-trigger'" >>/tmp/recovery.conf # [ name whose presence ends recovery]
##echo "recovery_target_timeline = 'latest'" >>/tmp/recovery.conf
##echo "recovery_target = 'immediate'" >>/tmp/recovery.conf
#echo "recovery_target_action = 'promote'" >>/tmp/recovery.conf
#mv /tmp/recovery.conf "$PGDATA/recovery.conf"
#
## setup postgresql.conf
#touch /tmp/postgresql.conf
#echo "wal_level = replica" >>/tmp/postgresql.conf
#echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS
#
#echo "wal_keep_segments = 64" >>/tmp/postgresql.conf
#
#echo "wal_log_hints = on" >>/tmp/postgresql.conf

# ****************** Recovery config 12, 13, 14 **************************
touch $PGDATA/recovery.signal

# setup postgresql.conf
touch /tmp/postgresql.conf
echo "restore_command = 'wal-g wal-fetch %f %p'" >>/tmp/postgresql.conf
#echo "recovery_target_timeline = 'latest'" >>/tmp/postgresql.conf
if [[ "${PITR_TIME:-0}" != "latest" ]]; then
echo "recovery_target_time = '$PITR_TIME'" >>/tmp/postgresql.conf
else
echo "recovery_target_timeline = 'latest'" >>/tmp/postgresql.conf
fi
echo "recovery_target_action = 'promote'" >>/tmp/postgresql.conf
echo "wal_level = replica" >>/tmp/postgresql.conf
echo "max_wal_senders = 90" >>/tmp/postgresql.conf # default is 10. value must be less than max_connections minus superuser_reserved_connections. ref: https://www.postgresql.org/docs/11/runtime-config-replication.html#GUC-MAX-WAL-SENDERS

echo "wal_keep_size = 64" >>/tmp/postgresql.conf
echo "hot_standby = on" >>/tmp/postgresql.conf
echo "wal_log_hints = on" >>/tmp/postgresql.conf

# ****************** Recovery config 12 **************************
# we are not doing any archiving by default but it's better to have this config in our postgresql.conf file in case of customization.
echo "archive_mode = always" >>/tmp/postgresql.conf
echo "archive_command = '/bin/true'" >>/tmp/postgresql.conf

cat /run_scripts/role/postgresql.conf >>/tmp/postgresql.conf
mv /tmp/postgresql.conf "$PGDATA/postgresql.conf"

# setup pg_hba.conf for initial start. this one is just for initialization
touch /tmp/pg_hba.conf
{ echo '#TYPE DATABASE USER ADDRESS METHOD'; } >>tmp/pg_hba.conf
{ echo '# "local" is for Unix domain socket connections only'; } >>tmp/pg_hba.conf
{ echo 'local all all trust'; } >>tmp/pg_hba.conf
{ echo '# IPv4 local connections:'; } >>tmp/pg_hba.conf
{ echo 'host all all 127.0.0.1/32 trust'; } >>tmp/pg_hba.conf
mv /tmp/pg_hba.conf "$PGDATA/pg_hba.conf"

# start postgres
pg_ctl -D "$PGDATA" -w start &
sleep 10
# | [[ ! -e /var/pv/data/restore.done]]
while [[ -e /var/pv/data/recovery.signal && -e /var/pv/data/postmaster.pid ]]; do
echo "restoring..."
cluster_state=$(pg_controldata | grep "Database cluster state" | awk '{print $4, $5}')
if [[ $cluster_state == "in production" ]]; then
echo "database succefully recovered...."
rm -rf /var/pv/data/recovery.signal
fi
sleep 1
done

sleep 10

if [[ ! -e /var/pv/data/recovery.signal ]]; then
exit 0
else
exit 1
fi

0 comments on commit 592781a

Please sign in to comment.