Add pre and post backup hooks. Enforce only one running instance.

dev
hk 2017-06-05 02:29:38 +02:00
parent 40883bc54c
commit a48212db7e
2 changed files with 62 additions and 5 deletions

View File

@ -92,12 +92,44 @@ borg_exec () {
${BORG} "$@" ${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 trap 'error_handler ${LINENO} $?' ERR INT TERM
set -o errtrace -o pipefail set -o errtrace -o pipefail
# Default options
# Default parameters
CONFIG="/etc/borgwrapper/config.sh" CONFIG="/etc/borgwrapper/config.sh"
while getopts ":c:" OPT; do while getopts ":c:" OPT; do
@ -118,13 +150,25 @@ MODE="${1}"
source "${CONFIG}" || exit 1 source "${CONFIG}" || exit 1
export BORG_PASSPHRASE 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 if [[ ${MODE} == "init" ]]; then
borg_init borg_init
elif [[ ${MODE} == "backup" ]]; then elif [[ ${MODE} == "backup" ]]; then
trap 'exit_backup $?' ERR INT TERM
pre_backup_cmd
borg_backup borg_backup
borg_prune borg_prune
exit_backup 0
elif [[ ${MODE} == "verify" ]]; then elif [[ ${MODE} == "verify" ]]; then
trap 'exit_verify $?' ERR INT TERM
borg_verify borg_verify
exit_verify 0
elif [[ ${MODE} == "unlock" ]]; then elif [[ ${MODE} == "unlock" ]]; then
borg_unlock borg_unlock
elif [[ ${MODE} == "exec" ]]; then elif [[ ${MODE} == "exec" ]]; then
@ -140,5 +184,4 @@ else
exit 1 exit 1
fi fi
exit_clean 0
trap - ERR INT TERM

View File

@ -1,4 +1,4 @@
BORG="/usr/bin/borg" #BORG="/usr/bin/borg"
BORG_REPO="user@reposerver:/srv/borg/$(hostname -f)" BORG_REPO="user@reposerver:/srv/borg/$(hostname -f)"
BORG_PASSPHRASE="longandcomplexpassphrase" BORG_PASSPHRASE="longandcomplexpassphrase"
PATHS=( PATHS=(
@ -19,6 +19,20 @@ KEEP_WEEKLY=0
KEEP_MONTHLY=24 KEEP_MONTHLY=24
KEEP_YEARLY=5 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. # Optional arguments to the different borg commands.
# The current default values are listed. Modify if needed. # The current default values are listed. Modify if needed.
#BORG_BACKUP_ARGS=( #BORG_BACKUP_ARGS=(