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