Compare commits

..

4 Commits

Author SHA1 Message Date
Clemens Schwaighofer
2e5f0648f1 Merge branch 'development' into shellcheck-cleanup 2025-08-21 10:42:20 +09:00
Clemens Schwaighofer
acd5d369eb Merge branch 'development' into shellcheck-cleanup 2025-08-20 22:41:54 +09:00
Clemens Schwaighofer
de9d5d3588 Zabbix backup set port to 5432 default to override internal set ports 2025-08-20 22:11:38 +09:00
Clemens Schwaighofer
8407defe4f Starting shellcheck cleanup 2024-09-04 10:58:48 +09:00
16 changed files with 163 additions and 332 deletions

View File

@@ -141,7 +141,7 @@ All below have default values if not set in the main settings file
* COMPRESSION: zstd * COMPRESSION: zstd
* COMPRESSION_LEVEL: 3 * COMPRESSION_LEVEL: 3
* ENCRYPTION: keyfile (with no password) * ENCRYPTION: none
* FORCE_VERIFY: false * FORCE_VERIFY: false
* COMPACT_INTERVAL: 1 * COMPACT_INTERVAL: 1
* CHECK_INTERVAL: none * CHECK_INTERVAL: none

View File

@@ -31,16 +31,16 @@ MODULE_LIST="file gitea mysql pgsql zabbix"
# -n for dry run test # -n for dry run test
while getopts ":c:snd" opt; do while getopts ":c:snd" opt; do
case "${opt}" in case "${opt}" in
c|config) c) # config
BASE_FOLDER=${OPTARG}; BASE_FOLDER=${OPTARG};
;; ;;
s|nosudo) s) # nosudo
PGSQL_SUDO=0; PGSQL_SUDO=0;
;; ;;
d|debug) d) # debug
DEBUG=1; DEBUG=1;
;; ;;
n|dryrun) n) # dryrun
DRYRUN=1; DRYRUN=1;
;; ;;
:) :)
@@ -64,6 +64,7 @@ if [ ! -f "${BASE_FOLDER}${SETTINGS_FILE}" ]; then
echo "Could not find: ${BASE_FOLDER}${SETTINGS_FILE}"; echo "Could not find: ${BASE_FOLDER}${SETTINGS_FILE}";
exit; exit;
fi; fi;
# shellcheck source="borg.backup.settings"
. "${BASE_FOLDER}${SETTINGS_FILE}"; . "${BASE_FOLDER}${SETTINGS_FILE}";
if [ -n "${TARGET_BORG_PATH}" ]; then if [ -n "${TARGET_BORG_PATH}" ]; then

View File

