various fixes
[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 now=$(date +%s)
22 age_limit_sec=$(( 60 * 60 * 50 )) # 50 hours
23 for prefix in root boot; do
24 vol=${prefix}_ubuntubionic
25 snaps=(/mnt/r7/amy/$prefix/btrbk/${vol}.20*)
26 if [[ ! ${snaps[*]} ]]; then
27 err no snapshots starting with /mnt/r7/amy/$prefix/btrbk/${vol}.20
28 break
29 fi
30
31 read last_snap_sec last_snap < <(
32 for s in ${snaps[@]}; do
33 f=${s##*/}
34 unix_time=$(date -d $(sed -r 's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
35 printf "%s %s\n" $unix_time $s # part of the pipeline
36 done | sort -r | head -n 1 ||:
37 )
38 if [[ ! $last_snap ]]; then
39 # should not happen.
40 err "could not find latest snapshot for $svp among ${snaps[*]}"
41 exit 1
42 fi
43 if (( last_snap_sec < now - age_limit_sec )); then
44 err vol $vol last backup older than 50 hours: $last_snap
45 fi
46 done
47 ## end check on btrbk