various fixes
[distro-setup] / check-subvol-stale
index f278188fa63c6e58b00a76f66b20b971f15873fc..b977f763f5a85849e7da464c544724e4c908b06c 100644 (file)
 # limitations under the License.
 
 # usage: $0 SUBVOL_MOUNTPOINT...
-# if latest subvols $@ are not mounted, exit 1, print message, and touch /nocow/btrfs-stale/$subvol
+#
+# In git, this is not not executable because it's meant to be installed
+# using ./install-my-scripts
+#
+# If latest subvols $@ are not mounted, exit 1, print message, and touch
+# /nocow/btrfs-stale/$subvol
+#
+# Either SUBVOL_MOUNTPOINT is a snapshot of the latest, or
+# the latest snapshot is snapshot of SUBVOL_MOUNTPOINT.
 
 [[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
 
@@ -31,7 +39,7 @@ ret=0
 for d; do
     vol=${d##*/}
     cd /mnt/root/btrbk
-    snaps=($vol.20*)
+    snaps=($vol.20*) # Assumes we are in the 21st century.
     if [[ ! $snaps ]]; then
         # no snapshots yet
         continue
@@ -45,26 +53,56 @@ for d; do
         done | sort -r | head -n 1 | awk '{print $2}'
              )
     if [[ ! $last_snap ]]; then
-        echo "$d stale"
+        # should not happen.
+        echo "$0: error: could not find latest snapshot for $d among ${snaps[@]}"
         ret=1
         continue
     fi
-    stale=true
-    if btrfs sub show $d 2>/dev/null | sed '0,/^\t*Snapshot(s):/d;s/^\s*//' | \
+
+    # check that $d has $last_snap as a snapshot,
+    # or else $d is a snapshot of $last_snap. In the second
+    # case, we use a uuid comparison, which if I remember from the
+    # docs, is a bit more robust, perhaps to renames.
+    if btrfs sub show $d 2>/dev/null | sed '0,/^\s*Snapshot(s):/d;s/^\s*//' | \
             grep -xF btrbk/$last_snap &>/dev/null; then
         stale=false
     else
-        last_uuid=$(btrfs sub show $last_snap| awk '$1 == "UUID:" {print $2}')
-        if btrfs sub show $d| grep "^\s*Parent UUID:\s*$last_uuid$" &>/dev/null; then
+        last_snap_uuid=$(btrfs sub show $last_snap| awk '$1 == "UUID:" {print $2}')
+        if btrfs sub show $d| grep "^\s*Parent UUID:\s*$last_snap_uuid$" &>/dev/null; then
             stale=false
         fi
     fi
+
+    # check if $d is a snapshot of any of the btrbk backups
+    if [[ ! $stale ]]; then
+        for f in ${snaps[@]}; do
+            if [[ $f == $last_snap ]]; then continue; fi
+            uuid=$(btrfs sub show $f| awk '$1 == "UUID:" {print $2}')
+            if btrfs sub show $d| grep "^\s*Parent UUID:\s*$uuid$" &>/dev/null; then
+                stale=true
+                echo "$d stale: it's a snapshot of $f"
+                break
+            fi
+        done
+    fi
+    # check if $d generation is later than last_snap
+    if [[ ! $stale ]]; then
+        last_snap_gen=$(btrfs sub show $last_snap| awk '$1 == "Generation:" {print $2}')
+        d_gen=$(btrfs sub show $d| awk '$1 == "Generation:" {print $2}')
+        if (( last_snap_gen < d_gen )); then
+            stale=false
+        else
+            echo "$d stale: it's generation, $d_gen, is earlier than the last snapshot's, $last_snap_gen"
+            stale=true
+        fi
+    fi
+
+
     stale_dir=/nocow/btrfs-stale
     stale_file=$stale_dir/$vol
     if $stale; then
         mkdir -p $stale_dir
         printf "%s\n" $last_snap > $stale_file
-        echo "$d stale"
         ret=1
         continue
     else
@@ -72,4 +110,3 @@ for d; do
     fi
 done
 exit $ret
-