Compare commits

..

11 Commits

Author SHA1 Message Date
Clemens Schwaighofer
32a75626bb Version number fixes 2025-12-03 15:49:45 +09:00
Clemens Schwaighofer
12ff648504 PostgreSQL backup hat wrong table selection 2025-12-03 15:31:55 +09:00
Clemens Schwaighofer
69be76e34c Default settings file update for BACKUP_FILE 2025-12-03 14:48:54 +09:00
Clemens Schwaighofer
4a95049d4f Default config file info update 2025-12-03 14:12:24 +09:00
Clemens Schwaighofer
d0e96a82e0 Clean up PostgreSQL call, clean up varify error block, ERR trap fixes
ERR trap is now returning last error so $? can bet used
Verify catches $? error now for INIT repository check
2025-12-03 14:00:49 +09:00
Clemens Schwaighofer
e5c5df6013 Fix error trap with return error to STD ERR 2025-12-03 13:08:35 +09:00
Clemens Schwaighofer
6023fac636 Fix trap clanup that also trapped ERR 2025-12-03 13:05:11 +09:00
Clemens Schwaighofer
9e0db8bae9 Fix the error trap with exit to stop calling it again with itself 2025-12-03 13:03:46 +09:00
Clemens Schwaighofer
8a8de773df Change borg repository verify error message collection 2025-12-03 12:47:49 +09:00
Clemens Schwaighofer
f41dd1b723 Change verify command error check 2025-12-03 12:45:56 +09:00
Clemens Schwaighofer
c5d88a64e1 Fix last command check for verify 2025-12-03 12:43:52 +09:00
7 changed files with 50 additions and 56 deletions

View File

