diff --git a/src/borgwrapper.sh b/src/borgwrapper.sh index c5a2084..ca88dfd 100755 --- a/src/borgwrapper.sh +++ b/src/borgwrapper.sh @@ -92,12 +92,44 @@ borg_exec () { ${BORG} "$@" } +pre_backup_cmd () { + [[ -n ${PRE_BACKUP_CMD} ]] || return + echo "Running pre backup command: ${PRE_BACKUP_CMD[@]}" + "${PRE_BACKUP_CMD[@]}" +} + +post_backup_cmd () { + [[ -n ${POST_BACKUP_CMD} ]] || return + echo "Running post backup command: ${POST_BACKUP_CMD[@]}" + "${POST_BACKUP_CMD[@]}" +} + +post_verify_cmd () { + [[ -n ${POST_VERIFY_CMD} ]] || return + echo "Running post verify command: ${POST_VERIFY_CMD[@]}" + "${POST_VERIFY_CMD[@]}" +} + +exit_backup () { + post_backup_cmd + exit_clean $1 +} + +exit_verify () { + post_verify_cmd + exit_clean $1 +} + +exit_clean () { + trap - ERR INT TERM + exit $1 +} + trap 'error_handler ${LINENO} $?' ERR INT TERM set -o errtrace -o pipefail - -# Default parameters +# Default options CONFIG="/etc/borgwrapper/config.sh" while getopts ":c:" OPT; do @@ -118,13 +150,25 @@ MODE="${1}" source "${CONFIG}" || exit 1 export BORG_PASSPHRASE +# Global defaults +[[ -z ${LOCKFILE} ]] && LOCKFILE="/var/lock/borgwrapper.lock" +[[ -z ${BORG} ]] && BORG="/usr/bin/borg" + +# Ensure this is the only instance of borgwrapper running +[[ "${FLOCKER}" != "$0" ]] && exec env FLOCKER="$0" flock -en "${LOCKFILE}" "$0" "$@" || true + if [[ ${MODE} == "init" ]]; then borg_init elif [[ ${MODE} == "backup" ]]; then + trap 'exit_backup $?' ERR INT TERM + pre_backup_cmd borg_backup borg_prune + exit_backup 0 elif [[ ${MODE} == "verify" ]]; then + trap 'exit_verify $?' ERR INT TERM borg_verify + exit_verify 0 elif [[ ${MODE} == "unlock" ]]; then borg_unlock elif [[ ${MODE} == "exec" ]]; then @@ -140,5 +184,4 @@ else exit 1 fi - -trap - ERR INT TERM +exit_clean 0 diff --git a/src/config.sh.example b/src/config.sh.example index e3e009c..e964f1e 100644 --- a/src/config.sh.example +++ b/src/config.sh.example @@ -1,4 +1,4 @@ -BORG="/usr/bin/borg" +#BORG="/usr/bin/borg" BORG_REPO="user@reposerver:/srv/borg/$(hostname -f)" BORG_PASSPHRASE="longandcomplexpassphrase" PATHS=( @@ -19,6 +19,20 @@ KEEP_WEEKLY=0 KEEP_MONTHLY=24 KEEP_YEARLY=5 +#LOCKFILE="/var/lock/borgwrapper.lock" + +# Command to execute before backup, e.g., taking a ZFS snapshot. Must return successfully +# for backup to start. POST_BACKUP_CMD is executed even if this fails. +#PRE_BACKUP_CMD=() + +# Command to execute after backup, e.g., removing a snapshot or sending a report somewhere. +# This command is executed regardless of backup success. +#POST_BACKUP_CMD=() + +# Command to execute after verification. +# This command is executed regardless of verification success. +#POST_VERIFY_CMD=() + # Optional arguments to the different borg commands. # The current default values are listed. Modify if needed. #BORG_BACKUP_ARGS=(