@@ -1,8 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# allow variables in printf format string
# shellcheck disable=SC2059
# Plain file backup # Plain file backup
# set last edit date + time # set last edit date + time
@@ -38,7 +35,7 @@ FOLDERS=();
# this if for debug output with quoted folders # this if for debug output with quoted folders
FOLDERS_Q=(); FOLDERS_Q=();
# include list # include list
while read -r include_folder; do while read include_folder; do
# strip any leading spaces from that folder # strip any leading spaces from that folder
include_folder=$(echo "${include_folder}" | sed -e 's/^[ \t]*//'); include_folder=$(echo "${include_folder}" | sed -e 's/^[ \t]*//');
# check that those folders exist, warn on error, # check that those folders exist, warn on error,
@@ -114,7 +111,7 @@ if [ -s "${BASE_FOLDER}${EXCLUDE_FILE}" ]; then
if [ -n "${exclude_folder}" ]; then if [ -n "${exclude_folder}" ]; then
# if it DOES NOT start with a / we assume free folder and add as is # if it DOES NOT start with a / we assume free folder and add as is
if [[ "${exclude_folder}" != /* ]]; then if [[ "${exclude_folder}" != /* ]]; then
echo "${exclude_folder}" >> "${TMP_EXCLUDE_FILE}"; echo "${exclude_folder}" >> ${TMP_EXCLUDE_FILE};
echo "+ [E] General exclude: '${exclude_folder}'"; echo "+ [E] General exclude: '${exclude_folder}'";
# if this is a glob, do a double check that the base folder actually exists (?) # if this is a glob, do a double check that the base folder actually exists (?)
elif [[ "${exclude_folder}" =~ $REGEX_GLOB ]]; then elif [[ "${exclude_folder}" =~ $REGEX_GLOB ]]; then
@@ -124,7 +121,7 @@ if [ -s "${BASE_FOLDER}${EXCLUDE_FILE}" ]; then
if [ ! -d "${_exclude_folder}" ]; then if [ ! -d "${_exclude_folder}" ]; then
echo "- [E] Exclude folder with glob '${exclude_folder}' does not exist or is not accessable"; echo "- [E] Exclude folder with glob '${exclude_folder}' does not exist or is not accessable";
else else
echo "${exclude_folder}" >> "${TMP_EXCLUDE_FILE}"; echo "${exclude_folder}" >> ${TMP_EXCLUDE_FILE};
echo "+ [E] Exclude folder with glob '${exclude_folder}'"; echo "+ [E] Exclude folder with glob '${exclude_folder}'";
fi; fi;
# do a warning for a possible invalid folder # do a warning for a possible invalid folder
@@ -132,7 +129,7 @@ if [ -s "${BASE_FOLDER}${EXCLUDE_FILE}" ]; then
elif [ ! -d "${exclude_folder}" ] && [ ! -e "${exclude_folder}" ]; then elif [ ! -d "${exclude_folder}" ] && [ ! -e "${exclude_folder}" ]; then
echo "- [E] Exclude folder or file '${exclude_folder}' does not exist or is not accessable"; echo "- [E] Exclude folder or file '${exclude_folder}' does not exist or is not accessable";
else else
echo "${exclude_folder}" >> "${TMP_EXCLUDE_FILE}"; echo "${exclude_folder}" >> ${TMP_EXCLUDE_FILE};
# if it is a folder, remove the last / or the symlink check will not work # if it is a folder, remove the last / or the symlink check will not work
if [ -d "${exclude_folder}" ]; then if [ -d "${exclude_folder}" ]; then
_exclude_folder=${exclude_folder%/*}; _exclude_folder=${exclude_folder%/*};

View File

@@ -1,8 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# allow variables in printf format string
# shellcheck disable=SC2059
if [ -z "${MODULE}" ]; then if [ -z "${MODULE}" ]; then
echo "Script cannot be run on its own"; echo "Script cannot be run on its own";
exit 1; exit 1;
@@ -21,13 +18,13 @@ if [ $# -ge 1 ] && [ "$1" = "auto" ]; then
# get current date timestmap # get current date timestmap
CURRENT_DATE=$(date +%s); CURRENT_DATE=$(date +%s);
# if =1 always ok # if =1 always ok
if [ "${CHECK_INTERVAL}" -eq 1 ]; then if [ ${CHECK_INTERVAL} -eq 1 ]; then
RUN_CHECK=1; RUN_CHECK=1;
# always add verify data for automatic check # always add verify data for automatic check
OPT_CHECK_VERIFY_DATA="--verify-data"; OPT_CHECK_VERIFY_DATA="--verify-data";
# set new check time here # set new check time here
echo "${CURRENT_DATE}" > "${BASE_FOLDER}${BACKUP_CHECK_FILE}"; echo ${CURRENT_DATE} > "${BASE_FOLDER}${BACKUP_CHECK_FILE}";
elif [ "${CHECK_INTERVAL}" -gt 1 ]; then elif [ ${CHECK_INTERVAL} -gt 1 ]; then
# else load last timestamp and check if today - last time stamp > days # else load last timestamp and check if today - last time stamp > days
if [ -z "${LAST_CHECK_DATE}" ]; then if [ -z "${LAST_CHECK_DATE}" ]; then
LAST_CHECK_DATE=$(cat "${BASE_FOLDER}${BACKUP_CHECK_FILE}" 2>/dev/null | sed -e 's/ //g'); LAST_CHECK_DATE=$(cat "${BASE_FOLDER}${BACKUP_CHECK_FILE}" 2>/dev/null | sed -e 's/ //g');
@@ -37,19 +34,19 @@ if [ $# -ge 1 ] && [ "$1" = "auto" ]; then
LAST_CHECK_DATE=0; LAST_CHECK_DATE=0;
fi; fi;
# if the difference greate than check date, run. CHECK INTERVAL is in days # if the difference greate than check date, run. CHECK INTERVAL is in days
if [ $((CURRENT_DATE - LAST_CHECK_DATE)) -ge $((CHECK_INTERVAL * 86400)) ]; then if [ $(($CURRENT_DATE-$LAST_CHECK_DATE)) -ge $((${CHECK_INTERVAL}*86400)) ]; then
RUN_CHECK=1; RUN_CHECK=1;
# always add verify data for automatic check # always add verify data for automatic check
OPT_CHECK_VERIFY_DATA="--verify-data"; OPT_CHECK_VERIFY_DATA="--verify-data";
# set new check time here # set new check time here
echo "${CURRENT_DATE}" > "${BASE_FOLDER}${BACKUP_CHECK_FILE}"; echo ${CURRENT_DATE} > "${BASE_FOLDER}${BACKUP_CHECK_FILE}";
fi; fi;
fi; fi;
elif [ "${CHECK}" -eq 1 ]; then elif [ ${CHECK} -eq 1 ]; then
RUN_CHECK=1; RUN_CHECK=1;
fi; fi;
if [ "${RUN_CHECK}" -eq 1 ]; then if [ ${RUN_CHECK} -eq 1 ]; then
# run borg check command # run borg check command
IFS=${_IFS}; IFS=${_IFS};
printf "${PRINTF_SUB_BLOCK}" "CHECK" "$(date +'%F %T')" "${MODULE}"; printf "${PRINTF_SUB_BLOCK}" "CHECK" "$(date +'%F %T')" "${MODULE}";
@@ -61,21 +58,21 @@ if [ "${RUN_CHECK}" -eq 1 ]; then
OPT_GLOB="-P ${CHECK_PREFIX}"; OPT_GLOB="-P ${CHECK_PREFIX}";
fi; fi;
# debug/dryrun # debug/dryrun
if [ "${DEBUG}" -eq 1 ] || [ "${DRYRUN}" -eq 1 ]; then if [ ${DEBUG} -eq 1 ] || [ ${DRYRUN} -eq 1 ]; then
echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";${BORG_COMMAND} check ${OPT_REMOTE} ${OPT_PROGRESS} ${OPT_CHECK_VERIFY_DATA} ${OPT_GLOB} ${REPOSITORY}"; echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";${BORG_COMMAND} check ${OPT_REMOTE} ${OPT_PROGRESS} ${OPT_CHECK_VERIFY_DATA} ${OPT_GLOB} ${REPOSITORY}";
fi; fi;
# run info command if not a dry drun # run info command if not a dry drun
if [ "${DRYRUN}" -eq 0 ]; then if [ ${DRYRUN} -eq 0 ]; then
# if glob add glob command directly # if glob add glob command directly
if [[ "${CHECK_PREFIX}" =~ $REGEX_GLOB ]]; then if [[ "${CHECK_PREFIX}" =~ $REGEX_GLOB ]]; then
${BORG_COMMAND} check ${OPT_REMOTE} ${OPT_PROGRESS} ${OPT_CHECK_VERIFY_DATA} -a "${CHECK_PREFIX}" "${REPOSITORY}"; ${BORG_COMMAND} check ${OPT_REMOTE} ${OPT_PROGRESS} ${OPT_CHECK_VERIFY_DATA} -a "${CHECK_PREFIX}" ${REPOSITORY};
else else
${BORG_COMMAND} check ${OPT_REMOTE} ${OPT_PROGRESS} ${OPT_CHECK_VERIFY_DATA} ${OPT_GLOB} "${REPOSITORY}"; ${BORG_COMMAND} check ${OPT_REMOTE} ${OPT_PROGRESS} ${OPT_CHECK_VERIFY_DATA} ${OPT_GLOB} ${REPOSITORY};
fi; fi;
fi; fi;
# print additional info for use --repair command # print additional info for use --repair command
# but only for manual checks # but only for manual checks
if [ "${VERBOSE}" -eq 1 ] && [ "${CHECK}" -eq 1 ]; then if [ ${VERBOSE} -eq 1 ] && [ ${CHECK} -eq 1 ]; then
echo ""; echo "";
echo "In case of needed repair: " echo "In case of needed repair: "
echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";${BORG_COMMAND} check ${OPT_REMOTE} ${OPT_PROGRESS} --repair ${OPT_GLOB} ${REPOSITORY}"; echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";${BORG_COMMAND} check ${OPT_REMOTE} ${OPT_PROGRESS} --repair ${OPT_GLOB} ${REPOSITORY}";

View File

@@ -1,8 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# allow variables in printf format string
# shellcheck disable=SC2059
if [ -z "${MODULE}" ]; then if [ -z "${MODULE}" ]; then
echo "Script cannot be run on its own"; echo "Script cannot be run on its own";
exit 1; exit 1;
@@ -19,7 +16,7 @@ if [ $# -ge 1 ] && [ "$1" = "1" ]; then
printf "${PRINTF_MASTER_BLOCK}" "ERROR" "$(date +'%F %T')" "${MODULE}"; printf "${PRINTF_MASTER_BLOCK}" "ERROR" "$(date +'%F %T')" "${MODULE}";
else else
# running time calculation # running time calculation
DURATION=$(( $(date +'%s') - START )); DURATION=$(( $(date +'%s')-START ));
echo "=== [Run time: $(convert_time ${DURATION})]"; echo "=== [Run time: $(convert_time ${DURATION})]";
printf "${PRINTF_MASTER_BLOCK}" "END" "$(date +'%F %T')" "${MODULE}"; printf "${PRINTF_MASTER_BLOCK}" "END" "$(date +'%F %T')" "${MODULE}";
fi; fi;

View File

@@ -1,8 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# allow variables in printf format string
# shellcheck disable=SC2059
if [ -z "${MODULE}" ]; then if [ -z "${MODULE}" ]; then
echo "Script cannot be run on its own"; echo "Script cannot be run on its own";
exit 1; exit 1;
@@ -35,7 +32,7 @@ if [ "$(version "$BORG_VERSION")" -ge "$(version "1.2.0")" ]; then
LAST_COMPACT_DATE=0; LAST_COMPACT_DATE=0;
fi; fi;
# if the difference greate than compact date, run. COMPACT INTERVAL is in days # if the difference greate than compact date, run. COMPACT INTERVAL is in days
if [ $((CURRENT_DATE - LAST_COMPACT_DATE)) -ge $((COMPACT_INTERVAL * 86400)) ]; then if [ $((CURRENT_DATE-LAST_COMPACT_DATE)) -ge $((COMPACT_INTERVAL*86400)) ]; then
RUN_COMPACT=1; RUN_COMPACT=1;
# set new compact time here # set new compact time here
echo "${CURRENT_DATE}" > "${BASE_FOLDER}${BACKUP_COMPACT_FILE}"; echo "${CURRENT_DATE}" > "${BASE_FOLDER}${BACKUP_COMPACT_FILE}";
@@ -45,9 +42,10 @@ if [ "$(version "$BORG_VERSION")" -ge "$(version "1.2.0")" ]; then
RUN_COMPACT=1; RUN_COMPACT=1;
fi; fi;
if [ "${RUN_COMPACT}" -eq 1 ]; then if [ ${RUN_COMPACT} -eq 1 ]; then
# reset to normal IFS, so command works here # reset to normal IFS, so command works here
IFS=${_IFS}; IFS=${_IFS};
# shellcheck disable=SC2059
printf "${PRINTF_SUB_BLOCK}" "COMPACT" "$(date +'%F %T')" "${MODULE}"; printf "${PRINTF_SUB_BLOCK}" "COMPACT" "$(date +'%F %T')" "${MODULE}";
BORG_COMPACT="${BORG_COMMAND} compact ${OPT_REMOTE} -v ${OPT_PROGRESS} ${REPOSITORY}"; BORG_COMPACT="${BORG_COMMAND} compact ${OPT_REMOTE} -v ${OPT_PROGRESS} ${REPOSITORY}";
if [ "${DEBUG}" -eq 1 ]; then if [ "${DEBUG}" -eq 1 ]; then

View File

@@ -1,8 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# allow variables in printf format string
# shellcheck disable=SC2059
if [ -z "${MODULE}" ]; then if [ -z "${MODULE}" ]; then
echo "Script cannot be run on its own"; echo "Script cannot be run on its own";
exit 1; exit 1;
@@ -16,15 +13,10 @@ if [ "${INFO}" -eq 1 ]; then
fi; fi;
# run info command if not a dry drun # run info command if not a dry drun
if [ "${DRYRUN}" -eq 0 ]; then if [ "${DRYRUN}" -eq 0 ]; then
${BORG_COMMAND} info ${OPT_REMOTE} "${REPOSITORY}"; ${BORG_COMMAND} info ${OPT_REMOTE} ${REPOSITORY};
if [ "${VERBOSE}" -eq 1 ]; then
# print key information
echo "------------------------------------------------------------------------------";
${BORG_COMMAND} key export "${REPOSITORY}";
fi;
fi; fi;
if [ "${MODULE}" = "files" ]; then if [ "${MODULE}" = "files" ]; then
if [ "${FOLDER_OK}" -eq 1 ]; then if [ "$FOLDER_OK" -eq 1 ]; then
echo "--- [Run command]:"; echo "--- [Run command]:";
#IFS="#"; #IFS="#";
echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";${COMMAND} ${FOLDERS_Q[*]}"; echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";${COMMAND} ${FOLDERS_Q[*]}";

View File

@@ -5,24 +5,16 @@ if [ -z "${MODULE}" ]; then
exit 1; exit 1;
fi; fi;
# E: inherit trap ERR
# T: DEBUG and RETURN traps are inherited
# u: unset variables ere error
set -ETu #-e -o pipefail set -ETu #-e -o pipefail
trap _catch ERR trap cleanup SIGINT SIGTERM ERR
trap _cleanup SIGINT SIGTERM
_cleanup() { cleanup() {
# script cleanup here # script cleanup here
echo "Script abort: $? @LINE: $(caller)"; echo "Some part of the script failed with an error: $? @LINE: $(caller)";
# unset exported vars # unset exported vars
unset BORG_BASE_DIR BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK BORG_RELOCATED_REPO_ACCESS_IS_OK; unset BORG_BASE_DIR BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK BORG_RELOCATED_REPO_ACCESS_IS_OK;
# end trap # end trap
trap - SIGINT SIGTERM trap - SIGINT SIGTERM ERR
}
_catch() {
local last_exit_code=$?;
echo "Some part of the script failed with ERROR: $last_exit_code @COMMAND: '$BASH_COMMAND' @LINE: $(caller)" >&2;
} }
# on exit unset any exported var # on exit unset any exported var
trap "unset BORG_BASE_DIR BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK BORG_RELOCATED_REPO_ACCESS_IS_OK" EXIT; trap "unset BORG_BASE_DIR BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK BORG_RELOCATED_REPO_ACCESS_IS_OK" EXIT;
@@ -32,7 +24,7 @@ function version {
} }
# version for all general files # version for all general files
VERSION="4.8.0"; VERSION="4.5.4";
# borg version and borg comamnd # borg version and borg comamnd
BORG_VERSION=""; BORG_VERSION="";
@@ -93,9 +85,7 @@ REGEX="";
REGEX_COMMENT="^[\ \t]*#"; REGEX_COMMENT="^[\ \t]*#";
REGEX_GLOB='\*'; REGEX_GLOB='\*';
REGEX_NUMERIC="^[0-9]{1,2}$"; REGEX_NUMERIC="^[0-9]{1,2}$";
# port regex, but only approximately REGEX_ERROR="^Some part of the script failed with an error:";
REGEX_PORT="^[0-9]{2,5}$";
REGEX_ERROR="^Some part of the script failed with ERROR:";
PRUNE_DEBUG=""; PRUNE_DEBUG="";
INIT_REPOSITORY=0; INIT_REPOSITORY=0;
FOLDER_OK=0; FOLDER_OK=0;
@@ -135,7 +125,7 @@ COMPRESSION_LEVEL="";
SUB_COMPRESSION=""; SUB_COMPRESSION="";
SUB_COMPRESSION_LEVEL=""; SUB_COMPRESSION_LEVEL="";
# encryption settings # encryption settings
DEFAULT_ENCRYPTION="keyfile"; DEFAULT_ENCRYPTION="none";
ENCRYPTION=""; ENCRYPTION="";
# force verify always # force verify always
DEFAULT_FORCE_VERIFY="false"; DEFAULT_FORCE_VERIFY="false";
@@ -158,10 +148,6 @@ SUB_BACKUP_SET="";
# for database backup only # for database backup only
DATABASE_FULL_DUMP=""; DATABASE_FULL_DUMP="";
DATABASE_USER=""; DATABASE_USER="";
DATABASE_USE_SUDO="";
DATABASE_SUDO_USER="";
DATABASE_PORT="";
DATABASE_HOST="";
# only for mysql old config file # only for mysql old config file
MYSQL_DB_CONFIG=""; MYSQL_DB_CONFIG="";
MYSQL_DB_CONFIG_PARAM=""; MYSQL_DB_CONFIG_PARAM="";
@@ -348,12 +334,12 @@ if [ ${VERIFY} -eq 1 ] || [ ${INIT} -eq 1 ] && [ ${INFO} -eq 1 ]; then
exit 1; exit 1;
fi; fi;
# print -P cannot be run with -i/-C/-I together # print -P cannot be run with -i/-C/-I together
if [ ${PRINT} -eq 1 ] && { [ ${INIT} -eq 1 ] || [ ${VERIFY} -eq 1 ] || [ ${INFO} -eq 1 ]; }; then if [ ${PRINT} -eq 1 ] && ([ ${INIT} -eq 1 ] || [ ${VERIFY} -eq 1 ] || [ ${INFO} -eq 1 ]); then
echo "Cannot have -P print option and -i info, -V verify or -I initizalize option at the same time"; echo "Cannot have -P print option and -i info, -V verify or -I initizalize option at the same time";
exit 1; exit 1;
fi; fi;
# if tag is set, you can't have init, verify, info, etc # if tag is set, you can't have init, verify, info, etc
if [ -n "${ONE_TIME_TAG}" ] && { [ ${PRINT} -eq 1 ] || [ ${INIT} -eq 1 ] || [ ${VERIFY} -eq 1 ] || [ ${INFO} -eq 1 ]; }; then if [ -n "${ONE_TIME_TAG}" ] && ([ ${PRINT} -eq 1 ] || [ ${INIT} -eq 1 ] || [ ${VERIFY} -eq 1 ] || [ ${INFO} -eq 1 ]); then
echo "Cannot have -T '${ONE_TIME_TAG}' option with -i info, -V verify, -I initialize or -P print option at the same time"; echo "Cannot have -T '${ONE_TIME_TAG}' option with -i info, -V verify, -I initialize or -P print option at the same time";
exit 1; exit 1;
fi; fi;
@@ -362,18 +348,18 @@ if [ -n "${ONE_TIME_TAG}" ] && ! [[ "${ONE_TIME_TAG}" =~ ^[A-Za-z0-9_-]+$ ]]; th
echo "One time tag '${ONE_TIME_TAG}' must be alphanumeric with dashes and underscore only."; echo "One time tag '${ONE_TIME_TAG}' must be alphanumeric with dashes and underscore only.";
exit 1; exit 1;
elif [ -n "${ONE_TIME_TAG}" ]; then elif [ -n "${ONE_TIME_TAG}" ]; then
# all ok, attach '.' at the end # all ok, attach . at the end
ONE_TIME_TAG=${ONE_TIME_TAG}"."; ONE_TIME_TAG=${ONE_TIME_TAG}".";
fi; fi;
# if -D, cannot be with -T, -i, -C, -I, -P # if -D, cannot be with -T, -i, -C, -I, -P
if [ -n "${DELETE_ONE_TIME_TAG}" ] && { [ -n "${ONE_TIME_TAG}" ] || [ ${PRINT} -eq 1 ] || [ ${INIT} -eq 1 ] || [ ${VERIFY} -eq 1 ] || [ ${INFO} -eq 1 ]; }; then if [ -n "${DELETE_ONE_TIME_TAG}" ] && ([ -n "${ONE_TIME_TAG}" ] || [ ${PRINT} -eq 1 ] || [ ${INIT} -eq 1 ] || [ ${VERIFY} -eq 1 ] || [ ${INFO} -eq 1 ]); then
echo "Cannot have -D delete tag option with -T one time tag, -i info, -V verify, -I initialize or -P print option at the same time"; echo "Cannot have -D delete tag option with -T one time tag, -i info, -V verify, -I initialize or -P print option at the same time";
exit 1; exit 1;
fi; fi;
# -D also must be in valid backup set format # -D also must be in valid backup set format
# ! [[ "${DELETE_ONE_TIME_TAG}" =~ ^[A-Za-z0-9_-]+\.${MODULE},(\*-)?[0-9]{4}-[0-9]{2}-[0-9]{2}T\*$ ]] # ! [[ "${DELETE_ONE_TIME_TAG}" =~ ^[A-Za-z0-9_-]+\.${MODULE},(\*-)?[0-9]{4}-[0-9]{2}-[0-9]{2}T\*$ ]]
if [ -n "${DELETE_ONE_TIME_TAG}" ] && ! [[ "${DELETE_ONE_TIME_TAG}" =~ ^[A-Za-z0-9_-]+\.${MODULE},([A-Za-z0-9_-]+-)?[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$ ]] && ! [[ "${DELETE_ONE_TIME_TAG}" =~ ^[A-Za-z0-9_-]+\.${MODULE},(\*-)?[0-9]{4}-[0-9]{2}-[0-9]{2}T\*$ ]]; then if [ -n "${DELETE_ONE_TIME_TAG}" ] && ! [[ "${DELETE_ONE_TIME_TAG}" =~ ^[A-Za-z0-9_-]+\.${MODULE},([A-Za-z0-9_-]+-)?[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$ ]] && ! [[ "${DELETE_ONE_TIME_TAG}" =~ ^[A-Za-z0-9_-]+\.${MODULE},(\*-)?[0-9]{4}-[0-9]{2}-[0-9]{2}T\*$ ]]; then
echo "Delete one time tag '${DELETE_ONE_TIME_TAG}' is in an invalid format." echo "Delete one time tag '${DELETE_ONE_TIME_TAG}' is in an invalid format. "
echo "Please verify existing tags with -P option." echo "Please verify existing tags with -P option."
echo "For a globing be sure it is in the format of: TAG.MODULE,*-YYYY-MM-DDT*"; echo "For a globing be sure it is in the format of: TAG.MODULE,*-YYYY-MM-DDT*";
echo "Note the dash (-) after the first *, also time (T) is a globa (*) must." echo "Note the dash (-) after the first *, also time (T) is a globa (*) must."
@@ -613,26 +599,11 @@ fi;
# if ENCRYPTION is empty or not in the valid list fall back to none # if ENCRYPTION is empty or not in the valid list fall back to none
# NOTE This is currently set in default and doesn't need to be set on empty # NOTE This is currently set in default and doesn't need to be set on empty
# only ivalid should be checked # only ivalid should be checked
if #if [ -z "${ENCRYPTION}" ]; then
[ "${ENCRYPTION}" = "authenticated" ] || # ENCRYPTION="none";
[ "${ENCRYPTION}" = "repokey" ] || #else
[ "${ENCRYPTION}" = "authenticated-blake2" ] || # TODO check for invalid encryption string
[ "${ENCRYPTION}" = "repokey-blake2" ] ; #fi;
then
# if "authenticated" or "repokey" a password must be set
if [[ ! -v BORG_PASSPHRASE ]] && [[ ! -v BORG_PASSCOMMAND ]] && [[ ! -v BORG_PASSPHRASE_FD ]]; then
echo "Encryption method '${ENCRYPTION}' requires a BORG_PASSPHRASE, BORG_PASSCOMMAND or BORG_PASSPHRASE_FD to be set.";
exit 1;
fi;
elif [ "${ENCRYPTION}" = "keyfile" ] || [ "${ENCRYPTION}" = "keyfile-blake2" ]; then
# if no password, set empty password
if [[ ! -v BORG_PASSPHRASE ]] && [[ ! -v BORG_PASSCOMMAND ]] && [[ ! -v BORG_PASSPHRASE_FD ]]; then
export BORG_PASSPHRASE="";
fi;
elif [ "${ENCRYPTION}" != "none" ]; then
echo "Encryption method '${ENCRYPTION}' is not valid.";
exit 1;
fi;
## FUNCTIONS ## FUNCTIONS
@@ -647,22 +618,22 @@ function convert_time
{ {
timestamp=${1}; timestamp=${1};
# round to four digits for ms # round to four digits for ms
timestamp=$(printf "%1.4f" "$timestamp"); timestamp=$(printf "%1.4f" $timestamp);
# get the ms part and remove any leading 0 # get the ms part and remove any leading 0
ms=$(echo "${timestamp}" | cut -d "." -f 2 | sed -e 's/^0*//'); ms=$(echo ${timestamp} | cut -d "." -f 2 | sed -e 's/^0*//');
timestamp=$(echo "${timestamp}" | cut -d "." -f 1); timestamp=$(echo ${timestamp} | cut -d "." -f 1);
timegroups=(86400 3600 60 1); # day, hour, min, sec timegroups=(86400 3600 60 1); # day, hour, min, sec
timenames=("d" "h" "m" "s"); # day, hour, min, sec timenames=("d" "h" "m" "s"); # day, hour, min, sec
output=( ); output=( );
time_string=; time_string=;
for timeslice in "${timegroups[@]}"; do for timeslice in ${timegroups[@]}; do
# floor for the division, push to output # floor for the division, push to output
output[${#output[*]}]=$(awk "BEGIN {printf \"%d\", ${timestamp}/${timeslice}}"); output[${#output[*]}]=$(awk "BEGIN {printf \"%d\", ${timestamp}/${timeslice}}");
timestamp=$(awk "BEGIN {printf \"%d\", ${timestamp}%${timeslice}}"); timestamp=$(awk "BEGIN {printf \"%d\", ${timestamp}%${timeslice}}");
done; done;
for ((i=0; i<${#output[@]}; i++)); do for ((i=0; i<${#output[@]}; i++)); do
if [ "${output[$i]}" -gt 0 ] || [ -n "$time_string" ]; then if [ ${output[$i]} -gt 0 ] || [ -n "$time_string" ]; then
if [ -n "${time_string}" ]; then if [ -n "${time_string}" ]; then
time_string=${time_string}" "; time_string=${time_string}" ";
fi; fi;

View File

@@ -1,8 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# allow variables in printf format string
# shellcheck disable=SC2059
if [ -z "${MODULE}" ]; then if [ -z "${MODULE}" ]; then
echo "Script cannot be run on its own"; echo "Script cannot be run on its own";
exit 1; exit 1;
@@ -13,7 +10,7 @@ START=$(date +'%s');
# set init date, or today if not file is set # set init date, or today if not file is set
BACKUP_INIT_DATE=''; BACKUP_INIT_DATE='';
if [ -f "${BASE_FOLDER}${BACKUP_INIT_FILE}" ]; then if [ -f "${BASE_FOLDER}${BACKUP_INIT_FILE}" ]; then
BACKUP_INIT_DATE=$(printf '%(%c)T' "$(cat "${BASE_FOLDER}${BACKUP_INIT_FILE}" 2>/dev/null)"); BACKUP_INIT_DATE=$(printf '%(%c)T' $(cat "${BASE_FOLDER}${BACKUP_INIT_FILE}" 2>/dev/null));
fi; fi;
# start logging from here # start logging from here
exec &> >(tee -a "${LOG}"); exec &> >(tee -a "${LOG}");
@@ -39,7 +36,7 @@ if [ "$(version "$BORG_VERSION")" -ge "$(version "1.2.0")" ]; then
if [ -f "${BASE_FOLDER}${BACKUP_COMPACT_FILE}" ]; then if [ -f "${BASE_FOLDER}${BACKUP_COMPACT_FILE}" ]; then
LAST_COMPACT_DATE=$(cat "${BASE_FOLDER}${BACKUP_COMPACT_FILE}" 2>/dev/null); LAST_COMPACT_DATE=$(cat "${BASE_FOLDER}${BACKUP_COMPACT_FILE}" 2>/dev/null);
printf "${PRINTF_INFO_STRING}" "Module last compact" \ printf "${PRINTF_INFO_STRING}" "Module last compact" \
"$(printf '%(%c)T' "${LAST_COMPACT_DATE}") ($(convert_time $(($(date +%s) - LAST_COMPACT_DATE))) ago)"; "$(printf '%(%c)T' ${LAST_COMPACT_DATE}) ($(convert_time $(($(date +%s)-${LAST_COMPACT_DATE}))) ago)";
else else
printf "${PRINTF_INFO_STRING}" "Module last compact" "No compact run yet" printf "${PRINTF_INFO_STRING}" "Module last compact" "No compact run yet"
fi; fi;
@@ -52,7 +49,7 @@ if [ "${CHECK_INTERVAL##*[!0-9]*}" ]; then
if [ -f "${BASE_FOLDER}${BACKUP_CHECK_FILE}" ]; then if [ -f "${BASE_FOLDER}${BACKUP_CHECK_FILE}" ]; then
LAST_CHECK_DATE=$(cat "${BASE_FOLDER}${BACKUP_CHECK_FILE}" 2>/dev/null); LAST_CHECK_DATE=$(cat "${BASE_FOLDER}${BACKUP_CHECK_FILE}" 2>/dev/null);
printf "${PRINTF_INFO_STRING}" "Module last check" \ printf "${PRINTF_INFO_STRING}" "Module last check" \
"$(printf '%(%c)T' "${LAST_CHECK_DATE}") ($(convert_time $(($(date +%s) - LAST_CHECK_DATE))) ago)"; "$(printf '%(%c)T' ${LAST_CHECK_DATE}) ($(convert_time $(($(date +%s)-${LAST_CHECK_DATE}))) ago)";
else else
printf "${PRINTF_INFO_STRING}" "Module last check" "No check run yet"; printf "${PRINTF_INFO_STRING}" "Module last check" "No check run yet";
fi; fi;
@@ -155,10 +152,10 @@ if [ -n "${COMPRESSION}" ]; then
# fi; # fi;
error_message="[! $(date +'%F %T')] Compression level for ${COMPRESSION} needs to be a numeric value between ${MIN_COMPRESSION} and ${MAX_COMPRESSION}: ${COMPRESSION_LEVEL}"; error_message="[! $(date +'%F %T')] Compression level for ${COMPRESSION} needs to be a numeric value between ${MIN_COMPRESSION} and ${MAX_COMPRESSION}: ${COMPRESSION_LEVEL}";
if ! [[ "${COMPRESSION_LEVEL}" =~ ${REGEX_NUMERIC} ]]; then if ! [[ "${COMPRESSION_LEVEL}" =~ ${REGEX_NUMERIC} ]]; then
echo "${error_message}"; echo ${error_message};
exit 1; exit 1;
elif [ "${COMPRESSION_LEVEL}" -lt "${MIN_COMPRESSION}" ] || [ "${COMPRESSION_LEVEL}" -gt "${MAX_COMPRESSION}" ]; then elif [ ${COMPRESSION_LEVEL} -lt ${MIN_COMPRESSION} ] || [ ${COMPRESSION_LEVEL} -gt ${MAX_COMPRESSION} ]; then
echo "${error_message}"; echo ${error_message};
exit 1; exit 1;
else else
OPT_COMPRESSION=${OPT_COMPRESSION}","${COMPRESSION_LEVEL}; OPT_COMPRESSION=${OPT_COMPRESSION}","${COMPRESSION_LEVEL};
@@ -291,18 +288,20 @@ COMMAND_INFO="${COMMAND_EXPORT}${BORG_COMMAND} info ${OPT_REMOTE} ${REPOSITORY}"
# if this is user@host, we need to use ssh command to verify if the file is there # if this is user@host, we need to use ssh command to verify if the file is there
# else a normal verify is ok # else a normal verify is ok
# unless explicit given, verify is skipped # unless explicit given, verify is skipped
# MARK: VERIFY / INFO
if [ "${VERIFY}" -eq 1 ] || [ "${INIT}" -eq 1 ]; then if [ "${VERIFY}" -eq 1 ] || [ "${INIT}" -eq 1 ]; then
printf "${PRINTF_SUB_BLOCK}" "VERIFY" "$(date +'%F %T')" "${MODULE}"; printf "${PRINTF_SUB_BLOCK}" "VERIFY" "$(date +'%F %T')" "${MODULE}";
if [ "${DEBUG}" -eq 1 ]; then if [ -n "${TARGET_SERVER}" ]; then
echo "${BORG_COMMAND} info ${OPT_REMOTE} ${REPOSITORY} 2>&1 "; if [ "${DEBUG}" -eq 1 ]; then
fi; echo "${BORG_COMMAND} info ${OPT_REMOTE} ${REPOSITORY} 2>&1|grep \"Repository ID:\"";
# use borg info and verify if it returns "Repository ID:" in the first line fi;
REPO_VERIFY=$(${BORG_COMMAND} info ${OPT_REMOTE} "${REPOSITORY}" 2>&1); # use borg info and verify if it returns "Repository ID:" in the first line
__last_error=$?; REPO_VERIFY=$(${BORG_COMMAND} info ${OPT_REMOTE} ${REPOSITORY} 2>&1|grep "Repository ID:");
# on any error in verify command force new INIT # this is currently a hack to work round the error code in borg info
if [[ $__last_error -ne 0 ]]; then # this checks if REPO_VERIFY holds this error message and then starts init
echo "[!] Repository verify error: ${REPO_VERIFY}"; if [[ -z "${REPO_VERIFY}" ]] || [[ "${REPO_VERIFY}" =~ ${REGEX_ERROR} ]]; then
INIT_REPOSITORY=1;
fi;
elif [ ! -d "${REPOSITORY}" ]; then
INIT_REPOSITORY=1; INIT_REPOSITORY=1;
fi; fi;
# if verrify but no init and repo is there but init file is missing set it # if verrify but no init and repo is there but init file is missing set it
@@ -326,29 +325,14 @@ if [ "${VERIFY}" -eq 1 ] || [ "${INIT}" -eq 1 ]; then
exit; exit;
fi; fi;
fi; fi;
# MARK: INIT
if [ "${INIT}" -eq 1 ] && [ "${INIT_REPOSITORY}" -eq 1 ]; then if [ "${INIT}" -eq 1 ] && [ "${INIT_REPOSITORY}" -eq 1 ]; then
printf "${PRINTF_SUB_BLOCK}" "INIT" "$(date +'%F %T')" "${MODULE}"; printf "${PRINTF_SUB_BLOCK}" "INIT" "$(date +'%F %T')" "${MODULE}";
if [ "${DEBUG}" -eq 1 ] || [ "${DRYRUN}" -eq 1 ]; then if [ "${DEBUG}" -eq 1 ] || [ "${DRYRUN}" -eq 1 ]; then
echo "${BORG_COMMAND} init ${OPT_REMOTE} -e ${ENCRYPTION} ${OPT_VERBOSE} ${REPOSITORY}"; echo "${BORG_COMMAND} init ${OPT_REMOTE} -e ${ENCRYPTION} ${OPT_VERBOSE} ${REPOSITORY}";
echo "${BORG_COMMAND} key export ${REPOSITORY}";
echo "${BORG_COMMAND} key export --paper ${REPOSITORY}";
fi fi
if [ "${DRYRUN}" -eq 0 ]; then if [ "${DRYRUN}" -eq 0 ]; then
# should trap and exit properly here # should trap and exit properly here
${BORG_COMMAND} init ${OPT_REMOTE} -e "${ENCRYPTION}" ${OPT_VERBOSE} "${REPOSITORY}"; ${BORG_COMMAND} init ${OPT_REMOTE} -e ${ENCRYPTION} ${OPT_VERBOSE} ${REPOSITORY};
# show the key file
if [ "${ENCRYPTION}" = "keyfile" ]; then
echo "--- [ENCRYPTION KEY] --[START]-------------------------------------------------->";
echo "Store the key and password in a safe place";
echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";borg key export [--paper] ${REPOSITORY}";
echo "----[BORG KEY] -------------------------------->";
${BORG_COMMAND} key export "${REPOSITORY}";
echo "----[BORG KEY:paper] -------------------------->";
${BORG_COMMAND} key export --paper "${REPOSITORY}";
echo "--- [ENCRYPTION KEY] --[END ]-------------------------------------------------->";
fi;
# write init file # write init file
date +%s > "${BASE_FOLDER}${BACKUP_INIT_FILE}"; date +%s > "${BASE_FOLDER}${BACKUP_INIT_FILE}";
echo "Repository initialized"; echo "Repository initialized";
@@ -359,7 +343,7 @@ if [ "${INIT}" -eq 1 ] && [ "${INIT_REPOSITORY}" -eq 1 ]; then
# exit after init # exit after init
exit; exit;
elif [ "${INIT}" -eq 1 ] && [ "${INIT_REPOSITORY}" -eq 0 ]; then elif [ "${INIT}" -eq 1 ] && [ "${INIT_REPOSITORY}" -eq 0 ]; then
echo "[!] ($(date +'%F %T')) Repository already initialized"; echo "[! $(date +'%F %T')] Repository already initialized";
echo "For more information run:" echo "For more information run:"
echo "${COMMAND_INFO}"; echo "${COMMAND_INFO}";
. "${DIR}/borg.backup.functions.close.sh" 1; . "${DIR}/borg.backup.functions.close.sh" 1;
@@ -368,13 +352,12 @@ fi;
# verify for init file # verify for init file
if [ ! -f "${BASE_FOLDER}${BACKUP_INIT_FILE}" ]; then if [ ! -f "${BASE_FOLDER}${BACKUP_INIT_FILE}" ]; then
echo "[!] ($(date +'%F %T')) It seems the repository has never been initialized." echo "[! $(date +'%F %T')] It seems the repository has never been initialized."
echo "Please run -I to initialize or if already initialzed run with -C for init update." echo "Please run -I to initialize or if already initialzed run with -C for init update."
. "${DIR}/borg.backup.functions.close.sh" 1; . "${DIR}/borg.backup.functions.close.sh" 1;
exit 1; exit 1;
fi; fi;
# MARK: LIST / PRINT
# PRINT OUT current data, only do this if REPO exists # PRINT OUT current data, only do this if REPO exists
if [ "${PRINT}" -eq 1 ]; then if [ "${PRINT}" -eq 1 ]; then
printf "${PRINTF_SUB_BLOCK}" "PRINT" "$(date +'%F %T')" "${MODULE}"; printf "${PRINTF_SUB_BLOCK}" "PRINT" "$(date +'%F %T')" "${MODULE}";
@@ -385,7 +368,7 @@ if [ "${PRINT}" -eq 1 ]; then
fi; fi;
# run info command if not a dry drun # run info command if not a dry drun
if [ "${DRYRUN}" -eq 0 ]; then if [ "${DRYRUN}" -eq 0 ]; then
${BORG_COMMAND} list ${OPT_REMOTE} --format "${FORMAT}" "${REPOSITORY}" ; ${BORG_COMMAND} list ${OPT_REMOTE} --format "${FORMAT}" ${REPOSITORY} ;
fi; fi;
if [ "${VERBOSE}" -eq 1 ]; then if [ "${VERBOSE}" -eq 1 ]; then
echo ""; echo "";
@@ -451,7 +434,7 @@ if [ -n "${DELETE_ONE_TIME_TAG}" ]; then
# not that compact only works on borg 1.2 # not that compact only works on borg 1.2
if [ "$(version "$BORG_VERSION")" -ge "$(version "1.2.0")" ]; then if [ "$(version "$BORG_VERSION")" -ge "$(version "1.2.0")" ]; then
if [ "${DRYRUN}" -eq 0 ]; then if [ "${DRYRUN}" -eq 0 ]; then
${BORG_COMMAND} compact ${OPT_REMOTE} "${REPOSITORY}"; ${BORG_COMMAND} compact ${OPT_REMOTE} ${REPOSITORY};
fi; fi;
if [ "${DEBUG}" -eq 1 ]; then if [ "${DEBUG}" -eq 1 ]; then
echo "${BORG_COMMAND} compact ${OPT_REMOTE} ${REPOSITORY}"; echo "${BORG_COMMAND} compact ${OPT_REMOTE} ${REPOSITORY}";

View File

@@ -1,12 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# allow variables in printf format string
# shellcheck disable=SC2059
# Backup gitea database, all git folders and gitea settings # Backup gitea database, all git folders and gitea settings
MODULE="gitea" MODULE="gitea"
MODULE_VERSION="1.2.2"; MODULE_VERSION="1.2.1";
DIR="${BASH_SOURCE%/*}" DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
@@ -96,7 +93,7 @@ if [ ${DRYRUN} -eq 0 ]; then
fi; fi;
chown -R ${GIT_USER}: "${GITEA_WORKING_DIR}"; chown -R ${GIT_USER}: "${GITEA_WORKING_DIR}";
# this needs to be run in a folder that can be stat by git user # this needs to be run in a folder that can be stat by git user
cd "${GITEA_WORKING_DIR}" || exit 1; cd "${GITEA_WORKING_DIR}";
sudo -u ${GIT_USER} ${GITEA_BIN} dump -c ${GITEA_CONFIG} -w ${GITEA_WORKING_DIR} -t ${GITEA_TEMP_DIR} --type ${GITEA_EXPORT_TYPE} -L -f - | ${BORG_CALL}; sudo -u ${GIT_USER} ${GITEA_BIN} dump -c ${GITEA_CONFIG} -w ${GITEA_WORKING_DIR} -t ${GITEA_TEMP_DIR} --type ${GITEA_EXPORT_TYPE} -L -f - | ${BORG_CALL};
) 2>&1 | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' # remove all ESC strings ) 2>&1 | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' # remove all ESC strings
fi; fi;

View File

@@ -1,8 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# allow variables in printf format string
# shellcheck disable=SC2059
# Backup MySQL/MariaDB # Backup MySQL/MariaDB
# default is per table dump, can be set to one full dump # default is per table dump, can be set to one full dump
# config override set in borg.backup.mysql.settings # config override set in borg.backup.mysql.settings
@@ -10,7 +7,7 @@
# set last edit date + time # set last edit date + time
MODULE="mysql" MODULE="mysql"
MODULE_VERSION="1.1.5"; MODULE_VERSION="1.1.4";
DIR="${BASH_SOURCE%/*}" DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
@@ -65,11 +62,10 @@ if [ -z "${_MYSQL_OK}" ]; then
fi; fi;
# below is for file name only # below is for file name only
# set DB_VERSION (Distrib n.n.n-type) # set DB_VERSION (Distrib n.n.n-type)
# OLD: mysql Ver 14.14 Distrib 5.7.35, for Linux (x86_64) using EditLine wrapper # NEW: mysql Ver 15.1 Distrib 10.5.12-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper
# NEW: mysql Ver 15.1 Distrib 10.5.12-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper # OLD: mysql Ver 14.14 Distrib 5.7.35, for Linux (x86_64) using EditLine wrapper
# VARIANT: mysql from 11.8.3-MariaDB, client 15.2 for debian-linux-gnu (x86_64) using EditLine wrapper
_DB_VERSION_TYPE=$("${MYSQL_CMD}" --version); _DB_VERSION_TYPE=$("${MYSQL_CMD}" --version);
_DB_VERSION=$(echo "${_DB_VERSION_TYPE}" | sed 's/.*\(Distrib\|from\) \([0-9]\{1,\}\.[0-9]\{1,\}\)\.[0-9]\{1,\}.*/\2/'); _DB_VERSION=$(echo "${_DB_VERSION_TYPE}" | sed 's/.*Distrib \([0-9]\{1,\}\.[0-9]\{1,\}\)\.[0-9]\{1,\}.*/\1/');
DB_VERSION=$(echo "${_DB_VERSION}" | cut -d " " -f 1); DB_VERSION=$(echo "${_DB_VERSION}" | cut -d " " -f 1);
# temporary until correct type detection is set # temporary until correct type detection is set
DB_TYPE="mysql"; DB_TYPE="mysql";
@@ -129,11 +125,11 @@ if [ -n "${DATABASE_FULL_DUMP}" ]; then
echo "Prune repository with keep${KEEP_INFO:1}"; echo "Prune repository with keep${KEEP_INFO:1}";
${BORG_PRUNE}; ${BORG_PRUNE};
fi; fi;
DURATION=$(( $(date +'%s') - LOCAL_START )); DURATION=$[ $(date +'%s')-$LOCAL_START ];
printf "${PRINTF_DB_RUN_TIME_SUB_BLOCK}" "DONE" "all databases" "${MODULE}" "$(convert_time ${DURATION})"; printf "${PRINTF_DB_RUN_TIME_SUB_BLOCK}" "DONE" "all databases" "${MODULE}" "$(convert_time ${DURATION})";
else else
${MYSQL_CMD} ${MYSQL_DB_CONFIG_PARAM} -B -N -e "show databases" | ${MYSQL_CMD} ${MYSQL_DB_CONFIG_PARAM} -B -N -e "show databases" |
while read -r db; do while read db; do
LOCAL_START=$(date +'%s'); LOCAL_START=$(date +'%s');
printf "${PRINTF_DB_SUB_BLOCK}" "DB" "${db}" "${MODULE}"; printf "${PRINTF_DB_SUB_BLOCK}" "DB" "${db}" "${MODULE}";
printf "${PRINTF_SUBEXT_BLOCK}" "BACKUP" "${db}" "$(date +'%F %T')" "${MODULE}"; printf "${PRINTF_SUBEXT_BLOCK}" "BACKUP" "${db}" "$(date +'%F %T')" "${MODULE}";
@@ -151,7 +147,7 @@ else
fi; fi;
exclude=0; exclude=0;
if [ -f "${BASE_FOLDER}${EXCLUDE_FILE}" ]; then if [ -f "${BASE_FOLDER}${EXCLUDE_FILE}" ]; then
while read -r excl_db; do while read excl_db; do
if [ "${db}" = "${excl_db}" ]; then if [ "${db}" = "${excl_db}" ]; then
exclude=1; exclude=1;
break; break;
@@ -181,7 +177,7 @@ else
SCHEMA_ONLY=''; # empty for all SCHEMA_ONLY=''; # empty for all
schema_flag='data'; # or data schema_flag='data'; # or data
if [ -s "${BASE_FOLDER}${SCHEMA_ONLY_FILE}" ]; then if [ -s "${BASE_FOLDER}${SCHEMA_ONLY_FILE}" ]; then
while read -r schema_db; do while read schema_db; do
if [ "${db}" = "${schema_db}" ]; then if [ "${db}" = "${schema_db}" ]; then
SCHEMA_ONLY='--no-data'; SCHEMA_ONLY='--no-data';
schema_flag='schema'; schema_flag='schema';
@@ -221,7 +217,7 @@ else
else else
echo "- [E] ${db}"; echo "- [E] ${db}";
fi; fi;
DURATION=$(( $(date +'%s') - LOCAL_START )); DURATION=$[ $(date +'%s')-$LOCAL_START ];
printf "${PRINTF_DB_RUN_TIME_SUB_BLOCK}" "DONE" "${db}" "${MODULE}" "$(convert_time ${DURATION})"; printf "${PRINTF_DB_RUN_TIME_SUB_BLOCK}" "DONE" "${db}" "${MODULE}" "$(convert_time ${DURATION})";
done; done;
fi; fi;

View File

@@ -9,15 +9,5 @@
# note that with this databases that have been dropped need to be pruned manually # note that with this databases that have been dropped need to be pruned manually
# if 'schema' word is used, only schema data is dumped # if 'schema' word is used, only schema data is dumped
DATABASE_FULL_DUMP=""; DATABASE_FULL_DUMP="";
# override default postgres user and sudo user for all postgres commands # override default postgres user
# All commands must be run as the postgres admin user
DATABASE_USER=""; DATABASE_USER="";
# disable sudo usage by setting to "0", default is to use sudo
DATABASE_USE_SUDO="";
# set the sudo user, if not set postgres or DATABASE_USER is used
DATABASE_SUDO_USER="";
# override port
DATABASE_PORT="";
# override database host, if set to local, localhost or 127.0.0.1 it will use sockets to connect
# for socket connection ident or sudo has to be used
DATABASE_HOST="";

View File

@@ -1,8 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# allow variables in printf format string
# shellcheck disable=SC2059
# Backup PostgreSQL # Backup PostgreSQL
# default is per table dump, can be set to one full dump # default is per table dump, can be set to one full dump
# config override set in borg.backup.pgsql.settings # config override set in borg.backup.pgsql.settings
@@ -10,13 +7,11 @@
# set last edit date + time # set last edit date + time
MODULE="pgsql" MODULE="pgsql"
MODULE_VERSION="1.3.0"; MODULE_VERSION="1.2.7";
DIR="${BASH_SOURCE%/*}" DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
DEFAULT_DATABASE_USE_SUDO="1";
DEFAULT_DATABASE_USER="postgres";
# init system # init system
. "${DIR}/borg.backup.functions.init.sh"; . "${DIR}/borg.backup.functions.init.sh";
@@ -37,41 +32,15 @@ BACKUP_LOCK_FILE="borg.backup.${MODULE}.lock";
# if info print info and then abort run # if info print info and then abort run
. "${DIR}/borg.backup.functions.info.sh"; . "${DIR}/borg.backup.functions.info.sh";
# set db and sudo user
if [ -n "${DATABASE_USER}" ]; then if [ -n "${DATABASE_USER}" ]; then
DB_USER="${DATABASE_USER}"; DB_USER=${DATABASE_USER};
else else
DB_USER="${DEFAULT_DATABASE_USER}"; DB_USER='postgres';
fi; fi;
if [ -n "${DATABASE_SUDO_USER}" ]; then
DB_SUDO_USER="${DATABASE_SUDO_USER}";
else
DB_SUDO_USER="${DB_USER}";
fi;
# set flag if we should use sudo
if [ -n "${DATABASE_USE_SUDO}" ]; then
USE_SUDO="${DATABASE_USE_SUDO}";
else
USE_SUDO="${DEFAULT_DATABASE_USE_SUDO}";
fi;
# sudo command, or none if not requested
SUDO_COMMAND_A=("sudo" "-u" "${DB_SUDO_USER}");
if [ "${USE_SUDO}" -eq "0" ]; then
SUDO_COMMAND_A=()
fi;
# get current pgsql version first # get current pgsql version first
# if first part <10 then user full, else only first part # if first part <10 then user full, else only first part
# eg 9.4 -> 9.4, 12.5 -> 12 # eg 9.4 -> 9.4, 12.5 -> 12
PG_VERSION_CMD=("${SUDO_COMMAND_A[@]}" "psql" "-U" "${DB_USER}" "-d" "template1" "-t" "-A" "-F" "," "-X" "-q" "-c" "select version();"); PG_VERSION=$(pgv=$(psql -U ${DB_USER} -d template1 -t -A -F "," -X -q -c 'select version();' | sed -e 's/^PostgreSQL \([0-9]\{1,\}\.[0-9]\{1,\}\).*/\1/'); if [[ $(echo "${pgv}" | cut -d "." -f 1) -ge 10 ]]; then echo "${pgv}" | cut -d "." -f 1; else echo "${pgv}" | cut -d "." -f 1,2; fi );
PG_VERSION=$(
pgv=$("${PG_VERSION_CMD[@]}" | sed -e 's/^PostgreSQL \([0-9]\{1,\}\.[0-9]\{1,\}\).*/\1/');
if [[ $(echo "${pgv}" | cut -d "." -f 1) -ge 10 ]]; then
echo "${pgv}" | cut -d "." -f 1;
else
echo "${pgv}" | cut -d "." -f 1,2;
fi;
);
_PATH_PG_VERSION=${PG_VERSION}; _PATH_PG_VERSION=${PG_VERSION};
_backup_error=$?; _backup_error=$?;
if [ $_backup_error -ne 0 ] || [ -z "${PG_VERSION}" ]; then if [ $_backup_error -ne 0 ] || [ -z "${PG_VERSION}" ]; then
@@ -81,75 +50,49 @@ if [ $_backup_error -ne 0 ] || [ -z "${PG_VERSION}" ]; then
fi; fi;
# path set per Distribution type and current running DB version # path set per Distribution type and current running DB version
# Debian/Ubuntu: PG_BASE_PATH='/usr/lib/postgresql/';
# Redhat: PG_BASE_PATH='/usr/pgsql-'; # Redhat: PG_BASE_PATH='/usr/pgsql-';
PG_BASE_PATH_LIST=("/usr/lib/postgresql" "/usr/lib64/pgsql"); # AWS 1: PG_BASE_PATH='/usr/lib64/pgsql';
PG_BASE_PATH=""; # Debian: PG_BASE_PATH='/usr/lib/postgresql/';
for path in "${PG_BASE_PATH_LIST[@]}"; do PG_BASE_PATH='/usr/lib/postgresql/';
if [ -f "${path}/${_PATH_PG_VERSION}/bin/psql" ]; then if [ ! -f "${PG_BASE_PATH}${_PATH_PG_VERSION}/bin/psql" ]; then
PG_BASE_PATH="${path}/"; PG_BASE_PATH='/usr/pgsql-';
break; if [ ! -f "${PG_BASE_PATH}${_PATH_PG_VERSION}/bin/psql" ]; then
PG_BASE_PATH='/usr/lib64/pgsql';
_PATH_PG_VERSION=$(echo "${PG_VERSION}" | sed -e 's/\.//');
if [ ! -f "${PG_BASE_PATH}${_PATH_PG_VERSION}/bin/psql" ]; then
echo "[! $(date +'%F %T')] PostgreSQL not found in any paths";
. "${DIR}/borg.backup.functions.close.sh" 1;
exit 1;
fi;
fi; fi;
done; fi;
if [ -z "${PG_BASE_PATH}" ]; then PG_PATH=${PG_BASE_PATH}${_PATH_PG_VERSION}'/bin/';
echo "[! $(date +'%F %T')] PostgreSQL not found in any paths"; PG_PSQL=${PG_PATH}'psql';
. "${DIR}/borg.backup.functions.close.sh" 1; PG_DUMP=${PG_PATH}'pg_dump';
PG_DUMPALL=${PG_PATH}'pg_dumpall';
# check that command are here
if [ ! -f "${PG_PSQL}" ]; then
echo "[! $(date +'%F %T')] psql binary not found in ${PG_PATH}";
. "${DIR}/borg.backup.functions.close.sh" 1;
exit 1; exit 1;
fi; fi;
PG_PATH="${PG_BASE_PATH}${_PATH_PG_VERSION}/bin/"; if [ ! -f "${PG_DUMP}" ]; then
PG_PSQL=("${PG_PATH}psql"); echo "[! $(date +'%F %T')] pg_dump binary not found in ${PG_PATH}";
PG_DUMP=("${PG_PATH}pg_dump");
PG_DUMPALL=("${PG_PATH}pg_dumpall");
PG_ERROR=0
# check that command are here
if [ ! -f "${PG_PSQL[0]}" ]; then
echo "[!] ($(date +'%F %T')) psql binary not found in ${PG_PATH}";
PG_ERROR=1;
fi;
if [ ! -f "${PG_DUMP[0]}" ]; then
echo "[!] ($(date +'%F %T')) pg_dump binary not found in ${PG_PATH}";
PG_ERROR=1;
fi;
if [ ! -f "${PG_DUMPALL[0]}" ]; then
echo "[!] ($(date +'%F %T')) pg_dumpall binary not found in ${PG_PATH}";
PG_ERROR=1;
fi;
if [ ${PG_ERROR} -ne 0 ]; then
. "${DIR}/borg.backup.functions.close.sh" 1; . "${DIR}/borg.backup.functions.close.sh" 1;
exit ${PG_ERROR}; exit 1;
fi; fi;
# prefix with sudo, if sudo command is requested if [ ! -f "${PG_DUMPALL}" ]; then
if [ "${USE_SUDO}" -ne "0" ]; then echo "[! $(date +'%F %T')] pg_dumpall binary not found in ${PG_PATH}";
PG_PSQL=("${SUDO_COMMAND_A[@]}" "${PG_PSQL[@]}"); . "${DIR}/borg.backup.functions.close.sh" 1;
PG_DUMP=("${SUDO_COMMAND_A[@]}" "${PG_DUMP[@]}"); exit 1;
PG_DUMPALL=("${SUDO_COMMAND_A[@]}" "${PG_DUMPALL[@]}");
fi; fi;
DB_VERSION=${PG_VERSION}; DB_VERSION=${PG_VERSION};
# override for port or host name # TODO override port/host info
if [ -n "${DATABASE_PORT}" ] && [[ "${DATABASE_PORT}" =~ ${REGEX_PORT} ]]; then DB_PORT='5432';
DB_PORT="${DATABASE_PORT}"; DB_HOST='local'; # or <host>
else CONN_DB_HOST=''; # -h <host>
DB_PORT="5432"; CONN_DB_PORT=''; # -p <port>
fi;
if [ -n "${DATABASE_HOST}" ]; then
DB_HOST="${DATABASE_HOST}";
else
DB_HOST="local";
fi;
# use socket for local
if [ "${DB_HOST}" = "local" ] || [ "${DB_HOST}" = "localhost" ] || [ "${DB_HOST}" = "127.0.0.1" ]; then
DB_HOST='local';
CONN_DB_HOST=();
CONN_DB_PORT=();
else
CONN_DB_HOST=("-p" "${DB_HOST}"); # -h <host>
CONN_DB_PORT=("-h" "${DB_HOSTNAME}"); # -p <port>
fi;
# now add user, con, host to all commands
PG_PSQL=("${PG_PSQL[@]}" "-U" "${DB_USER}" "${CONN_DB_HOST[@]}" "${CONN_DB_PORT[@]}");
PG_DUMP=("${PG_DUMP[@]}" "-U" "${DB_USER}" "${CONN_DB_HOST[@]}" "${CONN_DB_PORT[@]}");
PG_DUMPALL=("${PG_DUMPALL[@]}" "-U" "${DB_USER}" "${CONN_DB_HOST[@]}" "${CONN_DB_PORT[@]}");
# ALL IN ONE FILE or PER DATABASE FLAG # ALL IN ONE FILE or PER DATABASE FLAG
if [ -n "${DATABASE_FULL_DUMP}" ]; then if [ -n "${DATABASE_FULL_DUMP}" ]; then
@@ -167,25 +110,17 @@ if [ -n "${DATABASE_FULL_DUMP}" ]; then
BACKUP_SET_PREFIX="${MODULE},all-"; BACKUP_SET_PREFIX="${MODULE},all-";
BACKUP_SET_NAME="${ONE_TIME_TAG}${BACKUP_SET_PREFIX}${schema_flag}-${BACKUP_SET}"; BACKUP_SET_NAME="${ONE_TIME_TAG}${BACKUP_SET_PREFIX}${schema_flag}-${BACKUP_SET}";
# borg call # borg call
BORG_CALL=$( BORG_CALL=$(echo "${_BORG_CALL}" | sed -e "s/##FILENAME##/${FILENAME}/" | sed -e "s/##BACKUP_SET##/${BACKUP_SET_NAME}/");
echo "${_BORG_CALL}" | BORG_PRUNE=$(echo "${_BORG_PRUNE}" | sed -e "s/##BACKUP_SET_PREFIX##/${BACKUP_SET_PREFIX}/");
sed -e "s/##FILENAME##/${FILENAME}/" |
sed -e "s/##BACKUP_SET##/${BACKUP_SET_NAME}/"
);
BORG_PRUNE=$(
echo "${_BORG_PRUNE}" |
sed -e "s/##BACKUP_SET_PREFIX##/${BACKUP_SET_PREFIX}/"
);
PG_DUMPALL_CMD=("${PG_DUMPALL[@]}" "${SCHEMA_ONLY}" "-c");
if [ ${DEBUG} -eq 1 ] || [ ${DRYRUN} -eq 1 ]; then if [ ${DEBUG} -eq 1 ] || [ ${DRYRUN} -eq 1 ]; then
echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";"; echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";";
echo "${PG_DUMPALL_CMD[*]} | ${BORG_CALL}"; echo "${PG_DUMPALL} -U ${DB_USER} ${CONN_DB_HOST} ${CONN_DB_PORT} ${SCHEMA_ONLY} -c | ${BORG_CALL}";
if [ -z "${ONE_TIME_TAG}" ]; then if [ -z "${ONE_TIME_TAG}" ]; then
echo "${BORG_PRUNE}"; echo "${BORG_PRUNE}";
fi; fi;
fi; fi;
if [ ${DRYRUN} -eq 0 ]; then if [ ${DRYRUN} -eq 0 ]; then
"${PG_DUMPALL_CMD[@]}" | ${BORG_CALL}; $(${PG_DUMPALL} -U ${DB_USER} ${CONN_DB_HOST} ${CONN_DB_PORT} ${SCHEMA_ONLY} -c | ${BORG_CALL});
_backup_error=$?; _backup_error=$?;
if [ $_backup_error -ne 0 ]; then if [ $_backup_error -ne 0 ]; then
echo "[! $(date +'%F %T')] Backup creation failed for full dump with error code: ${_backup_error}"; echo "[! $(date +'%F %T')] Backup creation failed for full dump with error code: ${_backup_error}";
@@ -198,7 +133,7 @@ if [ -n "${DATABASE_FULL_DUMP}" ]; then
echo "Prune repository with keep${KEEP_INFO:1}"; echo "Prune repository with keep${KEEP_INFO:1}";
${BORG_PRUNE}; ${BORG_PRUNE};
fi; fi;
DURATION=$(( $(date +'%s') - LOCAL_START )); DURATION=$[ $(date +'%s')-$LOCAL_START ];
printf "${PRINTF_DB_RUN_TIME_SUB_BLOCK}" "DONE" "all databases" "${MODULE}" "$(convert_time ${DURATION})"; printf "${PRINTF_DB_RUN_TIME_SUB_BLOCK}" "DONE" "all databases" "${MODULE}" "$(convert_time ${DURATION})";
else else
# dump globals first # dump globals first
@@ -212,25 +147,17 @@ else
BACKUP_SET_PREFIX="${MODULE},${db}-"; BACKUP_SET_PREFIX="${MODULE},${db}-";
BACKUP_SET_NAME="${ONE_TIME_TAG}${BACKUP_SET_PREFIX}${schema_flag}-${BACKUP_SET}"; BACKUP_SET_NAME="${ONE_TIME_TAG}${BACKUP_SET_PREFIX}${schema_flag}-${BACKUP_SET}";
# borg call # borg call
BORG_CALL=$( BORG_CALL=$(echo "${_BORG_CALL}" | sed -e "s/##FILENAME##/${FILENAME}/" | sed -e "s/##BACKUP_SET##/${BACKUP_SET_NAME}/");
echo "${_BORG_CALL}" | BORG_PRUNE=$(echo "${_BORG_PRUNE}" | sed -e "s/##BACKUP_SET_PREFIX##/${BACKUP_SET_PREFIX}/");
sed -e "s/##FILENAME##/${FILENAME}/" |
sed -e "s/##BACKUP_SET##/${BACKUP_SET_NAME}/"
);
BORG_PRUNE=$(
echo "${_BORG_PRUNE}" |
sed -e "s/##BACKUP_SET_PREFIX##/${BACKUP_SET_PREFIX}/"
);
PG_DUMPALL_CMD=("${PG_DUMPALL[@]}" "--globals-only");
if [ ${DEBUG} -eq 1 ] || [ ${DRYRUN} -eq 1 ]; then if [ ${DEBUG} -eq 1 ] || [ ${DRYRUN} -eq 1 ]; then
echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";"; echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";";
echo "${PG_DUMPALL_CMD[*]} | ${BORG_CALL}"; echo "${PG_DUMPALL} -U ${DB_USER} ${CONN_DB_HOST} ${CONN_DB_PORT} --globals-only | ${BORG_CALL}";
if [ -z "${ONE_TIME_TAG}" ]; then if [ -z "${ONE_TIME_TAG}" ]; then
echo "${BORG_PRUNE}"; echo "${BORG_PRUNE}";
fi; fi;
fi; fi;
if [ ${DRYRUN} -eq 0 ]; then if [ ${DRYRUN} -eq 0 ]; then
"${PG_DUMPALL_CMD[@]}" | ${BORG_CALL}; ${PG_DUMPALL} -U ${DB_USER} ${CONN_DB_HOST} ${CONN_DB_PORT} --globals-only | ${BORG_CALL};
_backup_error=$?; _backup_error=$?;
if [ $_backup_error -ne 0 ]; then if [ $_backup_error -ne 0 ]; then
echo "[! $(date +'%F %T')] Backup creation failed for ${db} dump with error code: ${_backup_error}"; echo "[! $(date +'%F %T')] Backup creation failed for ${db} dump with error code: ${_backup_error}";
@@ -243,25 +170,21 @@ else
echo "Prune repository with keep${KEEP_INFO:1}"; echo "Prune repository with keep${KEEP_INFO:1}";
${BORG_PRUNE}; ${BORG_PRUNE};
fi; fi;
DURATION=$(( $(date +'%s') - LOCAL_START )); DURATION=$[ $(date +'%s')-$LOCAL_START ];
printf "${PRINTF_DB_RUN_TIME_SUB_BLOCK}" "DONE" "${db}" "${MODULE}" "$(convert_time ${DURATION})"; printf "${PRINTF_DB_RUN_TIME_SUB_BLOCK}" "DONE" "${db}" "${MODULE}" "$(convert_time ${DURATION})";
# get list of tables # get list of tables
GET_DB_CMD=( for owner_db in $(${PG_PSQL} -U ${DB_USER} ${CONN_DB_HOST} ${CONN_DB_PORT} -d template1 -t -A -F "," -X -q -c "SELECT pg_catalog.pg_get_userbyid(datdba) AS owner, datname, pg_catalog.pg_encoding_to_char(encoding) AS encoding FROM pg_catalog.pg_database WHERE datname "\!"~ 'template(0|1)' ORDER BY datname;"); do
"${PG_PSQL[@]}" "-d" "template1" "-t" "-A" "-F" "," "-X" "-q" "-c"
"SELECT pg_catalog.pg_get_userbyid(datdba) AS owner, datname, pg_catalog.pg_encoding_to_char(encoding) AS encoding FROM pg_catalog.pg_database WHERE datname !~ 'template(0|1)' ORDER BY datname;"
);
for owner_db in $("${GET_DB_CMD[@]}"); do
LOCAL_START=$(date +'%s'); LOCAL_START=$(date +'%s');
# get the user who owns the DB too # get the user who owns the DB too
owner=$(echo "${owner_db}" | cut -d "," -f 1); owner=$(echo ${owner_db} | cut -d "," -f 1);
db=$(echo "${owner_db}" | cut -d "," -f 2); db=$(echo ${owner_db} | cut -d "," -f 2);
encoding=$(echo "${owner_db}" | cut -d "," -f 3); encoding=$(echo ${owner_db} | cut -d "," -f 3);
printf "${PRINTF_DB_SUB_BLOCK}" "DB" "${db}" "${MODULE}"; printf "${PRINTF_DB_SUB_BLOCK}" "DB" "${db}" "${MODULE}";
printf "${PRINTF_SUBEXT_BLOCK}" "BACKUP" "${db}" "$(date +'%F %T')" "${MODULE}"; printf "${PRINTF_SUBEXT_BLOCK}" "BACKUP" "${db}" "$(date +'%F %T')" "${MODULE}";
include=0; include=0;
if [ -s "${BASE_FOLDER}${INCLUDE_FILE}" ]; then if [ -s "${BASE_FOLDER}${INCLUDE_FILE}" ]; then
while read -r incl_db; do while read incl_db; do
if [ "${db}" = "${incl_db}" ]; then if [ "${db}" = "${incl_db}" ]; then
include=1; include=1;
break; break;
@@ -272,7 +195,7 @@ else
fi; fi;
exclude=0; exclude=0;
if [ -f "${BASE_FOLDER}${EXCLUDE_FILE}" ]; then if [ -f "${BASE_FOLDER}${EXCLUDE_FILE}" ]; then
while read -r excl_db; do while read excl_db; do
if [ "${db}" = "${excl_db}" ]; then if [ "${db}" = "${excl_db}" ]; then
exclude=1; exclude=1;
break; break;
@@ -280,15 +203,17 @@ else
done<"${BASE_FOLDER}${EXCLUDE_FILE}"; done<"${BASE_FOLDER}${EXCLUDE_FILE}";
fi; fi;
if [ ${include} -eq 1 ] && [ ${exclude} -eq 0 ]; then if [ ${include} -eq 1 ] && [ ${exclude} -eq 0 ]; then
PG_DUMP_CMD=("${PG_DUMP[@]}" "-c" "--format=c");
# set dump type # set dump type
SCHEMA_ONLY='';
schema_flag=''; # schema or data schema_flag=''; # schema or data
# schema exclude over data exclude, can't have both # schema exclude over data exclude, can't have both
if [ -s "${BASE_FOLDER}${SCHEMA_ONLY_FILE}" ]; then if [ -s "${BASE_FOLDER}${SCHEMA_ONLY_FILE}" ]; then
# default is data dump # default is data dump
SCHEMA_ONLY='';
schema_flag='data'; schema_flag='data';
while read -r schema_db; do while read schema_db; do
if [ "${db}" = "${schema_db}" ]; then if [ "${db}" = "${schema_db}" ]; then
SCHEMA_ONLY='-s';
schema_flag='schema'; schema_flag='schema';
# skip out # skip out
break; break;
@@ -296,9 +221,11 @@ else
done<"${BASE_FOLDER}${SCHEMA_ONLY_FILE}"; done<"${BASE_FOLDER}${SCHEMA_ONLY_FILE}";
elif [ -s "${BASE_FOLDER}${DATA_ONLY_FILE}" ]; then elif [ -s "${BASE_FOLDER}${DATA_ONLY_FILE}" ]; then
# default to schema, unless in data list # default to schema, unless in data list
SCHEMA_ONLY='-s';
schema_flag='schema'; schema_flag='schema';
while read -r data_db; do while read data_db; do
if [ "${db}" = "${data_db}" ]; then if [ "${db}" = "${data_db}" ]; then
SCHEMA_ONLY='';
schema_flag='data'; schema_flag='data';
# skip out # skip out
break; break;
@@ -307,13 +234,9 @@ else
fi; fi;
# if nothing is set, default to data # if nothing is set, default to data
if [ -z "${schema_flag}" ]; then if [ -z "${schema_flag}" ]; then
SCHEMA_ONLY=''
schema_flag="data"; schema_flag="data";
fi; fi;
# set schema flag if schmea only is requested
if [ $schema_flag = "schema" ]; then
PG_DUMP_CMD+=("-s");
fi;
PG_DUMP_CMD+=("${db}");
# Filename # Filename
# Database.User.Encoding.pgsql|data|schema-Version_Host_Port_YearMonthDay_HourMinute_Counter.Fromat(c).sql # Database.User.Encoding.pgsql|data|schema-Version_Host_Port_YearMonthDay_HourMinute_Counter.Fromat(c).sql
FILENAME="${db}.${owner}.${encoding}.${schema_flag}-${DB_VERSION}_${DB_HOST}_${DB_PORT}.c.sql" FILENAME="${db}.${owner}.${encoding}.${schema_flag}-${DB_VERSION}_${DB_HOST}_${DB_PORT}.c.sql"
@@ -322,25 +245,18 @@ else
# backup set: # backup set:
BACKUP_SET_NAME="${ONE_TIME_TAG}${BACKUP_SET_PREFIX}${schema_flag}-${BACKUP_SET}"; BACKUP_SET_NAME="${ONE_TIME_TAG}${BACKUP_SET_PREFIX}${schema_flag}-${BACKUP_SET}";
# borg call # borg call
BORG_CALL=$( BORG_CALL=$(echo "${_BORG_CALL}" | sed -e "s/##FILENAME##/${FILENAME}/" | sed -e "s/##BACKUP_SET##/${BACKUP_SET_NAME}/");
echo "${_BORG_CALL}" |
sed -e "s/##FILENAME##/${FILENAME}/" |
sed -e "s/##BACKUP_SET##/${BACKUP_SET_NAME}/"
);
# borg prune # borg prune
BORG_PRUNE=$( BORG_PRUNE=$(echo "${_BORG_PRUNE}" | sed -e "s/##BACKUP_SET_PREFIX##/${BACKUP_SET_PREFIX}/");
echo "${_BORG_PRUNE}" |
sed -e "s/##BACKUP_SET_PREFIX##/${BACKUP_SET_PREFIX}/"
);
if [ ${DEBUG} -eq 1 ] || [ ${DRYRUN} -eq 1 ]; then if [ ${DEBUG} -eq 1 ] || [ ${DRYRUN} -eq 1 ]; then
echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";"; echo "export BORG_BASE_DIR=\"${BASE_FOLDER}\";";
echo "${PG_DUMP_CMD[*]} | ${BORG_CALL}"; echo "${PG_DUMP} -U ${DB_USER} ${CONN_DB_HOST} ${CONN_DB_PORT} -c ${SCHEMA_ONLY} --format=c ${db} | ${BORG_CALL}";
if [ -z "${ONE_TIME_TAG}" ]; then if [ -z "${ONE_TIME_TAG}" ]; then
echo "${BORG_PRUNE}"; echo "${BORG_PRUNE}";
fi; fi;
fi; fi;
if [ ${DRYRUN} -eq 0 ]; then if [ ${DRYRUN} -eq 0 ]; then
"${PG_DUMP_CMD[@]}" | ${BORG_CALL}; ${PG_DUMP} -U ${DB_USER} ${CONN_DB_HOST} ${CONN_DB_PORT} -c ${SCHEMA_ONLY} --format=c ${db} | ${BORG_CALL};
_backup_error=$?; _backup_error=$?;
if [ $_backup_error -ne 0 ]; then if [ $_backup_error -ne 0 ]; then
echo "[! $(date +'%F %T')] Backup creation failed for ${db} dump with error code: ${_backup_error}"; echo "[! $(date +'%F %T')] Backup creation failed for ${db} dump with error code: ${_backup_error}";
@@ -356,7 +272,7 @@ else
else else
echo "- [E] ${db}"; echo "- [E] ${db}";
fi; fi;
DURATION=$(( $(date +'%s') - LOCAL_START )); DURATION=$[ $(date +'%s')-$LOCAL_START ];
printf "${PRINTF_DB_RUN_TIME_SUB_BLOCK}" "DONE" "${db}" "${MODULE}" "$(convert_time ${DURATION})"; printf "${PRINTF_DB_RUN_TIME_SUB_BLOCK}" "DONE" "${db}" "${MODULE}" "$(convert_time ${DURATION})";
done; done;
fi; fi;

View File

@@ -13,21 +13,20 @@ TARGET_PORT="";
TARGET_BORG_PATH=""; TARGET_BORG_PATH="";
# folder where the backup folder will be created # folder where the backup folder will be created
TARGET_FOLDER=""; TARGET_FOLDER="";
# the backup file (folder) for this backup, avoid dynamic variables in here, must end with .borg # the backup file (folder) for this host $(hostname), must end with .borg
BACKUP_FILE=""; BACKUP_FILE="";
# compression settings (none, lz4, zstd, zlib, lzma) # compression settings (empty for none, lz4, zstd, zlib, lzma)
# empty is default zstd # level, if empty then default, else number between 0 and 9, or 1 to 22 for zstd
# default is zstd, 3
COMPRESSION=""; COMPRESSION="";
# compression level, if empty then default 3, else number between 0 and 9, or 1 to 22 for zstd
COMPRESSION_LEVEL=""; COMPRESSION_LEVEL="";
# encryption settings: # encryption settings:
# SHA-256: 'none', 'authenticated', 'repokey', 'keyfile' # SHA-256: 'none', 'authenticated', 'repokey', 'keyfile'
# BLAKE2b: 'authenticated-blake2', 'repokey-blake2', 'keyfile-blake2' # BLAKE2b: 'authenticated-blake2', 'repokey-blake2', 'keyfile-blake2'
# Note: none does not encrypt and is not recommended # Note: none or empty does not encrypt
# Default is keyfile # Blank passwords allowed for only key (if used, use keyfile)
# Blank passwords allowed for only keyfile, else passwords have to be set via BORG_PASSPHRASE or BORG_PASSCOMMAND
# See: http://borgbackup.readthedocs.io/en/stable/faq.html#how-can-i-specify-the-encryption-passphrase-programmatically # See: http://borgbackup.readthedocs.io/en/stable/faq.html#how-can-i-specify-the-encryption-passphrase-programmatically
ENCRYPTION="" ENCRYPTION="";
# force repository verify, default is off, set to true for verify on every run # force repository verify, default is off, set to true for verify on every run
FORCE_VERIFY=""; FORCE_VERIFY="";
# compact interval, only if using borg 1.2 or higher # compact interval, only if using borg 1.2 or higher

View File

@@ -1,12 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# allow variables in printf format string
# shellcheck disable=SC2059
# Backup zabbix config and settings only # Backup zabbix config and settings only
MODULE="zabbix" MODULE="zabbix"
MODULE_VERSION="1.1.4"; MODULE_VERSION="1.1.3";
DIR="${BASH_SOURCE%/*}" DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
@@ -55,7 +52,7 @@ if [ ! -f "${ZABBIX_DUMP_BIN}" ]; then
exit 1; exit 1;
fi; fi;
# -i (ignore)/ -f (backup) # -i (ignore)/ -f (backup)
if [ ! -z "${ZABBIX_UNKNOWN_TABLES}" ]; then if [ -n "${ZABBIX_UNKNOWN_TABLES}" ]; then
OPT_ZABBIX_UNKNOWN_TABLES="-f"; OPT_ZABBIX_UNKNOWN_TABLES="-f";
else else
OPT_ZABBIX_UNKNOWN_TABLES="-i"; OPT_ZABBIX_UNKNOWN_TABLES="-i";

View File

@@ -80,7 +80,7 @@ if [ ${UMOUNT} -eq 0 ]; then
. ${BASE_FOLDER}${SETTINGS_FILE} . ${BASE_FOLDER}${SETTINGS_FILE}
# set the borg backup file base on the settings data # set the borg backup file base on the settings data
# if we have user/host then we build the ssh command # if we have user/host then we build the ssh command
if [ ! -z "${TARGET_USER}" ] && [ ! -z "${TARGET_HOST}" ]; then if [ -n "${TARGET_USER}" ] && [ -n "${TARGET_HOST}" ]; then
TARGET_SERVER=${TARGET_USER}"@"${TARGET_HOST}":"; TARGET_SERVER=${TARGET_USER}"@"${TARGET_HOST}":";
fi; fi;
REPOSITORY=${TARGET_SERVER}${TARGET_FOLDER}${BACKUP_FILE}; REPOSITORY=${TARGET_SERVER}${TARGET_FOLDER}${BACKUP_FILE};
@@ -90,7 +90,7 @@ if [ ${UMOUNT} -eq 0 ]; then
# check that the repostiory exists # check that the repostiory exists
REPOSITORY_OK=0; REPOSITORY_OK=0;
if [ ! -z "${TARGET_SERVER}" ]; then if [ -n "${TARGET_SERVER}" ]; then
# remove trailing : for this # remove trailing : for this
TARGET_SERVER=${TARGET_SERVER/:}; TARGET_SERVER=${TARGET_SERVER/:};
# use ssh command to check remote existense # use ssh command to check remote existense