3db63fcd253a9bf743ebf4a3a1ec531c1bde7dc1
[distro-setup] / ziva-backup-check
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4
5 source /a/bin/errhandle/err
6
7 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
8
9 pre="${0##*/}:"
10 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
11
12 ## begin check on syncthing
13 if ! systemctl show --no-page syncthing@ziva | sed -n 's/^MainPID=//p' | egrep '^[0-9]+$' &>/dev/null; then
14 err no pid for syncthing@ziva. systemctl status:
15 systemctl status syncthing@ziva
16 fi
17 ## end check on syncthing
18
19
20 ## begin check on btrbk
21 age_limit_sec=$(( 60 * 60 * 24 * 7 )) # 7 days.
22 for prefix in root boot; do
23 if [[ $prefix == boot ]]; then
24 # its not uncommon for the /boot subvol to have no changes, and thus
25 # no new backups for 10 days or so. todo: instead of this error
26 # prone check, we should make it so the ziva computer will
27 # touch a file on our computer whenever btrbk succeeds
28 age_limit_sec=$(( age_limit_sec + 60* 60 * 24 * 35 ))
29 fi
30 vol=${prefix}_ubuntubionic
31 snaps=(/mnt/r7/amy/$prefix/btrbk/${vol}.20*)
32 if [[ ! ${snaps[*]} ]]; then
33 err no snapshots starting with /mnt/r7/amy/$prefix/btrbk/${vol}.20
34 break
35 fi
36
37 read last_snap_sec last_snap < <(
38 for s in ${snaps[@]}; do
39 f=${s##*/}
40 unix_time=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
41 printf "%s %s\n" $unix_time $s # part of the pipeline
42 done | sort -r | head -n 1 ||:
43 )
44 if [[ ! $last_snap ]]; then
45 # should not happen.
46 err "could not find latest snapshot for $svp among ${snaps[*]}"
47 exit 1
48 fi
49 if (( last_snap_sec < EPOCHSECONDS - age_limit_sec )); then
50 err vol $vol last backup older than 50 hours: $last_snap
51 fi
52 done
53 ## end check on btrbk