Fix mountpoint after full fs transfer if needed
[zfs-ubuntu/.git] / zfs-autosnap
index e7d939e9746453acfc0ac73d58f3cb5775f163fb..4c26aa9404ea5ed07b5965f4296bd38494e386e0 100755 (executable)
@@ -5,10 +5,13 @@
 #              It also employs an intelligent algorithm to roll off,
 #              or destroy, old snapshots.
 
-PATH=/usr/sbin:/sbin:$PATH
+# source our configuration
+config="${0%/*}/zfs-scripts.conf"
+[ -e "${config}.dist" ] && . ${config}.dist
+[ -e "${config}" ] && . ${config}
 
-if [ -z "$SNAP_UNDER_TEST" ]; then
-    exec >> /var/log/snap.log 2>&1
+if [[ -z "$SNAP_UNDER_TEST" ]]; then
+    exec >> $logdir/zfs-autosnap.log 2>&1
 fi
 
 # This script makes the following assumptions/requirements:
@@ -50,7 +53,6 @@ filesystem=$1
 mountpoint=${2-/$1}
 numsnapshots=${3-12}
 maxagedays=${4-0}
-lockdir="/tmp/${filesystem}.lock"
 pool=`echo "$filesystem" | awk -F '/' '{ print $1 }'`
 
 if [ -z "$filesystem" ] || [ -z "$mountpoint" ] || [ -z "$numsnapshots" ] || [ -z "$maxagedays" ]; then
@@ -64,20 +66,15 @@ if [ -z "$SNAP_UNDER_TEST" -a ! -d "$mountpoint" ]; then
 fi
 
 if [ -n "$SNAP_UNDER_TEST" ]; then
-    snapshotdir="./snapshot"
+    snapshots=$(ls -d ./snapshot/????-??-??.??.?? 2>/dev/null)
 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.
 # If it does, we cannot perform any snapshot create or destroy operations.
 if [ -z "$SNAP_UNDER_TEST" ]; then
-    zpool status $pool | grep scrub: | grep "in progress" > /dev/null 2>&1
+    zpool status $pool | grep scan: | grep "in progress" > /dev/null 2>&1
     if [ $? == 0 ]; then
        echo "-W- The zfs pool '$pool' is currently being scrubbed. Skipping all snapshot operations."
        exit 0
@@ -111,15 +108,14 @@ snapshot "${filesystem}@${datetime}"
 
 minutes=$(echo $datetime | datetime_to_minutes)
 
-lockdir="/tmp/zfs-admin-lock"
 if ! mkdir "$lockdir" >/dev/null 2>&1; then
+  echo "-W- The zfs filesystem has been locked down. Skipping snapshot cleanup."
   exit 0
 fi
 cleanup() { rm -rf "$lockdir"; }
 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)
@@ -129,19 +125,13 @@ for snapshot in $snapshots; do
     if [ $age -lt $((window * numsnapshots)) ]; then
       case $((snapminutes2 % window)) in
         0) ;;
-        *)
-          snapname=$(echo "$snapshot" |
-                       sed 's,/\(.*\)/.zfs/snapshot/\(.*\),\1@\2,')
-          destroy "$snapname"
-        ;;
+        *) destroy "$snapshot" ;;
       esac
       break
     fi
     window=$((window*2))
   done
   if [ $maxagedays -gt 0 ] && [ $age -gt $((maxagedays * 24 * 60)) ]; then
-    snapname=$(echo "$snapshot" |
-                     sed 's,/\(.*\)/.zfs/snapshot/\(.*\),\1@\2,')
-    destroy "$snapname"
+    destroy "$snapshot"
   fi
 done