|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
###############
|
|
|
|
# SCRIPT PREP #
|
|
|
|
###############
|
|
|
|
|
|
|
|
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
|
|
|
|
. "${SCRIPT_DIR}"/variables.sh
|
|
|
|
|
|
|
|
############################
|
|
|
|
# LOGGING & ERROR HANDLING #
|
|
|
|
############################
|
|
|
|
|
|
|
|
# Setup for logging
|
|
|
|
exec 3<&1 4<&2
|
|
|
|
trap "exec 2<&4 1<&3" 0 1 2 3
|
|
|
|
exec > >(tee >(ts "%Y-%m-%d_%H:%M:%S" > "${BACKUP_LOG}")) 2>&1
|
|
|
|
set -eEuo pipefail
|
|
|
|
|
|
|
|
# Set logging to debug
|
|
|
|
if [ $DEBUG = 'yes' ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If script fails, send notification to NTFY
|
|
|
|
if [ $NTFY_NOTIFICATIONS = 'yes' ]; then
|
|
|
|
trap 'err_report' ERR
|
|
|
|
|
|
|
|
function err_report() {
|
|
|
|
sleep 5
|
|
|
|
curl \
|
|
|
|
-T "${BACKUP_LOG}" \
|
|
|
|
-H "Filename: backupLog_"${DATETIME}".log" \
|
|
|
|
-H prio:high \
|
|
|
|
-H "Title: Backup Failed on ${HOSTNAME}" \
|
|
|
|
ntfyUser:ntfyPassword@ntfyDomain/ntfyTopic
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
|
|
|
##################
|
|
|
|
# BACKUP SCRIPTS #
|
|
|
|
##################
|
|
|
|
|
|
|
|
# If selected, run an rsync backup
|
|
|
|
if [ $RSYNC_BACKUP = 'yes' ]; then
|
|
|
|
. "${SCRIPT_DIR}"/rsync.sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If selected, run a local restic backup
|
|
|
|
if [ $LOCAL_RESTIC_BACKUP = 'yes' ]; then
|
|
|
|
. "${SCRIPT_DIR}"/rsync.sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If selected, run a forget, prune, and check on the local restic repo after a local restic backup
|
|
|
|
if [ $LOCAL_RESTIC_BACKUP_CLEAN = 'yes' ]; then
|
|
|
|
. "${SCRIPT_DIR}"/rsync.sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If selected, run a forget, prune, and check on the local restic repo before a remote restic backup
|
|
|
|
if [ $REMOTE_RESTIC_BACKUP_CLEAN_LOCAL = 'yes' ]; then
|
|
|
|
. "${SCRIPT_DIR}"/rsync.sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If selected, run a remote restic backup
|
|
|
|
if [ $REMOTE_RESTIC_BACKUP = 'yes' ]; then
|
|
|
|
. "${SCRIPT_DIR}"/rsync.sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
# If selected, run a forget, prune, and check on the remote restic repo after a remote restic backup
|
|
|
|
if [ $REMOTE_RESTIC_BACKUP_CLEAN = 'yes' ]; then
|
|
|
|
. "${SCRIPT_DIR}"/rsync.sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
##############
|
|
|
|
# TIDYING UP #
|
|
|
|
##############
|
|
|
|
|
|
|
|
# Clean up log files older than log retention days
|
|
|
|
find "${LOG_DIR}"/*.log -mtime +"${LOG_RETENTION}" -type f -delete
|
|
|
|
|
|
|
|
# End of script message in log
|
|
|
|
echo > >(tee >(echo "$(ts "%Y-%m-%d_%H:%M:%S") Backup Script Complete on ${HOSTNAME}" >> "${BACKUP_LOG}"))
|
|
|
|
|
|
|
|
# If script succeeds, send notification to NTFY
|
|
|
|
if [ $NTFY_NOTIFICATIONS = 'yes' ]; then
|
|
|
|
sleep 5
|
|
|
|
curl \
|
|
|
|
-T "${BACKUP_LOG}" \
|
|
|
|
-H "Filename: backupLog_"${DATETIME}".log" \
|
|
|
|
-H prio:low \
|
|
|
|
-H "Title: Backup Succeeded on ${HOSTNAME}" \
|
|
|
|
ntfyUser:ntfyPassword@ntfyDomain/ntfyTopic
|
|
|
|
fi
|