Skip to content

Commit fb2061f

Browse files
committed
Update container to match fixed operator implementation
Removes capability to run a backup as a part of restore. Instead of multiple variables, just uses one MODE variable to switch behavior between a backup job, a restore init container and repo host. Restore mode now stores last completed restore on the PVC to avoid redoing when it was already successful. Restore only runs recovery on pod-0, replicas will use Patroni to catch up to the primary.
1 parent 13d3f01 commit fb2061f

3 files changed

Lines changed: 79 additions & 72 deletions

File tree

launcher/pgbackrest/launch.sh

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,32 @@ source "${PGBACKREST_PATH}/bin/shell_lib.sh"
77

88
output_info "Start pgBackRest-PreCondition-Check"
99

10-
if [ "$USE_PGBACKREST" == true ]; then
11-
output_info "Check if RepoHost-Server needs to start"
12-
if [ "$REPO_HOST" == true ]; then
13-
if [[ -n $RESTORE_ENABLE ]] || [[ -n $RESTORE_BASEBACKUP ]]; then
14-
output_info "pgBackRest: Starting temporary Repo-Host"
15-
pgbackrest server &
16-
pid=$!
17-
trap 'kill $pid' EXIT
18-
else
10+
if [[ "$USE_PGBACKREST" == true ]]; then
11+
case $MODE in
12+
repo)
1913
output_info "pgBackRest: Starting Repo-Host"
20-
/opt/pgbackrest/bin/repo-host/start.sh &
14+
"${PGBACKREST_PATH}/bin/repo-host/start.sh" &
2115
pgbackrest server
22-
fi
23-
else
24-
output_info "RepoHost-Server not needed. Skip Step"
25-
fi
26-
27-
if [ "$USE_PGBACKREST" == true ] && [ "$PGBACKREST_MODE" == "backup" ]; then
28-
output_info "pgBackRest: Backup-Job found"
29-
source "${PGBACKREST_PATH}/bin/backup/start.sh"
30-
output_success "pgBackRest: Backup-Job completed"
31-
else
32-
#For Restore with pgBackrest
33-
if [ "$RESTORE_ENABLE" == "true" ]; then
16+
;;
17+
restore)
3418
output_info "pgBackRest: Restore-Job found"
19+
if [ "$RESTORE_ENABLE" != "true" ]; then
20+
output_info "pgBackRest: restore not requested, skipping."
21+
exit 0
22+
fi
23+
3524
source "${PGBACKREST_PATH}/bin/restore/start.sh"
3625
output_success "Restore-Job completed"
37-
else
38-
output_info "Restore not defined - Skip Restore-Step"
39-
if [ "$RESTORE_BASEBACKUP" == "false" ]; then
40-
output_info "pgBackRest: Backup-Job found"
41-
export SELECTOR="cluster-name=${SCOPE},spilo-role=master"
42-
export COMMAND_OPTS="--type=full --stanza=db --repo=1"
43-
source "${PGBACKREST_PATH}/bin/backup/start.sh"
44-
output_info "pgBackRest: Update Restore-Configmap"
45-
configmap="${SCOPE}-pgbackrest-restore"
46-
kubectl get cm $configmap -o yaml | \
47-
sed -e 's|restore_basebackup: "false"|restore_basebackup: "true"|' | \
48-
kubectl apply -f -
49-
output_success "pgBackRest: Backup-Job completed"
50-
else
51-
output_info "Basebackup not defined - Skip create basebackup"
52-
fi
53-
fi
54-
fi
26+
;;
27+
backup)
28+
output_info "pgBackRest: Backup-Job found"
29+
source "${PGBACKREST_PATH}/bin/backup/start.sh"
30+
output_success "pgBackRest: Backup-Job completed"
31+
;;
32+
*)
33+
output_error "Unknown MODE: $MODE"
34+
exit 1
35+
esac
5536
else
5637
output_info "pgBackRest not used. Skip Container"
5738
fi

scripts/pgbackrest/backup/start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ else
2525
output_info "pgBackRest: Detect Primary-Pod $hostName"
2626
while true ; do
2727
unset error
28-
kubectl exec $hostName -c postgres -- /bin/bash -c "pgbackrest backup ${COMMAND_OPTS}" || { output_error "pgBackRest: Create basebackup failed"; error=true; }
28+
kubectl exec $hostName -c ${CONTAINER} -- /bin/bash -c "pgbackrest backup ${COMMAND_OPTS}" || { output_error "pgBackRest: Create basebackup failed"; error=true; }
2929
if [ "$error" = true ]; then
3030
if [ "$count" == 3 ]; then
3131
output_error "pgBackRest: Basebackup could not be created. Abort init-script";

scripts/pgbackrest/restore/start.sh

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,48 @@ if [ -d "$pgroot/data_bootstrap" ]; then
2323
output_info "pgBackRest: Folder data_bootstrap found and deleted"
2424
fi
2525

26+
if [ -f "${PGROOT}/RESTORE_ID" ]; then
27+
prev_restore="$(< "$PGROOT/RESTORE_ID")"
28+
if [ "$prev_restore" = "$RESTORE_ID" ]; then
29+
output_info "pgBackRest: Found a completion marker for restore \"$RESTORE_ID\", skipping restore"
30+
exit 0
31+
else
32+
output_info "pgBackRest: Cleaning up completion marker for restore \"$prev_restore\""
33+
rm "$PGROOT/RESTORE_ID"
34+
fi
35+
fi
36+
2637
# Add Path
27-
restore_command="$restore_opts --stanza=db --pg1-path=$pgdata --delta --link-map=pg_wal=$pgroot/data_wal --target-action=promote"
38+
POD_INDEX="${HOSTNAME##*-}"
39+
if [ "$POD_INDEX" -eq 0 ]; then
40+
action=" --target-action=promote"
41+
else
42+
restore_opts="${restore_opts//--type=[^ ]+/}"
43+
action=" --type=none"
44+
fi
45+
restore_command="$restore_opts --stanza=db --pg1-path=$pgdata --delta $action"
2846

