-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-backups
executable file
·90 lines (79 loc) · 2.38 KB
/
run-backups
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
source /etc/b2-backup/local.conf
CONF_DIR=${CONF_DIR:-/etc/b2-backup}
STATE_DIR=${STATE_DIR:-/var/lib/b2-backup}
LOG_DIR=${LOG_DIR:-/var/log/b2-backup}
PROM_EXPORTER_DIR=${PROM_EXPORTER_DIR:-/var/lib/prometheus/node-exporter}
host=$(hostname -s)
echo "Launching backups in background (parallel):"
let count=0
while read path name; do
/usr/sbin/do-backup "${path}" "${name}" "$$" &>"${LOG_DIR}/${backup}.execlog" &
echo "+ ${name}"
let count=count+1
done < "${CONF_DIR}/backups"
echo -n "Waiting for backup jobs to complete... "
wait
echo "Done."
prom=$(mktemp)
echo "# HELP b2_backup_timestamp Timestamp that B2 Backup last reported data">$prom
echo "# TYPE b2_backup_timestamp gauge">>$prom
echo "b2_backup_timestamp $(date +%s)">>$prom
echo "# HELP b2_backup_status B2 Backup status by name">>$prom
echo "# TYPE b2_backup_status gauge">>$prom
email=$(mktemp)
echo "SUMMARY:" >$email
echo "Backup Results:"
let ok=0
while read path name; do
backup="${host}-${name}"
status=$(cat "${LOG_DIR}/${backup}.success" 2>/dev/null)
if [ "$status" == "$$" ]; then
echo "- ${name} SUCCEEDED" | tee -a $email
let ok=ok+1
echo "b2_backup_status{backup=\"${name}\"} 1">>$prom
else
echo "- ${name} FAILED (check logs)" | tee -a $email
echo "b2_backup_status{backup=\"${name}\"} 0">>$prom
fi
done < "${CONF_DIR}/backups"
if [ -w "${PROM_EXPORTER_DIR}" ]; then
cp "${prom}" "${PROM_EXPORTER_DIR}"/b2_backup.prom
chmod 644 "${PROM_EXPORTER_DIR}"/b2_backup.prom
fi
rm -f $prom
while read path name; do
backup="${host}-${name}"
echo -e "\n\n" >>$email
echo "== $name ($path) ==" >>$email
status=$(cat "${LOG_DIR}/${backup}.success" 2>/dev/null)
if [ "$status" != "$$" ]; then
latest=$(find "${LOG_DIR}" \
-regextype grep \
-regex ".*${backup}-[[:digit:]]\{8\}-[[:digit:]]\{6\}$" |
sort | tail -n1)
if [ -e "$latest" ]; then
echo -e "Last 10 lines of $latest ...\n" >>$email
tail -n 10 $latest >>$email
echo -e "\n--\n" >>$email
fi
fi
cat "${LOG_DIR}/${backup}.status" >>$email
done < "${CONF_DIR}/backups"
if [ ! -z "$BACKUP_EMAIL_TO" ]; then
mail -s "Backup Status ($host): $ok/$count succeeded" \
$BACKUP_EMAIL_TO <$email
fi
rm -f $email
# Clean-up logs
find "${LOG_DIR}" \
-regextype grep \
-regex '.*[[:digit:]]\{8\}-[[:digit:]]\{6\}$' \
-type f \
-mtime +1 \
-execdir gzip '{}' \;
find "${LOG_DIR}" \
-type f \
-not -name *-status \
-mtime +62 \
-delete