@@ -5,11 +5,14 @@ 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 cleanup SIGINT SIGTERM ERR trap _catch ERR
trap error_trap ERR trap _cleanup SIGINT SIGTERM
cleanup() { _cleanup() {
# script cleanup here # script cleanup here
echo "Script abort: $? @LINE: $(caller)"; echo "Script abort: $? @LINE: $(caller)";
# unset exported vars # unset exported vars
@@ -17,9 +20,9 @@ cleanup() {
# end trap # end trap
trap - SIGINT SIGTERM trap - SIGINT SIGTERM
} }
error_trap() { _catch() {
echo "Some part of the script failed with an error: $? @LINE: $(caller)"; local last_exit_code=$?;
trap - ERR 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;
@@ -29,7 +32,7 @@ function version {
} }
# version for all general files # version for all general files
VERSION="4.5.4"; VERSION="4.7.2";
# borg version and borg comamnd # borg version and borg comamnd
BORG_VERSION=""; BORG_VERSION="";
@@ -90,7 +93,7 @@ REGEX="";
REGEX_COMMENT="^[\ \t]*#"; REGEX_COMMENT="^[\ \t]*#";
REGEX_GLOB='\*'; REGEX_GLOB='\*';
REGEX_NUMERIC="^[0-9]{1,2}$"; REGEX_NUMERIC="^[0-9]{1,2}$";
REGEX_ERROR="^Some part of the script failed with an error:"; 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;

View File

@@ -294,25 +294,15 @@ COMMAND_INFO="${COMMAND_EXPORT}${BORG_COMMAND} info ${OPT_REMOTE} ${REPOSITORY}"
# MARK: VERIFY / INFO # 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 [ -n "${TARGET_SERVER}" ]; then if [ "${DEBUG}" -eq 1 ]; then
if [ "${DEBUG}" -eq 1 ]; then echo "${BORG_COMMAND} info ${OPT_REMOTE} ${REPOSITORY} 2>&1 ";
echo "${BORG_COMMAND} info ${OPT_REMOTE} ${REPOSITORY} 2>&1|grep \"Repository ID:\""; fi;
fi; # use borg info and verify if it returns "Repository ID:" in the first line
# use borg info and verify if it returns "Repository ID:" in the first line REPO_VERIFY=$(${BORG_COMMAND} info ${OPT_REMOTE} "${REPOSITORY}" 2>&1);
REPO_VERIFY=$(${BORG_COMMAND} info ${OPT_REMOTE} "${REPOSITORY}" 2>&1); __last_error=$?;
if ! $?; then # on any error in verify command force new INIT
echo "[!] Repository verify error: ${REPO_VERIFY}"; if [[ $__last_error -ne 0 ]]; then
REPO_VERIFY=""; echo "[!] Repository verify error: ${REPO_VERIFY}";
else
REPO_VERIFY=$(echo "${REPO_VERIFY}" | grep "Repository ID:");
fi;
# | grep "Repository ID:"
# this is currently a hack to work round the error code in borg info
# this checks if REPO_VERIFY holds this error message and then starts init
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
@@ -369,7 +359,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;
@@ -378,7 +368,7 @@ 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;

View File

@@ -6,7 +6,7 @@
# 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.1"; MODULE_VERSION="1.2.2";
DIR="${BASH_SOURCE%/*}" DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi

View File

@@ -10,7 +10,7 @@
# set last edit date + time # set last edit date + time
MODULE="mysql" MODULE="mysql"
MODULE_VERSION="1.1.4"; MODULE_VERSION="1.1.5";
DIR="${BASH_SOURCE%/*}" DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi

View File

@@ -1,5 +1,8 @@
#!/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
@@ -7,7 +10,7 @@
# set last edit date + time # set last edit date + time
MODULE="pgsql" MODULE="pgsql"
MODULE_VERSION="1.2.7"; MODULE_VERSION="1.2.8";
DIR="${BASH_SOURCE%/*}" DIR="${BASH_SOURCE%/*}"
@@ -32,7 +35,7 @@ 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";
if [ ! -z "${DATABASE_USER}" ]; then if [ -n "${DATABASE_USER}" ]; then
DB_USER=${DATABASE_USER}; DB_USER=${DATABASE_USER};
else else
DB_USER='postgres'; DB_USER='postgres';
@@ -95,7 +98,7 @@ CONN_DB_HOST=''; # -h <host>
CONN_DB_PORT=''; # -p <port> CONN_DB_PORT=''; # -p <port>
# ALL IN ONE FILE or PER DATABASE FLAG # ALL IN ONE FILE or PER DATABASE FLAG
if [ ! -z "${DATABASE_FULL_DUMP}" ]; then if [ -n "${DATABASE_FULL_DUMP}" ]; then
SCHEMA_ONLY=''; SCHEMA_ONLY='';
schema_flag='data'; schema_flag='data';
if [ "${DATABASE_FULL_DUMP}" = "schema" ]; then if [ "${DATABASE_FULL_DUMP}" = "schema" ]; then
@@ -120,7 +123,7 @@ if [ ! -z "${DATABASE_FULL_DUMP}" ]; then
fi; fi;
fi; fi;
if [ ${DRYRUN} -eq 0 ]; then if [ ${DRYRUN} -eq 0 ]; then
$(${PG_DUMPALL} -U ${DB_USER} ${CONN_DB_HOST} ${CONN_DB_PORT} ${SCHEMA_ONLY} -c | ${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}";
@@ -133,7 +136,7 @@ if [ ! -z "${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
@@ -170,21 +173,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
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 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
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 incl_db; do while read -r incl_db; do
if [ "${db}" = "${incl_db}" ]; then if [ "${db}" = "${incl_db}" ]; then
include=1; include=1;
break; break;
@@ -195,7 +198,7 @@ else
fi; fi;
exclude=0; exclude=0;
if [ -f "${BASE_FOLDER}${EXCLUDE_FILE}" ]; then if [ -f "${BASE_FOLDER}${EXCLUDE_FILE}" ]; then
while read excl_db; do while read -r excl_db; do
if [ "${db}" = "${excl_db}" ]; then if [ "${db}" = "${excl_db}" ]; then
exclude=1; exclude=1;
break; break;
@@ -211,7 +214,7 @@ else
# default is data dump # default is data dump
SCHEMA_ONLY=''; SCHEMA_ONLY='';
schema_flag='data'; schema_flag='data';
while read schema_db; do while read -r schema_db; do
if [ "${db}" = "${schema_db}" ]; then if [ "${db}" = "${schema_db}" ]; then
SCHEMA_ONLY='-s'; SCHEMA_ONLY='-s';
schema_flag='schema'; schema_flag='schema';
@@ -223,7 +226,7 @@ else
# default to schema, unless in data list # default to schema, unless in data list
SCHEMA_ONLY='-s'; SCHEMA_ONLY='-s';
schema_flag='schema'; schema_flag='schema';
while read data_db; do while read -r data_db; do
if [ "${db}" = "${data_db}" ]; then if [ "${db}" = "${data_db}" ]; then
SCHEMA_ONLY=''; SCHEMA_ONLY='';
schema_flag='data'; schema_flag='data';
@@ -256,7 +259,7 @@ else
fi; fi;
fi; fi;
if [ ${DRYRUN} -eq 0 ]; then if [ ${DRYRUN} -eq 0 ]; then
${PG_DUMP} -U ${DB_USER} ${CONN_DB_HOST} ${CONN_DB_PORT} -c ${SCHEMA_ONLY} --format=c ${db} | ${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}";
@@ -272,7 +275,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,19 @@ 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 host $(hostname), must end with .borg # the backup file (folder) for this backup, avoid dynamic variables in here, must end with .borg
BACKUP_FILE=""; BACKUP_FILE="";
# compression settings (empty for none, lz4, zstd, zlib, lzma) # compression settings (none, lz4, zstd, zlib, lzma)
# level, if empty then default, else number between 0 and 9, or 1 to 22 for zstd # empty is default 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 # Note: none does not encrypt and is not recommended
# Blank passwords allowed for only key (if used, use keyfile)
# Default is keyfile # Default is keyfile
# passwords have to be set via BORG_PASSPHRASE or BORG_PASSCOMMAND # Blank passwords allowed for only keyfile, else passwords have to be set via BORG_PASSPHRASE or BORG_PASSCOMMAND
# keyfile can have blank passwords
# 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

View File

@@ -6,7 +6,7 @@
# Backup zabbix config and settings only # Backup zabbix config and settings only
MODULE="zabbix" MODULE="zabbix"
MODULE_VERSION="1.1.3"; 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