Compare commits
14 commits
998687fdbe
...
bcd39666f4
Author | SHA1 | Date | |
---|---|---|---|
|
bcd39666f4 | ||
|
0ae361d18c | ||
|
0b3c02345f | ||
|
55c40c9bc7 | ||
|
06296f39e1 | ||
|
3cd7f89231 | ||
|
bfd4eb9007 | ||
|
c9b850d7dd | ||
|
696f416aa2 | ||
|
6f463f450b | ||
|
448b04a3da | ||
|
450165ed99 | ||
|
88cea11b44 | ||
|
bc0a204ccc |
4 changed files with 25 additions and 20 deletions
39
bin/backup
39
bin/backup
|
@ -1,19 +1,15 @@
|
||||||
#! /bin/bash
|
#! /bin/bash
|
||||||
set -uo pipefail
|
set -uo pipefail
|
||||||
|
|
||||||
#backup li run
|
display_usage() {
|
||||||
#backup li
|
|
||||||
|
|
||||||
display_usage() {
|
|
||||||
echo "Usage: $0 <repo> (local|monitor <host> <warning> <critical>|restic arguments)" >&2
|
echo "Usage: $0 <repo> (local|monitor <host> <warning> <critical>|restic arguments)" >&2
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ "$#" -lt 2 ]; then
|
if [ "$#" -lt 2 ]; then
|
||||||
display_usage
|
display_usage
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
TARGET=$1
|
TARGET=$1
|
||||||
ACTION=$2
|
ACTION=$2
|
||||||
RESTIC=$(which restic)
|
RESTIC=$(which restic)
|
||||||
|
@ -26,7 +22,7 @@ check_config() {
|
||||||
exit 1
|
exit 1
|
||||||
else
|
else
|
||||||
set -a
|
set -a
|
||||||
source $CONFIG
|
source $CONFIG
|
||||||
set +a
|
set +a
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -35,19 +31,20 @@ check_config() {
|
||||||
echo "Restic binary not found"
|
echo "Restic binary not found"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handle_params () {
|
handle_params () {
|
||||||
|
|
||||||
|
. /etc/backup/local.config
|
||||||
if [ $2 == "local" ]; then
|
if [ $2 == "local" ]; then
|
||||||
do_local_backup
|
do_local_backup
|
||||||
elif [ $2 == "monitor" ]; then
|
elif [ $2 == "monitor" ]; then
|
||||||
do_monitor $@
|
do_monitor $@
|
||||||
else
|
else
|
||||||
shift 1
|
shift 1
|
||||||
$RESTIC $@
|
# define an empty default if RESTIC_ARGS is not set
|
||||||
|
RESTIC_ARGS=${RESTIC_ARGS:-""}
|
||||||
|
$RESTIC $RESTIC_ARGS $@
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,11 +57,14 @@ do_local_backup () {
|
||||||
|
|
||||||
. /etc/backup/local.config
|
. /etc/backup/local.config
|
||||||
|
|
||||||
|
# define an empty default if BACKUP_ARGS is not set
|
||||||
|
BACKUP_ARGS=${BACKUP_ARGS:-""}
|
||||||
|
|
||||||
if [[ -x /etc/backup/local.pre ]]; then
|
if [[ -x /etc/backup/local.pre ]]; then
|
||||||
/etc/backup/local.pre $TARGET
|
/etc/backup/local.pre $TARGET
|
||||||
fi
|
fi
|
||||||
|
|
||||||
$RESTIC --exclude-file /etc/backup/local.exclude backup --hostname $BACKUP_HOSTNAME $BACKUP_DIR
|
$RESTIC --exclude-file /etc/backup/local.exclude backup --host $BACKUP_HOSTNAME $BACKUP_ARGS $BACKUP_DIR
|
||||||
|
|
||||||
if [[ -x /etc/backup/local.post ]]; then
|
if [[ -x /etc/backup/local.post ]]; then
|
||||||
/etc/backup/local.post $TARGET
|
/etc/backup/local.post $TARGET
|
||||||
|
@ -85,8 +85,12 @@ do_monitor () {
|
||||||
WARN=$4
|
WARN=$4
|
||||||
CRIT=$5
|
CRIT=$5
|
||||||
|
|
||||||
|
# define an empty default if RESTIC_ARGS is not set
|
||||||
|
RESTIC_ARGS=${RESTIC_ARGS:-""}
|
||||||
|
SNAPS=`$RESTIC $RESTIC_ARGS snapshots --compact --no-lock -H $3`
|
||||||
# Get last line and parse into variables. Removes header and is empty when no snapshot exists for host
|
# Get last line and parse into variables. Removes header and is empty when no snapshot exists for host
|
||||||
LAST=`$RESTIC snapshots --compact --no-lock -H $3 | sed 1,2d | tail -n 1`
|
LAST=`sed 1,2d <<< $SNAPS | head -n -2 | tail -n 1`
|
||||||
|
COUNT=`tail -n 1 <<< $SNAPS | cut -d " " -f 1`
|
||||||
if [ ! $? -eq 0 ]; then
|
if [ ! $? -eq 0 ]; then
|
||||||
echo "WARNING - restic command returned an error"
|
echo "WARNING - restic command returned an error"
|
||||||
exit 1;
|
exit 1;
|
||||||
|
@ -94,7 +98,7 @@ do_monitor () {
|
||||||
|
|
||||||
IFS=' ' read HASH DATE TIME HOST DIR <<< "$LAST"
|
IFS=' ' read HASH DATE TIME HOST DIR <<< "$LAST"
|
||||||
|
|
||||||
if [ -z "$HASH" ]; then
|
if [ -z "$HASH" ]; then
|
||||||
echo "UNKNOWN - No snapshot found for $3"
|
echo "UNKNOWN - No snapshot found for $3"
|
||||||
exit 4;
|
exit 4;
|
||||||
fi
|
fi
|
||||||
|
@ -109,12 +113,13 @@ do_monitor () {
|
||||||
BACKUP_TST=$(date -d "$DATE $TIME" +"%s" )
|
BACKUP_TST=$(date -d "$DATE $TIME" +"%s" )
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
NOW_TST=$(date +%s)
|
NOW_TST=$(date +%s)
|
||||||
DIFF_S=`expr $NOW_TST - $BACKUP_TST`
|
DIFF_S=`expr $NOW_TST - $BACKUP_TST`
|
||||||
|
|
||||||
DIFF_H=`expr $DIFF_S / 3600`
|
DIFF_H=`expr $DIFF_S / 3600`
|
||||||
|
|
||||||
MESSAGE="Last snapshot #$HASH ${DIFF_H}h ago"
|
MESSAGE="Last snapshot #$HASH ${DIFF_H}h ago|snapshots=$COUNT; age=$DIFF_H"
|
||||||
RET=0
|
RET=0
|
||||||
RET_H="OK"
|
RET_H="OK"
|
||||||
|
|
||||||
|
@ -124,7 +129,7 @@ do_monitor () {
|
||||||
elif [ $DIFF_H -lt $CRIT ]; then
|
elif [ $DIFF_H -lt $CRIT ]; then
|
||||||
RET=1
|
RET=1
|
||||||
RET_H="WARNING"
|
RET_H="WARNING"
|
||||||
else
|
else
|
||||||
RET=2
|
RET=2
|
||||||
RET_H="CRITICAL"
|
RET_H="CRITICAL"
|
||||||
|
|
||||||
|
@ -133,9 +138,5 @@ do_monitor () {
|
||||||
return $RET
|
return $RET
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
check_config $@
|
check_config $@
|
||||||
handle_params $@
|
handle_params $@
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
BACKUP_HOSTNAME="backuphost.example.org"
|
BACKUP_HOSTNAME=$(hostname -f)
|
||||||
BACKUP_DIR="/"
|
BACKUP_DIR="/"
|
||||||
|
BACKUP_ARGS="--tag filesystem"
|
||||||
# HEALTHCHECK_URL="<URL from https://healthchecks.io/checks/ to notify>"
|
# HEALTHCHECK_URL="<URL from https://healthchecks.io/checks/ to notify>"
|
||||||
|
# RESTIC_ARGS="<optional restic args appended to all commands>"
|
||||||
|
|
1
etc/backup/local.post
Executable file
1
etc/backup/local.post
Executable file
|
@ -0,0 +1 @@
|
||||||
|
# run commands after the backup (script must be executable)
|
1
etc/backup/local.pre
Executable file
1
etc/backup/local.pre
Executable file
|
@ -0,0 +1 @@
|
||||||
|
# run commands before the backup (script must be executable)
|
Loading…
Reference in a new issue