Update pgbackrest wrapper to support multiple repositories in the selection

This commit is contained in:
2025-12-02 13:24:54 +09:00
parent 0c35a50648
commit a4b4e595b9
4 changed files with 52 additions and 37 deletions

View File

@@ -25,7 +25,7 @@ This file holds the sudo_user for calling the commands
```ini ```ini
[PgBackRest] [PgBackRest]
sudo_user=pgbackrest sudo_user=postgres
``` ```
### `config/stanza.cfg` ### `config/stanza.cfg`
@@ -34,8 +34,11 @@ Example file is `stanza.sample.cfg`
This files holds the stanzas to backup 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 ```ini
[stanzas] [stanzas]
stanza_a stanza_a
stanza_b stanza_b:1
stanza_b:2
``` ```

View File

@@ -113,6 +113,12 @@ while getopts ":b:s:lt" opt; do
esac; esac;
done; 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 ## SCRIPT
if [ $TEST -eq 1 ]; then if [ $TEST -eq 1 ]; then
@@ -121,9 +127,6 @@ fi;
if [ $LIST_STANZA -eq 1 ]; then if [ $LIST_STANZA -eq 1 ]; then
echo "+ Stanza List:"; echo "+ Stanza List:";
for stanza in ${stanza_list}; do
echo "${stanza}";
done;
while read -r stanza; do while read -r stanza; do
# skip empty # skip empty
[ -z "${stanza}" ] && continue; [ -z "${stanza}" ] && continue;
@@ -131,15 +134,25 @@ if [ $LIST_STANZA -eq 1 ]; then
[[ "${stanza}" =~ ${REGEX_COMMENT} ]] && continue; [[ "${stanza}" =~ ${REGEX_COMMENT} ]] && continue;
# skip the ini header blocks # skip the ini header blocks
[[ "${stanza}" =~ ${INI_BLOCK} ]] && continue; [[ "${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 override stanza matching
if [ "${stanza}" = "${OVERRIDE_STANZA}" ]; then if [ "${stanza_name}" = "${OVERRIDE_STANZA_NAME}" ]; then
echo "[*] ${stanza}"; 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 else
echo "[ ] ${stanza}"; echo "[ ] ${stanza}";
fi; fi;
done <<< "$(cat "${stanza_file}")"; done <<< "$(cat "${stanza_file}")";
echo ""; 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 ""; echo "";
exit; exit;
fi; fi;
@@ -155,29 +168,6 @@ if [ "${BACKUP_TYPE}" != "full" ] && [ "${BACKUP_TYPE}" != "diff" ]; then
fi; fi;
echo "* PgBackrest Backup run type '${BACKUP_TYPE}' on $(date +'%F')"; 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 # run backup
while read -r stanza; do while read -r stanza; do
# skip empty # skip empty
@@ -186,23 +176,44 @@ while read -r stanza; do
[[ "${stanza}" =~ ${REGEX_COMMENT} ]] && continue; [[ "${stanza}" =~ ${REGEX_COMMENT} ]] && continue;
# skip the ini header blocks # skip the ini header blocks
[[ "${stanza}" =~ ${INI_BLOCK} ]] && continue; [[ "${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 # override stanza check
if [ -n "${OVERRIDE_STANZA}" ]; then if [ -n "${OVERRIDE_STANZA}" ]; then
if [ "${stanza}" != "${OVERRIDE_STANZA}" ]; then # skip if not matching name
if [ "${stanza_name}" != "${OVERRIDE_STANZA_NAME}" ]; then
continue continue
fi; 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; 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 # main backup start
START=$(date +'%s'); START=$(date +'%s');
# shellcheck disable=SC2059 # shellcheck disable=SC2059
printf "${PRINTF_BLOCK}" "START" "$(date +'%F %T')" "${stanza}"; printf "${PRINTF_BLOCK}" "START" "$(date +'%F %T')" "${stanza}";
# --log-level-console=info # --log-level-console=info
if [ $TEST -eq 1 ]; then if [ $TEST -eq 1 ]; then
echo "sudo -u ${sudo_user} pgbackrest --type=${BACKUP_TYPE} --stanza=${stanza} backup;"; echo "${CALL[@]}";
else 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; fi;
DURATION=$(( $(date +'%s')-START )); DURATION=$(( $(date +'%s') - START ));
# shellcheck disable=SC2059 # shellcheck disable=SC2059
printf "${PRINTF_BLOCK}" "END" "$(convert_time ${DURATION})" "${stanza}"; printf "${PRINTF_BLOCK}" "END" "$(convert_time ${DURATION})" "${stanza}";
done <<< "$(cat "${stanza_file}")"; done <<< "$(cat "${stanza_file}")";

View File

@@ -1,2 +1,2 @@
[PgBackRest] [PgBackRest]
sudo_user=pgbackrest sudo_user=postgres

View File

@@ -1,3 +1,4 @@
[stanzas] [stanzas]
stanza_a stanza_a
stanza_b stanza_b:1
stanza_b:2