Skip to content

Commit 01b8da3

Browse files
committed
find end lsn with backup_manifest in pg13+
1 parent 9b1d9b8 commit 01b8da3

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

addons/postgresql/dataprotection/pg-basebackup-restore.sh

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,43 @@ function remote_file_exists() {
1414
}
1515

1616
function save_backup_end_lsn() {
17-
local wal_dir="${DATA_DIR}/pg_wal"
1817
local backup_end_lsn=""
19-
for wal in $(ls -t "${wal_dir}" 2>/dev/null | grep -E '^[0-9A-F]{24}$'); do
20-
local wal_path="${wal_dir}/${wal}"
21-
# pg_waldump may exit non-zero when it hits the end of a partial WAL file,
22-
# which is normal for the last WAL segment in a backup.
23-
# so we check whether any valid records were parsed instead of relying on exit code.
24-
local last_record=$(pg_waldump "${wal_path}" 2>/dev/null | tail -1 || true)
25-
if [ -n "${last_record}" ]; then
26-
backup_end_lsn=$(echo "${last_record}" | awk '{print $10}' | tr -d ',')
27-
echo "found backup_end_lsn: ${backup_end_lsn} in wal file: ${wal_path}" >> "${DATA_DIR}/.backup_log"
28-
break
29-
else
30-
echo "skipping invalid wal file: ${wal_path}" >> "${DATA_DIR}/.backup_log"
18+
19+
# PG13+: backup_manifest contains the exact End-LSN in JSON format.
20+
local manifest="${DATA_DIR}/backup_manifest"
21+
if [[ -f "${manifest}" ]]; then
22+
echo "find with backup_manifest" >> "${DATA_DIR}/.backup_log"
23+
backup_end_lsn=$(grep -o '"End-LSN": *"[^"]*"' "${manifest}" | awk -F'"' '{print $4}')
24+
if [[ -n "${backup_end_lsn}" ]]; then
25+
echo "found backup_end_lsn: ${backup_end_lsn} from backup_manifest" >> "${DATA_DIR}/.backup_log"
3126
fi
32-
done
33-
if [ -n "${backup_end_lsn}" ]; then
27+
fi
28+
29+
# Fallback for PG12 and older: scan WAL files with pg_waldump.
30+
if [[ -z "${backup_end_lsn}" ]]; then
31+
local wal_dir="${DATA_DIR}/pg_wal"
32+
echo "find with pg_wal" >> "${DATA_DIR}/.backup_log"
33+
for wal in $(ls -t "${wal_dir}" 2>/dev/null | grep -E '^[0-9A-F]{24}$'); do
34+
local wal_path="${wal_dir}/${wal}"
35+
# pg_waldump may exit non-zero when it hits the end of a partial WAL file,
36+
# which is normal for the last WAL segment in a backup.
37+
# so we check whether any valid records were parsed instead of relying on exit code.
38+
local last_record
39+
last_record=$(pg_waldump "${wal_path}" 2>/dev/null | tail -1 || true)
40+
if [[ -n "${last_record}" ]]; then
41+
backup_end_lsn=$(echo "${last_record}" | awk '{print $10}' | tr -d ',')
42+
echo "found backup_end_lsn: ${backup_end_lsn} in wal file: ${wal_path}" >> "${DATA_DIR}/.backup_log"
43+
break
44+
else
45+
echo "skipping invalid wal file: ${wal_path}" >> "${DATA_DIR}/.backup_log"
46+
fi
47+
done
48+
fi
49+
50+
if [[ -n "${backup_end_lsn}" ]]; then
3451
echo "${backup_end_lsn}" > "${DATA_DIR}/.backup_end_lsn"
3552
else
36-
echo "warning: could not extract backup_end_lsn from any wal file" >> "${DATA_DIR}/.backup_log"
53+
echo "warning: could not extract backup_end_lsn" >> "${DATA_DIR}/.backup_log"
3754
fi
3855
}
3956

0 commit comments

Comments
 (0)