Changed from looking directly in .zfs for snaps.
authorAlan J. Pippin <ajp@pippins.net>
Sun, 21 May 2023 15:41:38 +0000 (09:41 -0600)
committerAlan J. Pippin <ajp@pippins.net>
Sun, 21 May 2023 15:41:38 +0000 (09:41 -0600)
Use zfs command to avoid automounting of .zfs.

Change-Id: Iab631a071fb8a335244d42b5e7b801f76b0ce2b0

zfs-autosnap

index 09e1a629b21ece6dbdd7a2f46c18aff4464b734a..4c26aa9404ea5ed07b5965f4296bd38494e386e0 100755 (executable)
@@ -66,14 +66,9 @@ if [ -z "$SNAP_UNDER_TEST" -a ! -d "$mountpoint" ]; then
 fi
 
 if [ -n "$SNAP_UNDER_TEST" ]; then
 fi
 
 if [ -n "$SNAP_UNDER_TEST" ]; then
-    snapshotdir="./snapshot"
+    snapshots=$(ls -d ./snapshot/????-??-??.??.?? 2>/dev/null)
 else
 else
-    snapshotdir="${mountpoint}/.zfs/snapshot"
-fi
-
-if [ ! -d "$snapshotdir" ]; then
-   echo "-E- Unable to find the snapshotdir: $snapshotdir"
-   exit 1
+    snapshots=$(zfs list -H -t snapshot -o name $filesystem | grep -P "\d+-\d+\.\d+\.\d+")
 fi
 
 # Check to see if this zfs filesystem has a scrub being performed on it now.
 fi
 
 # Check to see if this zfs filesystem has a scrub being performed on it now.
@@ -121,7 +116,6 @@ cleanup() { rm -rf "$lockdir"; }
 trap cleanup EXIT
 
 # Trim them down
 trap cleanup EXIT
 
 # Trim them down
-snapshots=$(ls -d ${snapshotdir}/????-??-??.??.?? 2>/dev/null)
 for snapshot in $snapshots; do
   snapminutes=$(echo "$snapshot" | sed 's,.*/,,' | datetime_to_minutes)
   snapminutes2=$(echo "$snapshot" | sed 's,.*/,,' | datetime_to_minutes2)
 for snapshot in $snapshots; do
   snapminutes=$(echo "$snapshot" | sed 's,.*/,,' | datetime_to_minutes)
   snapminutes2=$(echo "$snapshot" | sed 's,.*/,,' | datetime_to_minutes2)
@@ -131,19 +125,13 @@ for snapshot in $snapshots; do
     if [ $age -lt $((window * numsnapshots)) ]; then
       case $((snapminutes2 % window)) in
         0) ;;
     if [ $age -lt $((window * numsnapshots)) ]; then
       case $((snapminutes2 % window)) in
         0) ;;
-        *)
-          snapname=${filesystem}$(echo "$snapshot" |
-                                  sed 's,/\(.*\)/.zfs/snapshot/\(.*\),@\2,')
-          destroy "$snapname"
-        ;;
+        *) destroy "$snapshot" ;;
       esac
       break
     fi
     window=$((window*2))
   done
   if [ $maxagedays -gt 0 ] && [ $age -gt $((maxagedays * 24 * 60)) ]; then
       esac
       break
     fi
     window=$((window*2))
   done
   if [ $maxagedays -gt 0 ] && [ $age -gt $((maxagedays * 24 * 60)) ]; then
-    snapname=${filesystem}$(echo "$snapshot" |
-                            sed 's,/\(.*\)/.zfs/snapshot/\(.*\),@\2,')
-    destroy "$snapname"
+    destroy "$snapshot"
   fi
 done
   fi
 done