forked from wiedehopf/graphs1090
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriteback.sh
executable file
·52 lines (40 loc) · 1.64 KB
/
writeback.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
#!/bin/bash
set -e
trap 'echo "[ERROR] Error in line $LINENO when executing: $BASH_COMMAND"' ERR
TARGET=/var/lib/collectd/rrd
RUNFOLDER=/run/collectd
if ! [[ -f "$RUNFOLDER/readback-complete" ]]; then
echo "readback didn't complete, no writeback of $RUNFOLDER to disk!"
exit 1
fi
if ! [[ -d "$RUNFOLDER/localhost" ]]; then
echo "directory $RUNFOLDER/localhost not present, collectd hasn't been running long. if collectd has been running for a bit, this condition is unexpeted!"
exit 1
fi
echo "writing DB from $RUNFOLDER to disk"
mkdir -p "$TARGET"
#tar gz localhost
TMPF="$TARGET/localhost.tar.gz.tmp"
rm -f "$TMPF"
if ! tar --directory "$RUNFOLDER" -c localhost | gzip -1 -c > "$TMPF" || ! [[ -f "$TMPF" ]]; then
echo "FATAL: writeback failed"
exit 1
fi
if [[ -f "$TARGET/localhost.tar.gz" ]] && (( $(stat -c %s "$TARGET/localhost.tar.gz") > 150000 )); then
mv -f -T "$TARGET/localhost.tar.gz" "$TARGET/auto-backup-$(date +%Y-week_%V).tar.gz" &>/dev/null || true
find "$TARGET" -name 'auto-backup-*.tar.gz' -mtime +60 -delete || true
fi
sync
mv -f "$TMPF" "$TARGET/localhost.tar.gz"
echo "writeback size on disk: $(du -sh "$TARGET/localhost.tar.gz" || true)" || true
# remove localhost folder as it would be used with preference in the readback instead of localhost.tar.gz
if [[ -d "$TARGET/localhost" ]]; then
if tar --directory "$TARGET" -c localhost | gzip -1 -c > "$TMPF"; then
mv -f -T "$TMPF" "$TARGET/auto-backup-old-localhost-folder-$(date +%Y-week_%V).tar.gz" &>/dev/null || true
rm -rf "$TARGET/localhost"
fi
rm -f "$TMPF"
fi
# remove readback-complete flag
rm "$RUNFOLDER/readback-complete"
sync