|
1 | 1 | #!/bin/bash |
2 | 2 | set -e |
| 3 | +set -o pipefail |
3 | 4 | backup_base_path="$(dirname $DP_BACKUP_BASE_PATH)/wal-g/" |
4 | 5 |
|
5 | 6 | function config_wal_g_for_fetch_wal_log() { |
@@ -86,19 +87,42 @@ if [[ "${IS_REPLICA}" == "true" ]]; then |
86 | 87 | exit 1 |
87 | 88 | fi |
88 | 89 |
|
89 | | -# Primary restore: Restore from successful backup (.old directory exists) |
90 | | -if [[ -d "${DATA_DIR}.old" ]]; then |
91 | | - echo "Restoring data from ${DATA_DIR}.old..." |
92 | | - mkdir -p "${DATA_DIR}" |
93 | | - mv -f ${DATA_DIR}.old/* ${DATA_DIR}/ |
94 | | - # Durability barrier BEFORE removing the signal: the signal is the retry |
95 | | - # marker for this hook, so it must be the FINAL commit action. If sync |
96 | | - # (or the mv) fails, the signal survives and the bootstrap re-runs the |
97 | | - # hook instead of losing the restore marker with a half-durable DATA_DIR. |
| 90 | +HANDOFF_MARKER=".kb_walg_handoff" |
| 91 | +
|
| 92 | +function finalize_handoff() { |
| 93 | + local source_dir="${DATA_DIR}.old" |
| 94 | + local marker_path="${DATA_DIR}/${HANDOFF_MARKER}" |
| 95 | +
|
| 96 | + if [[ -d "${source_dir}" ]]; then |
| 97 | + if [[ -f "${marker_path}" ]]; then |
| 98 | + if [[ "$(cat "${marker_path}")" != "normal" ]] \ |
| 99 | + || [[ -n "$(find "${source_dir}" -mindepth 1 -maxdepth 1 -print -quit)" ]]; then |
| 100 | + echo "ERROR: WAL-G archive restore handoff state is inconsistent" >&2 |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + rmdir "${source_dir}" |
| 104 | + else |
| 105 | + if [[ -d "${DATA_DIR}" ]] \ |
| 106 | + && [[ -n "$(find "${DATA_DIR}" -mindepth 1 -maxdepth 1 -print -quit)" ]]; then |
| 107 | + echo "ERROR: PostgreSQL data directory is not empty before WAL-G archive handoff" >&2 |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | + printf 'normal\n' > "${source_dir}/${HANDOFF_MARKER}" |
| 111 | + rm -rf "${DATA_DIR}" |
| 112 | + mv "${source_dir}" "${DATA_DIR}" |
| 113 | + fi |
| 114 | + elif [[ ! -f "${marker_path}" ]] || [[ "$(cat "${marker_path}")" != "normal" ]]; then |
| 115 | + echo "ERROR: WAL-G archive restore handoff source is missing" >&2 |
| 116 | + exit 1 |
| 117 | + fi |
| 118 | +
|
98 | 119 | sync |
99 | | - rm -f ${RESTORE_SCRIPT_DIR}/kb_restore.signal |
100 | | - echo "Data restore completed successfully" |
101 | | -fi |
| 120 | + rm -f "${RESTORE_SCRIPT_DIR}/kb_restore.signal" |
| 121 | +} |
| 122 | +
|
| 123 | +echo "Restoring data from ${DATA_DIR}.old..." |
| 124 | +finalize_handoff |
| 125 | +echo "Data restore completed successfully" |
102 | 126 | EOF |
103 | 127 |
|
104 | 128 | # Replace variables in the generated script |
|
0 commit comments