@@ -23,23 +23,48 @@ if [ -d "$pgroot/data_bootstrap" ]; then
2323 output_info " pgBackRest: Folder data_bootstrap found and deleted"
2424fi
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
3048output_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+
3251output_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
4368archive_command = 'false'
4469archive_mode = 'on'
4570hba_file = '/tmp/pg_hba.restore.conf'
@@ -49,33 +74,34 @@ max_prepared_transactions = '${max_ptxn}'
4974max_worker_processes = '${max_work} '
5075unix_socket_directories = '/tmp'
5176EOF
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
76103output_info " pgBackRest: Restore complete"
77104output_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