various fixes
[distro-setup] / check-subvol-stale
index 127718257ba1000ac95dbe65964d296b726b4584..b977f763f5a85849e7da464c544724e4c908b06c 100644 (file)
@@ -58,7 +58,7 @@ for d; do
         ret=1
         continue
     fi
-    stale=true
+
     # 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
@@ -67,17 +67,42 @@ for d; do
             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