-
Notifications
You must be signed in to change notification settings - Fork 4
/
alpine-backup
executable file
·65 lines (50 loc) · 1.52 KB
/
alpine-backup
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
#!/bin/sh
RBU_CONF=/etc/lbu/rbu.conf
CMD_DEPS="mosquitto_pub openssl rsync ssh"
die() {
echo "$@" >&2
exit 1
}
mqtt_notify() {
local status="$1" size="$2" duration="$3"
local payload=$(printf '{ "status": "%s", "size": %u, "duration": %u }' "$status" "$size" "$duration")
local json=$(printf '{ "host": "%s", "key": "backup", "payload": %s }' "$FQDN" "$payload")
mosquitto_pub -h "$RBU_MQTT_HOST" $RBU_MQTT_TLS -t "$RBU_MQTT_TOPIC" -m "$json" $RBU_MQTT_AUTH
}
for cmd in $CMD_DEPS; do
command -v $cmd >/dev/null 2>&1 || \
die "Error: command \"$cmd\" not found."
done
if [ -f "$RBU_CONF" ]; then
. "$RBU_CONF"
else
die "Cannot read $RBU_CONF"
fi
if [ -n "$RBU_LOCATION" ] && [ -n " $RBU_CACHE" ]; then
mkdir -p "$RBU_LOCATION" "$RBU_CACHE"
else
die "Error: RBU_LOCATION and/or RBU_CACHE are not set."
fi
# set the suffix of backup files when ecryptions is enabled
[ -n "$ENCRYPTION" ] && export SUFFIX=".$ENCRYPTION"
# convert hostname to intra FQDN
if [ -n "$RBU_HOST" ]; then
export FQDN="$(hostname).${RBU_HOST}.${RBU_DOMAIN}"
else
export FQDN="$(hostname).${RBU_DOMAIN}"
fi
START_TS=$(date +%s)
LOGFILE="$RBU_CACHE/$FQDN-$(date -u "+%Y%m%d%H%M%S").log"
lbu_commit >>"$LOGFILE" 2>&1
ret=$?
DURATION=$(($(date +%s)-$START_TS))
SIZE=0
STATUS=failed
if [ $ret = 0 ]; then
BACKUP="$RBU_LOCATION/$(hostname).apkovl.tar.gz${SUFFIX}"
SIZE=$(stat -c %s $BACKUP)
STATUS=success
fi
ssh "$RBU_SERVER" mkdir -p $FQDN/logs
scp -q $LOGFILE "$RBU_SERVER:$FQDN/logs"
[ -n "$RBU_MQTT_HOST" ] && mqtt_notify "$STATUS" "$SIZE" "$DURATION"