-
Notifications
You must be signed in to change notification settings - Fork 3
/
borg-consciousness.sh
executable file
·66 lines (53 loc) · 2.23 KB
/
borg-consciousness.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
export BORG_REPO='user@server.domain:my-repo-name' # point to your borg repo;
#if you use SSH, you can also use the following format:
#export BORG_REPO='ssh://user@server.domain/./repo'
export BORG_RSH='ssh -i /root/.ssh/privateKey' #point to your SSH private key
export BORG_REMOTE_PATH=/usr/local/bin/borg1/borg1 # point to your borg executable
export BORG_PASSPHRASE='redacted'
n_days=3 # if backup was less recent than n_days, alert administrator
heartbeat_url="https://hc-ping.com/redacted" # using healthchecks.io for email/Telegram/whatever alerts
fail_url="https://hc-ping.com/redacted/fail"
make_it_fail=0 # set to 1 to test your alerts by simulating an old backup
die()
{
echo "Error fetching or calculating date"
curl --retry 3 $fail_url
exit 1
}
is_num() # returns (exits) 0 if passed a valid integer
{
if (( $1 == $1 )) 2>/dev/null; then
return 0
else
return 1
fi
}
last=`borg list --last 10 --format "{archive} {end} {NEWLINE}" | grep -v checkpoint | tail -1` # get the last 10 backups from the repo, filter out checkpoints, get last one
if (( $make_it_fail )); then
last="qwho-1989-05-08T11:11:11 Thu, 1989-05-08 11:11:11" # simulate an old backup
fi
echo Last backup: $last
last_date=$( echo $last | cut -d " " -f 3 )
if [[ -z $last_date ]]; then
die # didn't get a string
fi
last_timestamp=$( date --date=$last_date +%s 2>/dev/null )
now_timestamp=$( date +%s )
if ! ( ( is_num $last_timestamp ) && ( is_num $now_timestamp ) ); then
die # invalid timestamp
fi
days_ago=$( echo "($now_timestamp - $last_timestamp) / (3600 * 24)" | bc )
if ! ( ( is_num $days_ago ) && (( $days_ago >= 0 )) ); then
die # invalid or negative result
fi
# Sums successful - check backup time and send appropriate heartbeats.io request
if (( $days_ago < $n_days )); then
echo Good - last backup was $days_ago days ago - less than the $n_days day limit
echo Sending heartbeat...
curl --retry 3 $heartbeat_url
else
echo Bad - last backup was $days_ago days ago - greater than the $n_days day limit
echo Sending alert...
curl --retry 3 $fail_url
fi