You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.4 KiB
Bash

#!/bin/bash
###############
# SCRIPT PREP #
###############
# Update restic
if [ $UPDATE = 'yes' ]; then
restic self-update
fi
# Source base directory of script and source variables
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}" \
"${NTFY_URL}"
}
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 [ $LAN_RESTIC_BACKUP = 'yes' ]; then
. "${SCRIPT_DIR}"/restic.sh
fi
# If selected, run a forget, prune, and check on the local restic repo after a local restic backup
if [ $LAN_RESTIC_CLEAN = 'yes' ]; then
. "${SCRIPT_DIR}"/resticClean.sh
fi
# Overwrite LAN restic variables with the AWS restic variables
if [ $LAN_AND_AWS_RESTIC = 'yes' ]; then
. "${SCRIPT_DIR}"/variablesAWS.sh
fi
# If selected, run a remote restic backup
if [ $AWS_RESTIC_BACKUP = 'yes' ]; then
. "${SCRIPT_DIR}"/restic.sh
fi
# If selected, run a forget, prune, and check on the remote restic repo after a remote restic backup
if [ $AWS_RESTIC_CLEAN = 'yes' ]; then
. "${SCRIPT_DIR}"/resticClean.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}" \
"${NTFY_URL}"
fi