From a4b4e595b98737aa5f7938e343fb745955949512 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Tue, 2 Dec 2025 13:24:54 +0900 Subject: [PATCH] Update pgbackrest wrapper to support multiple repositories in the selection --- ReadMe.md | 7 +++- bin/pgbackrest_backup.sh | 77 ++++++++++++++++++++---------------- config/pgbackrest.sample.cfg | 2 +- config/stanza.sample.cfg | 3 +- 4 files changed, 52 insertions(+), 37 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 1c729ab..2f877b9 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -25,7 +25,7 @@ This file holds the sudo_user for calling the commands ```ini [PgBackRest] -sudo_user=pgbackrest +sudo_user=postgres ``` ### `config/stanza.cfg` @@ -34,8 +34,11 @@ Example file is `stanza.sample.cfg` This files holds the stanzas to backup +If the stanza has several repo settings as in "repo1-...", "repo2-..." then the repo to target can be listed in here by ":repo number" + ```ini [stanzas] stanza_a -stanza_b +stanza_b:1 +stanza_b:2 ``` diff --git a/bin/pgbackrest_backup.sh b/bin/pgbackrest_backup.sh index 8598b5b..4a48aaf 100755 --- a/bin/pgbackrest_backup.sh +++ b/bin/pgbackrest_backup.sh @@ -113,6 +113,12 @@ while getopts ":b:s:lt" opt; do esac; done; +## check if OVERIDE_STANZA has a repo target flag set, get the name only for checks +OVERRIDE_STANZA_NAME=""; +if [ -n "${OVERRIDE_STANZA}" ]; then + OVERRIDE_STANZA_NAME=$(echo "${OVERRIDE_STANZA}" | cut -d ":" -f 1); +fi; + ## SCRIPT if [ $TEST -eq 1 ]; then @@ -121,9 +127,6 @@ fi; if [ $LIST_STANZA -eq 1 ]; then echo "+ Stanza List:"; - for stanza in ${stanza_list}; do - echo "${stanza}"; - done; while read -r stanza; do # skip empty [ -z "${stanza}" ] && continue; @@ -131,15 +134,25 @@ if [ $LIST_STANZA -eq 1 ]; then [[ "${stanza}" =~ ${REGEX_COMMENT} ]] && continue; # skip the ini header blocks [[ "${stanza}" =~ ${INI_BLOCK} ]] && continue; + # split the stanz int stanza and stanza repo list + stanza_name=$(echo "${stanza}" | cut -d ":" -f 1); + stanza_repo=$(echo "${stanza}" | awk -F':' '{print (NF>1) ? $2 : ""}'); # if override stanza matching - if [ "${stanza}" = "${OVERRIDE_STANZA}" ]; then - echo "[*] ${stanza}"; + if [ "${stanza_name}" = "${OVERRIDE_STANZA_NAME}" ]; then + OVERRIDE_STANZA_REPO=$(echo "${OVERRIDE_STANZA}" | awk -F':' '{print (NF>1) ? $2 : ""}'); + if [ -n "${OVERRIDE_STANZA_REPO}" ] && [ -n "${stanza_repo}" ] && [ "${stanza_repo}" = "${OVERRIDE_STANZA_REPO}" ]; then + echo "[*] ${stanza}"; + elif [ -z "${OVERRIDE_STANZA_REPO}" ]; then + echo "[*] ${stanza}"; + else + echo "[ ] ${stanza}"; + fi; else echo "[ ] ${stanza}"; fi; done <<< "$(cat "${stanza_file}")"; echo ""; - echo "Entry marked with '*' is set as override stanza, and will be the only one backed up"; + echo "Entry marked with '*' is set as override stanza, and will be the only ones backed up"; echo ""; exit; fi; @@ -155,29 +168,6 @@ if [ "${BACKUP_TYPE}" != "full" ] && [ "${BACKUP_TYPE}" != "diff" ]; then fi; echo "* PgBackrest Backup run type '${BACKUP_TYPE}' on $(date +'%F')"; -SET_OVERRIDE_STANZA=0; -# if we have override single host -if [ -n "${OVERRIDE_STANZA}" ]; then - echo "+ Set override stanza too: ${OVERRIDE_STANZA}"; - while read -r stanza; do - # skip empty - [ -z "${stanza}" ] && continue; - # skip starting with # - [[ "${stanza}" =~ ${REGEX_COMMENT} ]] && continue; - # skip the ini header blocks - [[ "${stanza}" =~ ${INI_BLOCK} ]] && continue; - # if override stanza matching - if [ "${stanza}" = "${OVERRIDE_STANZA}" ]; then - stanza_list="${OVERRIDE_STANZA}"; - SET_OVERRIDE_STANZA=1; - break; - fi; - done <<< "$(cat "${stanza_file}")"; - if [ $SET_OVERRIDE_STANZA = 0 ]; then - echo "[!!!] Failed to set override stanza: ${OVERRIDE_STANZA}"; - exit; - fi; -fi; # run backup while read -r stanza; do # skip empty @@ -186,23 +176,44 @@ while read -r stanza; do [[ "${stanza}" =~ ${REGEX_COMMENT} ]] && continue; # skip the ini header blocks [[ "${stanza}" =~ ${INI_BLOCK} ]] && continue; + # split into the name and repo list + stanza_name=$(echo "${stanza}" | cut -d ":" -f 1); + stanza_repo=$(echo "${stanza}" | awk -F':' '{print (NF>1) ? $2 : ""}'); # override stanza check if [ -n "${OVERRIDE_STANZA}" ]; then - if [ "${stanza}" != "${OVERRIDE_STANZA}" ]; then + # skip if not matching name + if [ "${stanza_name}" != "${OVERRIDE_STANZA_NAME}" ]; then continue fi; + # if we have repo set, check if repo is matching + OVERRIDE_STANZA_REPO=$(echo "${OVERRIDE_STANZA}" | awk -F':' '{print (NF>1) ? $2 : ""}'); + if [ -n "${OVERRIDE_STANZA_REPO}" ] && [ -n "${stanza_repo}" ] && [ "${stanza_repo}" != "${OVERRIDE_STANZA_REPO}" ]; then + continue + fi; + # set repo from override + stanza_repo="${OVERRIDE_STANZA_REPO}"; fi; + # build the call command + CALL=( + "sudo" "-u" "${sudo_user}" + "pgbackrest" "--type=${BACKUP_TYPE}" "--stanza=${stanza}" + ); + if [ -n "${stanza_repo}" ]; then + CALL=("${CALL[@]}" "--repo=${stanza_repo}"); + fi; + CALL=("${CALL[@]}" "backup"); # main backup start START=$(date +'%s'); # shellcheck disable=SC2059 printf "${PRINTF_BLOCK}" "START" "$(date +'%F %T')" "${stanza}"; # --log-level-console=info if [ $TEST -eq 1 ]; then - echo "sudo -u ${sudo_user} pgbackrest --type=${BACKUP_TYPE} --stanza=${stanza} backup;"; + echo "${CALL[@]}"; else - sudo -u "${sudo_user}" pgbackrest --type="${BACKUP_TYPE}" --stanza="${stanza}" backup; + # sudo -u "${sudo_user}" pgbackrest --type="${BACKUP_TYPE}" --stanza="${stanza}" ${option_repo} backup; + "${CALL[@]}"; fi; - DURATION=$(( $(date +'%s')-START )); + DURATION=$(( $(date +'%s') - START )); # shellcheck disable=SC2059 printf "${PRINTF_BLOCK}" "END" "$(convert_time ${DURATION})" "${stanza}"; done <<< "$(cat "${stanza_file}")"; diff --git a/config/pgbackrest.sample.cfg b/config/pgbackrest.sample.cfg index b3abfcd..62d91eb 100644 --- a/config/pgbackrest.sample.cfg +++ b/config/pgbackrest.sample.cfg @@ -1,2 +1,2 @@ [PgBackRest] -sudo_user=pgbackrest +sudo_user=postgres diff --git a/config/stanza.sample.cfg b/config/stanza.sample.cfg index 39a5abc..09f2124 100644 --- a/config/stanza.sample.cfg +++ b/config/stanza.sample.cfg @@ -1,3 +1,4 @@ [stanzas] stanza_a -stanza_b +stanza_b:1 +stanza_b:2