149 lines
4.4 KiB
Bash
149 lines
4.4 KiB
Bash
|
#!/usr/bin/env bash
|
||
|
|
||
|
# Performs borgmatic backups and checks, given that
|
||
|
# enough time has elapsed since the last one performed.
|
||
|
# cerberus, 2022-02-03-1
|
||
|
|
||
|
set -o pipefail
|
||
|
|
||
|
[ -f /opt/borg/bin/borg ] && borg=/opt/borg/bin/borg || borg="$(command -v borg)"
|
||
|
[ -f /opt/borg/bin/borgmatic ] && borgmatic=/opt/borg/bin/borgmatic || borgmatic="$(command -v borgmatic)"
|
||
|
|
||
|
export LOG_FILE="/var/log/borgmatic-wrapper.log"
|
||
|
export STATE_DIR="/var/tmp/borgmatic-wrapper"
|
||
|
LIB_DIR="${LIB_DIR:-/usr/local/jilits}"
|
||
|
|
||
|
script_name="$(basename -- "$0")"
|
||
|
max_minutes_since_last_backup=""
|
||
|
max_minutes_since_last_check=720
|
||
|
|
||
|
. "$LIB_DIR/libverbosity.sh" || exit 127
|
||
|
. "$LIB_DIR/libcommon.sh" || exit 127
|
||
|
. "$LIB_DIR/libdatatype.sh" || exit 127
|
||
|
. "$LIB_DIR/libdatetime.sh" || exit 127
|
||
|
. "$LIB_DIR/libstate.sh" || exit 127
|
||
|
|
||
|
eggsit() {
|
||
|
rc=$?
|
||
|
[ $rc -eq 0 ] && echlog "Script exiting ($rc)..."
|
||
|
[ $rc -ne 0 ] && errlog "Script exiting ($rc)..."
|
||
|
exit $rc
|
||
|
}
|
||
|
trap "eggsit" EXIT
|
||
|
|
||
|
get_last_backup_ts() {
|
||
|
local ts state_ttl="$((6 * 60 * 60))"
|
||
|
ts="$(get_state CachedLastBackupTimestamp)"
|
||
|
|
||
|
if [ -n "$ts" ]; then
|
||
|
echo "$ts"
|
||
|
return 0
|
||
|
fi
|
||
|
|
||
|
ts="$(sudo "$borgmatic" list --last 1 --successful --json | jq -r '.[0].archives[0].start')"
|
||
|
set_state CachedLastBackupTimestamp "$ts" "$state_ttl"
|
||
|
echo "$ts"
|
||
|
}
|
||
|
|
||
|
minutes_since_last_backup() {
|
||
|
local ue
|
||
|
ue="$(dt_to_ue "$(get_last_backup_ts)")"
|
||
|
echo "$((($(current_ue) - ue) / 60))"
|
||
|
}
|
||
|
|
||
|
remote_is_online() {
|
||
|
local host port
|
||
|
host="$(cut -d: -f1 <<<"$1")"
|
||
|
port="$(cut -d: -f2 <<<"$1")"
|
||
|
nc -zvw1 "${host:?Missing remote host}" "${port:?Missing remote port}" | tee -a "$LOG_FILE" >/dev/null
|
||
|
}
|
||
|
|
||
|
perform_backup() {
|
||
|
echlog "Running backup."
|
||
|
|
||
|
rm_state CachedLastBackupTimestamp
|
||
|
rm_state LastBackupPrettyTimestamp
|
||
|
|
||
|
"$borgmatic" create --files --stats --verbosity 2 --syslog-verbosity -1 2>&1 | tee -a "$LOG_FILE" || return 1
|
||
|
set_state LastBackupPrettyTimestamp "$(dt_to_pretty "$(get_last_backup_ts)")" "$((4 * 60 * 60))"
|
||
|
|
||
|
"$borgmatic" prune --files --stats --verbosity 2 --syslog-verbosity -1 2>&1 | tee -a "$LOG_FILE" || return 2
|
||
|
}
|
||
|
|
||
|
perform_check() {
|
||
|
echlog "Running check."
|
||
|
|
||
|
rm_state LastCheckUnixEpoch
|
||
|
|
||
|
"$borgmatic" check --progress --verbosity 2 --syslog-verbosity -1 2>&1 | tee -a "$LOG_FILE" || return 1
|
||
|
set_state LastCheckUnixEpoch "$(current_ue)" "$((24 * 60 * 60))"
|
||
|
}
|
||
|
|
||
|
function usage() {
|
||
|
echo "Usage: $script_name [OPTIONS]"
|
||
|
echo
|
||
|
echo "Options:"
|
||
|
echo " -h Display help."
|
||
|
echo " -b [MINUTES] Max minutes since last backup. "
|
||
|
echo " -c [MINUTES] Max minutes since last check."
|
||
|
echo " -r [HOST:PORT] Ensure remote is online before running actions."
|
||
|
}
|
||
|
|
||
|
while getopts ":b:c:r:h" arg; do
|
||
|
case $arg in
|
||
|
b) max_minutes_since_last_backup=$OPTARG ;;
|
||
|
c) max_minutes_since_last_check=$OPTARG ;;
|
||
|
r) remote_host_port=$OPTARG ;;
|
||
|
h)
|
||
|
usage
|
||
|
exit 0
|
||
|
;;
|
||
|
:)
|
||
|
echo "$script_name: Must supply a value with -$OPTARG." >&2
|
||
|
usage
|
||
|
exit 1
|
||
|
;;
|
||
|
?)
|
||
|
echo "Invalid option: -${OPTARG}."
|
||
|
echo
|
||
|
usage
|
||
|
exit 2
|
||
|
;;
|
||
|
esac
|
||
|
done
|
||
|
|
||
|
rotlog 10000000
|
||
|
|
||
|
if ! is_int "$max_minutes_since_last_backup"; then
|
||
|
usage
|
||
|
echlog
|
||
|
echxit "ERROR: Invalid or unset value for argument -b: '$max_minutes_since_last_backup'" 2
|
||
|
fi
|
||
|
|
||
|
if ! im_root; then
|
||
|
echxit "ERROR: This script must be run as root" 1
|
||
|
elif ! command -v jq >/dev/null; then
|
||
|
echxit "ERROR: jq not installed" 3
|
||
|
elif pgrep -f "$borg" >/dev/null; then
|
||
|
echxit "ERROR: Borg seems to be active already" 11
|
||
|
elif [ -n "$remote_host_port" ]; then
|
||
|
if ! remote_is_online "$remote_host_port"; then
|
||
|
echxit "ERROR: Unable to reach $remote_host_port" 12
|
||
|
fi
|
||
|
fi
|
||
|
|
||
|
echlog "The last backup was carried out $(minutes_since_last_backup) minutes ago (>=$max_minutes_since_last_backup)."
|
||
|
if [[ "$(minutes_since_last_backup)" -ge "$max_minutes_since_last_backup" ]]; then
|
||
|
perform_backup || echxit "ERROR: Backup returned errors" 21
|
||
|
fi
|
||
|
|
||
|
last_check_unixepoch="$(get_state LastCheckUnixEpoch)" || last_check_unixepoch=0
|
||
|
minutes_since_last_check="$((($(current_ue) - last_check_unixepoch) / 60))"
|
||
|
echo "The last check was carried out $minutes_since_last_check minutes ago (>=$max_minutes_since_last_check)." | tee -a "$LOG_FILE"
|
||
|
|
||
|
if [[ "$minutes_since_last_check" -ge "$max_minutes_since_last_check" ]]; then
|
||
|
perform_check || echxit "ERROR: Check returned errors" 21
|
||
|
fi
|
||
|
|
||
|
echo "Script finished."
|