2947
# Do Restore
3048
output_info "pgBackRest: start restore: Defined options: $restore_command"
31-
bash -xc "pgbackrest restore ${restore_command}" || { output_error "pgBackRest: Restore failed"; exit 0; } #<< /home/postgres/pgdata/pgbackrest/log/restore-pod.log
49+
bash -xc "pgbackrest restore ${restore_command}" || { output_error "pgBackRest: Restore failed"; exit 1; } #<< /home/postgres/pgdata/pgbackrest/log/restore-pod.log
50+
3251
output_info "Defined Data-DIR: $pgdata"
33-
# Check Restore
34-
until [ "${recovery=}" = 'f' ]; do
35-
if [ -z "${recovery}" ]; then
36-
control=$(pg_controldata)
37-
read -r max_conn <<< "${control##*max_connections setting:}"
38-
read -r max_lock <<< "${control##*max_locks_per_xact setting:}"
39-
read -r max_ptxn <<< "${control##*max_prepared_xacts setting:}"
40-
read -r max_work <<< "${control##*max_worker_processes setting:}"
41-
echo > /tmp/pg_hba.restore.conf 'local all "postgres" peer'
42-
cat > /tmp/postgres.restore.conf << EOF
52+
53+
actual_version="$(< "$pgdata/PG_VERSION")"
54+
output_info "Restored version: $actual_version"
55+
bin_dir="/usr/pgsql-${actual_version}/bin"
56+
57+
if [ "$POD_INDEX" -eq 0 ]; then
58+
# Check Restore
59+
until [ "${recovery=}" = 'f' ]; do
60+
if [ -z "${recovery}" ]; then
61+
control=$(${bin_dir}/pg_controldata)
62+
read -r max_conn <<< "${control##*max_connections setting:}"
63+
read -r max_lock <<< "${control##*max_locks_per_xact setting:}"
64+
read -r max_ptxn <<< "${control##*max_prepared_xacts setting:}"
65+
read -r max_work <<< "${control##*max_worker_processes setting:}"
66+
echo > /tmp/pg_hba.restore.conf 'local all "postgres" peer'
67+
cat > /tmp/postgres.restore.conf << EOF
4368
archive_command = 'false'
4469
archive_mode = 'on'
4570
hba_file = '/tmp/pg_hba.restore.conf'
@@ -49,33 +74,34 @@ max_prepared_transactions = '${max_ptxn}'
4974
max_worker_processes = '${max_work}'
5075
unix_socket_directories = '/tmp'
5176
EOF
52-
if [ "$(< "$pgdata/PG_VERSION")" -ge 12 ]; then
53-
read -r max_wals <<< "${control##*max_wal_senders setting:}"
77+
if [ "$actual_version" -ge 12 ]; then
78+
read -r max_wals <<< "${control##*max_wal_senders setting:}"
5479

55-
echo >> /tmp/postgres.restore.conf "max_wal_senders = '${max_wals}'"
80+
echo >> /tmp/postgres.restore.conf "max_wal_senders = '${max_wals}'"
5681

82+
fi
83+
${bin_dir}/pg_ctl start --silent --timeout=31536000 --wait --options='--config-file=/tmp/postgres.restore.conf'
5784
fi
58-
pg_ctl start --silent --timeout=31536000 --wait --options='--config-file=/tmp/postgres.restore.conf'
59-
fi
6085

61-
recovery=$(psql -Atc "SELECT CASE
62-
WHEN NOT pg_catalog.pg_is_in_recovery() THEN false
63-
WHEN NOT pg_catalog.pg_is_wal_replay_paused() THEN true
64-
ELSE pg_catalog.pg_wal_replay_resume()::text = ''
65-
END recovery" && sleep 1) || true
66-
done
86+
recovery=$(${bin_dir}/psql -Atc "SELECT CASE
87+
WHEN NOT pg_catalog.pg_is_in_recovery() THEN false
88+
WHEN NOT pg_catalog.pg_is_wal_replay_paused() THEN true
89+
ELSE pg_catalog.pg_wal_replay_resume()::text = ''
90+
END recovery" && sleep 1) || true
91+
done
92+
93+
${bin_dir}/pg_ctl stop --silent --wait --timeout=31536000
94+
touch $pgdata/promote_after_restore.signal
95+
else
96+
output_info "Replica pod, letting Patroni take care of finalizing recovery"
97+
fi
6798

68-
pg_ctl stop --silent --wait --timeout=31536000
6999

70-
output_info "pgBackRest: Update Restore-Configmap"
71-
configmap="${SCOPE}-pgbackrest-restore"
72-
kubectl get cm $configmap -o yaml | \
73-
sed -e 's|restore_enable: "true"|restore_enable: "false"|' | \
74-
kubectl apply -f -
100+
output_info "pgBackRest: Marking PVC restored as \"$RESTORE_ID\""
101+
echo -n "$RESTORE_ID" > "$PGROOT/RESTORE_ID"
75102

76103
output_info "pgBackRest: Restore complete"
77104
output_info "pgBackRest: Create Initial-Sign for Database-Pod"
78-
touch $pgdata/promote_after_restore.signal
79105

80106
#echo "$pgdata" >> /home/postgres/pgdata/pgbackrest/test.txt
81-
#echo "$restore_opts" >> /home/postgres/pgdata/pgbackrest/test.txt
107+
#echo "$restore_opts" >> /home/postgres/pgdata/pgbackrest/test.txt

0 commit comments

Comments
 (0)