2017-09-04 10:36:00 +02:00
|
|
|
#! /bin/bash
|
|
|
|
set -uo pipefail
|
|
|
|
|
2018-01-21 11:28:14 +01:00
|
|
|
display_usage() {
|
2017-09-05 20:44:20 +02:00
|
|
|
echo "Usage: $0 <repo> (local|monitor <host> <warning> <critical>|restic arguments)" >&2
|
2018-01-21 11:28:14 +01:00
|
|
|
}
|
2017-09-04 10:36:00 +02:00
|
|
|
|
|
|
|
if [ "$#" -lt 2 ]; then
|
|
|
|
display_usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
TARGET=$1
|
|
|
|
ACTION=$2
|
|
|
|
RESTIC=$(which restic)
|
2017-09-05 12:39:33 +02:00
|
|
|
CURL=$(which curl)
|
2017-09-04 10:36:00 +02:00
|
|
|
|
|
|
|
check_config() {
|
|
|
|
CONFIG=/etc/backup/$1.repo
|
|
|
|
if [ ! -f $CONFIG ]; then
|
|
|
|
echo "Repo config file $CONFIG not found!"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
set -a
|
2018-01-21 11:28:14 +01:00
|
|
|
source $CONFIG
|
2017-09-04 10:36:00 +02:00
|
|
|
set +a
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2017-09-07 08:49:24 +02:00
|
|
|
if [[ ! -x $RESTIC ]]; then
|
2017-09-04 10:36:00 +02:00
|
|
|
echo "Restic binary not found"
|
2017-09-05 12:39:33 +02:00
|
|
|
exit 1
|
2017-09-04 10:36:00 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
handle_params () {
|
|
|
|
|
|
|
|
if [ $2 == "local" ]; then
|
|
|
|
do_local_backup
|
|
|
|
elif [ $2 == "monitor" ]; then
|
|
|
|
do_monitor $@
|
|
|
|
else
|
|
|
|
shift 1
|
|
|
|
$RESTIC $@
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
do_local_backup () {
|
|
|
|
if [ ! -f /etc/backup/local.config ]; then
|
|
|
|
echo "local backup config file $/etc/backup/local.config not found!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
. /etc/backup/local.config
|
2017-09-08 13:49:08 +02:00
|
|
|
|
2018-01-21 11:39:00 +01:00
|
|
|
# define an empty default if BACKUP_ARGS is not set
|
|
|
|
BACKUP_ARGS=${BACKUP_ARGS:-""}
|
|
|
|
|
2017-09-08 13:49:08 +02:00
|
|
|
if [[ -x /etc/backup/local.pre ]]; then
|
|
|
|
/etc/backup/local.pre $TARGET
|
|
|
|
fi
|
|
|
|
|
2018-01-21 11:39:00 +01:00
|
|
|
$RESTIC --exclude-file /etc/backup/local.exclude backup --hostname $BACKUP_HOSTNAME $BACKUP_ARGS $BACKUP_DIR
|
2017-09-04 16:38:38 +02:00
|
|
|
|
2017-09-08 13:49:08 +02:00
|
|
|
if [[ -x /etc/backup/local.post ]]; then
|
|
|
|
/etc/backup/local.post $TARGET
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "${HEALTHCHECK_URL:-}" ]]; then
|
2017-09-07 08:49:24 +02:00
|
|
|
if [[ -x $CURL ]]; then
|
2017-09-05 12:39:33 +02:00
|
|
|
$CURL -fsS --retry 4 "$HEALTHCHECK_URL" > /dev/null
|
|
|
|
fi
|
2017-09-04 16:38:38 +02:00
|
|
|
fi
|
2017-09-04 10:36:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
do_monitor () {
|
|
|
|
if [ $# -lt 5 ]; then
|
|
|
|
display_usage
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
WARN=$4
|
|
|
|
CRIT=$5
|
|
|
|
|
|
|
|
# Get last line and parse into variables. Removes header and is empty when no snapshot exists for host
|
2017-09-18 10:41:05 +02:00
|
|
|
LAST=`$RESTIC snapshots --compact --no-lock -H $3 | sed 1,2d | tail -n 1`
|
2017-09-04 10:36:00 +02:00
|
|
|
if [ ! $? -eq 0 ]; then
|
2017-09-18 10:41:05 +02:00
|
|
|
echo "WARNING - restic command returned an error"
|
2017-09-04 10:36:00 +02:00
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
|
|
|
IFS=' ' read HASH DATE TIME HOST DIR <<< "$LAST"
|
|
|
|
|
2018-01-21 11:28:14 +01:00
|
|
|
if [ -z "$HASH" ]; then
|
2017-09-04 10:36:00 +02:00
|
|
|
echo "UNKNOWN - No snapshot found for $3"
|
|
|
|
exit 4;
|
|
|
|
fi
|
|
|
|
|
2017-09-05 12:39:33 +02:00
|
|
|
|
2017-09-04 10:36:00 +02:00
|
|
|
# Compute time difference since last snapshot
|
2017-09-05 20:44:20 +02:00
|
|
|
case $(uname -s) in
|
|
|
|
Darwin)
|
|
|
|
BACKUP_TST=$(date -j -f "%Y-%m-%d %H:%M:%S" "$DATE $TIME" "+%s")
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
BACKUP_TST=$(date -d "$DATE $TIME" +"%s" )
|
|
|
|
;;
|
|
|
|
esac
|
2018-01-21 11:28:14 +01:00
|
|
|
|
2017-09-04 10:36:00 +02:00
|
|
|
NOW_TST=$(date +%s)
|
|
|
|
DIFF_S=`expr $NOW_TST - $BACKUP_TST`
|
2017-09-05 12:39:33 +02:00
|
|
|
|
2017-09-04 10:36:00 +02:00
|
|
|
DIFF_H=`expr $DIFF_S / 3600`
|
|
|
|
|
|
|
|
MESSAGE="Last snapshot #$HASH ${DIFF_H}h ago"
|
|
|
|
RET=0
|
|
|
|
RET_H="OK"
|
2017-09-05 12:39:33 +02:00
|
|
|
|
2017-09-04 10:36:00 +02:00
|
|
|
if [ $DIFF_H -lt $WARN ]; then
|
2017-09-05 12:39:33 +02:00
|
|
|
RET=0
|
2017-09-04 10:36:00 +02:00
|
|
|
RET_H="OK"
|
|
|
|
elif [ $DIFF_H -lt $CRIT ]; then
|
|
|
|
RET=1
|
|
|
|
RET_H="WARNING"
|
2018-01-21 11:28:14 +01:00
|
|
|
else
|
2017-09-04 10:36:00 +02:00
|
|
|
RET=2
|
|
|
|
RET_H="CRITICAL"
|
|
|
|
|
|
|
|
fi
|
|
|
|
echo "$RET_H - $MESSAGE"
|
2017-09-05 12:39:33 +02:00
|
|
|
return $RET
|
2017-09-04 10:36:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
check_config $@
|
|
|
|
handle_params $@
|