host info updates
[distro-setup] / check-subvol-stale
index 2d00cbbda084f90ccb517970df310a9bd4def376..9b5e88da97df8d36736230f350b0bf8fbc0f1d89 100644 (file)
@@ -1,12 +1,19 @@
 #!/bin/bash
-# Copyright (C) 2016 Ian Kelling
-#
+# I, Ian Kelling, follow the GNU license recommendations at
+# https://www.gnu.org/licenses/license-recommendations.en.html. They
+# recommend that small programs, < 300 lines, be licensed under the
+# Apache License 2.0. This file contains or is part of one or more small
+# programs. If a small program grows beyond 300 lines, I plan to switch
+# its license to GPL.
+
+# Copyright 2024 Ian Kelling
+
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
-#
+
 #     http://www.apache.org/licenses/LICENSE-2.0
-#
+
 # Unless required by applicable law or agreed to in writing, software
 # distributed under the License is distributed on an "AS IS" BASIS,
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # limitations under the License.
 
 
+
 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
 
-source /usr/local/lib/err
+set -e; . /usr/local/lib/bash-bear; set +e
 
 shopt -s nullglob
 
@@ -77,9 +85,11 @@ stale-file() {
   fi
 
 }
+pre="check-subvol-stale:${SSH_CLIENT:+ $HOSTNAME:}"
+
 d() {
   if $verbose; then
-    printf "%s\n" "$*"
+    printf "$pre %s\n" "$*"
   fi
 }
 
@@ -144,18 +154,31 @@ for d; do
     d "svp=$svp # subvolume path"
   fi
 
-  # Assumes we are in the 21st century.
-  ls -1dvrq $root_dir/btrbk/$subvol_dir.20* >$tmpf
-  mapfile -t snaps <$tmpf
-  if [[ ! ${snaps[*]} ]]; then
+  # note: relying on null glob
+  ls_args=($root_dir/btrbk/$subvol_dir.20*)
+  if (( ${#ls_args[@]} )); then
+    # Assumes we are in the 21st century.
+    ls -1dvrq $root_dir/btrbk/$subvol_dir.20* >$tmpf
+    mapfile -t snaps <$tmpf
+  else
     # no snapshots yet
-    # TODO: make this an error and override with a cli flag
+    # TODO: consider making this an error and override with a cli flag
     echo "$0: warning: no snapshots found at $root_dir/btrbk/$subvol_dir.20*. this is expected for a brand new volume"
     continue
   fi
 
   # last_snap by date.
   last_snap="${snaps[0]}"
+
+  case $last_snap in
+    $root_dir/btrbk/$subvol_dir.20*) : ;;
+    *)
+      echo "$0: error: unexpected last_snap:$last_snap"
+      exit 1
+      ;;
+  esac
+
+  d last_snap=$last_snap
   ## alternate slower alternative which would not rely on ls sorting:
   # last_snap=$(
   #   for s in ${snaps[@]}; do
@@ -170,7 +193,6 @@ for d; do
   #   echo "$0: error: could not find latest snapshot for $svp among ${snaps[*]}" >&2
   #   exit 1
   # fi
-  d last_snap=$last_snap
 
   if [[ ! -e $svp ]]; then
     echo "$0: warning: subvol does not exist: $svp"
@@ -192,7 +214,7 @@ for d; do
       if [[ $cgen -gt $last_received_cgen ]]; then
         last_received_cgen=$cgen
         last_received=$f
-        elif [[ $last_received ]]; then
+      elif [[ $last_received ]]; then
         # optimization: we are looking in reverse order by date, so if
         # we find one that has a lesser cgen, assume the rest will all
         # be lesser.