misc fixes and improvements
[distro-setup] / ziva-backup-check
diff --git a/ziva-backup-check b/ziva-backup-check
new file mode 100755 (executable)
index 0000000..31ae7dd
--- /dev/null
@@ -0,0 +1,46 @@
+#!/bin/bash
+# Copyright (C) 2019 Ian Kelling
+# SPDX-License-Identifier: AGPL-3.0-or-later
+
+source /a/bin/errhandle/err
+
+[[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
+
+pre="${0##*/}:"
+err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $pre: $*" >&2; }
+
+## begin check on syncthing
+if ! systemctl show --no-page syncthing@ziva | sed -n 's/^MainPID=//p' | egrep '^[0-9]+$' &>/dev/null; then
+  err no pid for syncthing@ziva. systemctl status:
+  systemctl status syncthing@ziva
+fi
+## end check on syncthing
+
+
+## begin check on btrbk
+now=$(date +%s)
+age_limit_sec=$(( 60 * 60 * 50 )) # 50 hours
+for vol in {root,boot}_ubuntubionic; do
+  snaps=(/mnt/r7/amy/btrbk/${vol}.20*)
+  if [[ ! ${snaps[*]} ]]; then
+    err no snapshots starting with /mnt/r7/amy/btrbk/${vol}_ubuntubionic.20
+    break
+  fi
+
+  read last_snap_sec last_snap < <(
+    for s in ${snaps[@]}; do
+      f=${s##*/}
+      unix_time=$(date -d $(sed -r  's/(.{4})(..)(.{5})(..)(.*)/\1-\2-\3:\4:\5/' <<<${f#$vol.}) +%s)
+      printf "%s %s\n" $unix_time $s # part of the pipeline
+    done | sort -r | head -n 1 ||:
+           )
+  if [[ ! $last_snap ]]; then
+    # should not happen.
+    err "could not find latest snapshot for $svp among ${snaps[*]}"
+    exit 1
+  fi
+  if (( last_snap_sec < now - age_limit_sec )); then
+    err vol $vol last backup older than 50 hours: $last_snap
+  fi
+done
+## end check on btrbk