-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrrb_interval
executable file
·39 lines (34 loc) · 1.08 KB
/
rrb_interval
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
#!/bin/sh
# Run rrb if it was last run more than the specified number of seconds ago.
# Useful in cron-scripts for clients, that are not online all the time.
set -e
HELP="Usage: $0 interval config"
EX_USAGE=64
EX_CONFIG=78
unset DEST_DIR
fail()
{
echo -e "$1" >&2
exit $2
}
do_rrb()
{
exec rrb $CONFIG
}
INTERVAL="$1"
CONFIG="$2"
[ "$#" -eq 2 ] || fail "Invalid usage.\n$HELP" $EX_USAGE
[ "$INTERVAL" ] || fail "Interval missing.\n$HELP" $EX_USAGE
[ "$INTERVAL" -ge 1 ] || fail "Invalid interval.\n$HELP" $EX_USAGE
[ "$CONFIG" ] || fail "Config file missing.\n$HELP" $EX_USAGE
source $CONFIG
[ "$DEST_DIR" ] || fail "Invalid config file, missing \$DEST_DIR.
$HELP" $EX_CONFIG
LATEST_DIR="$DEST_DIR/latest"
[ -e "$LATEST_DIR" ] || do_rrb
# date does not parse datetime with 'T' as delimiter correctly, replace with
# space. (Or it does it correctly by recognizing the timezone, but this is not
# wanted here)
DATE_LAST=`date --date="\`readlink $LATEST_DIR | tr T ' '\`" +%s`
DATE_LIMIT=`echo "\`date +%s\` - $INTERVAL" | bc`
[ $DATE_LAST -le $DATE_LIMIT ] && do_rrb