From 6efe39d8ad440aaedf1a9d0eeb8d03bbb61fa94d Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Tue, 2 Dec 2025 14:44:18 +0900 Subject: [PATCH] Allow setting of pgbackrest config file for each stanza --- ReadMe.md | 5 +++++ bin/pgbackrest_backup.sh | 37 +++++++++++++++++++++++++++++++++++- config/pgbackrest.sample.cfg | 1 + config/stanza.sample.cfg | 1 + 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/ReadMe.md b/ReadMe.md index 2f877b9..270d6a0 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -34,11 +34,16 @@ Example file is `stanza.sample.cfg` This files holds the stanzas to backup +`stanza:repo number:path to config file` + If the stanza has several repo settings as in "repo1-...", "repo2-..." then the repo to target can be listed in here by ":repo number" +The third optional parameter is a config file path + ```ini [stanzas] stanza_a stanza_b:1 stanza_b:2 +stanza_c::/etc/to/config ``` diff --git a/bin/pgbackrest_backup.sh b/bin/pgbackrest_backup.sh index 1761557..76fc966 100755 --- a/bin/pgbackrest_backup.sh +++ b/bin/pgbackrest_backup.sh @@ -71,6 +71,10 @@ if [ -z "${sudo_user}" ]; then echo "sudo_user not set in the config file ${config_file}"; exit; fi; +if [ -z "${pgbackrest_config}" ] ||[ ! -f "${pgbackrest_config}" ]; then + echo "Missing pgbackrest default config file ${pgbackrest_config}"; + exit; +fi; # stanza list file if [ ! -f "${stanza_file}" ]; then echo "Missing stanza config file ${stanza_file}"; @@ -179,6 +183,8 @@ while read -r stanza; do # 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 : ""}'); + # if we have a config + stanza_config=$(echo "${stanza}" | awk -F':' '{print (NF>1) ? $3 : ""}'); # override stanza check if [ -n "${OVERRIDE_STANZA}" ]; then # skip if not matching name @@ -187,20 +193,49 @@ while read -r stanza; do 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 + if [ -n "${OVERRIDE_STANZA_REPO}" ] && [ "${stanza_repo}" != "${OVERRIDE_STANZA_REPO}" ]; then continue fi; # set repo from override stanza_repo="${OVERRIDE_STANZA_REPO}"; + OVERRIDE_STANZA_CONFIG=$(echo "${OVERRIDE_STANZA}" | awk -F':' '{print (NF>2) ? $3 : ""}'); + # if we have a config set in the stanza config, but none in the override, use the one from the stanza config + if [ -z "${OVERRIDE_STANZA_CONFIG}" ] && [ -n "${stanza_config}" ]; then + OVERRIDE_STANZA_CONFIG="${stanza_config}"; + else + stanza_config="${OVERRIDE_STANZA_CONFIG}"; + fi; fi; # build the call command CALL=( "sudo" "-u" "${sudo_user}" "pgbackrest" "--type=${BACKUP_TYPE}" "--stanza=${stanza_name}" ); + _stanza_config="${pgbackrest_config}"; + if [ -f "${stanza_config}" ]; then + _stanza_config="${stanza_config}"; + fi; + stanza_check=$(grep -E "^\[${stanza_name}\]$" "${_stanza_config}"); + if [ -z "${stanza_check}" ]; then + echo "[!] Stanza ${stanza_name} not found in config file ${_stanza_config}, skipping stanza"; + continue; + fi; if [ -n "${stanza_repo}" ]; then + # check if repo exists in config file with "repo-" + repo_check=$(grep -E "^repo${stanza_repo}-" "${_stanza_config}"); + if [ -z "${repo_check}" ]; then + echo "[!] Stanza repo ${stanza_repo} for stanza ${stanza_name} not found in config file ${_stanza_config}, skipping stanza"; + continue; + fi; CALL=("${CALL[@]}" "--repo=${stanza_repo}"); fi; + if [ -n "${stanza_config}" ]; then + if [ ! -f "${stanza_config}" ]; then + echo "[!] Stanza config file ${stanza_config} for stanza ${stanza_name} not found, skipping stanza"; + continue; + fi; + CALL=("${CALL[@]}" "--config=${stanza_config}"); + fi; CALL=("${CALL[@]}" "backup"); # main backup start START=$(date +'%s'); diff --git a/config/pgbackrest.sample.cfg b/config/pgbackrest.sample.cfg index 62d91eb..9491873 100644 --- a/config/pgbackrest.sample.cfg +++ b/config/pgbackrest.sample.cfg @@ -1,2 +1,3 @@ [PgBackRest] sudo_user=postgres +pgbackrest_config=/etc/pgbackrest.conf diff --git a/config/stanza.sample.cfg b/config/stanza.sample.cfg index 09f2124..573fdad 100644 --- a/config/stanza.sample.cfg +++ b/config/stanza.sample.cfg @@ -2,3 +2,4 @@ stanza_a stanza_b:1 stanza_b:2 +stanca_c::/path/to/config/file