X-Git-Url: http://git.pippins.net/embedvideo/.git/?a=blobdiff_plain;f=zfs-autosnap;h=09e1a629b21ece6dbdd7a2f46c18aff4464b734a;hb=7f9a942edf3de137e1f244b5a680d5ec993171cc;hp=795c80259d1cd28aca9c8215b1558603b799e080;hpb=698062c29aad5253bc10d488958de289055429e5;p=zfs-ubuntu%2F.git diff --git a/zfs-autosnap b/zfs-autosnap index 795c802..09e1a62 100755 --- a/zfs-autosnap +++ b/zfs-autosnap @@ -5,9 +5,14 @@ # It also employs an intelligent algorithm to roll off, # or destroy, old snapshots. -test=0 +# source our configuration +config="${0%/*}/zfs-scripts.conf" +[ -e "${config}.dist" ] && . ${config}.dist +[ -e "${config}" ] && . ${config} -[ $test == 0 ] && exec >> /var/log/zfs/zfs-autosnap.log 2>&1 +if [[ -z "$SNAP_UNDER_TEST" ]]; then + exec >> $logdir/zfs-autosnap.log 2>&1 +fi # This script makes the following assumptions/requirements: # * this script only handles one zfs filesystem, a wrapper should be created @@ -21,21 +26,33 @@ datetime_to_minutes() { perl -n -e '/(\d+)-(\d+)-(\d+)\.(\d+)\.(\d+)/; print $1 * 527040 + $2 * 44640 + $3 * 1440 + $4 * 60 + $5,"\n"' } +# This converts date/time from YYYY-MM-DD.hh.mm to an integer that aligns +# things to prefer certain times such as the first of the month or midnight, +# etc datetime_to_minutes2() { - perl -n -e '/(\d+)-(\d+)-(\d+)\.(\d+)\.(\d+)/; $monthadj=int(($2-1)/3)-1; $minadj=int($5/15); $houradj=int($4/3); print $1 * 1048576 + ( $2 + $monthadj ) * 65536 + $3 * 2048 + ( $4 + $houradj ) * 64 + $5 + $minadj,"\n"' + perl -n -e '/(\d+)-(\d+)-(\d+)\.(\d+)\.(\d+)/; + $monthadj=int(($2-1)/3)-1; # Prefer months numbered 1,4,7 and 10 + $dayadj=$3 == 1 ? -1 : 0; # Make sure day 1 is prefered + $minadj=int($5/15); # Prefer multiples of 15 minutes + $houradj=int($4/3); # Prefer midnight,noon,etc + $intvalue=( + 1048576 * $1 + + 65536 * ( $2 + $monthadj ) + + 2048 * ( $3 + $dayadj ) + + 64 * ( $4 + $houradj ) + + $5 + $minadj + ); + print $intvalue,"\n"' } -# test: if set to 1, no zfs snapshot commands will be run, they will only be echoed # filesystem: This is the zfs filesystem to snapshot # mountpoint: This is the mountpoint of the zfs filesystem to snapshot # numsnapshots: This number is the number of equally spaced snapshots that should exist over any given period in the past # maxagedays: This is the maximum number of days to keep any snapshot around for (0=infinite) (default=0). filesystem=$1 -mountpoint=$2 -numsnapshots=${3-20} +mountpoint=${2-/$1} +numsnapshots=${3-12} maxagedays=${4-0} - -lockdir="/tmp/zfs-admin-lock" pool=`echo "$filesystem" | awk -F '/' '{ print $1 }'` if [ -z "$filesystem" ] || [ -z "$mountpoint" ] || [ -z "$numsnapshots" ] || [ -z "$maxagedays" ]; then @@ -43,40 +60,65 @@ if [ -z "$filesystem" ] || [ -z "$mountpoint" ] || [ -z "$numsnapshots" ] || [ - exit 1 fi -if [ ! -d "$mountpoint" ]; then +if [ -z "$SNAP_UNDER_TEST" -a ! -d "$mountpoint" ]; then echo "-E- Unable to find the mountpoint: $mountpoint" exit 1 fi -snapshotdir="${mountpoint}/.zfs/snapshot" +if [ -n "$SNAP_UNDER_TEST" ]; then + snapshotdir="./snapshot" +else + snapshotdir="${mountpoint}/.zfs/snapshot" +fi + if [ ! -d "$snapshotdir" ]; then echo "-E- Unable to find the snapshotdir: $snapshotdir" exit 1 fi -# Check to see if this zfs pool has a scrub being performed on it now. +# 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. -zpool status $pool | grep scrub: | 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 +if [ -z "$SNAP_UNDER_TEST" ]; then + 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 + fi fi +snapshot() { + echo "-I- Creating $1" + if [ -z "$SNAP_UNDER_TEST" ]; then + zfs snapshot "$1" + else + mkdir -p snapshot/$(dirname "$(echo "$1" | sed 's,.*@,,')") + touch snapshot/"$(echo "$1" | sed 's,.*@,,')" + fi +} + +destroy() { + echo "-I- Destroying old $1" + if [ -z "$SNAP_UNDER_TEST" ]; then + zfs destroy "$1" + else + rm -f "$1" + fi +} + # Get the various components of the date datetime=${ZFSDATETIME:-$(date +%Y-%m-%d.%H.%M)} # Create the snapshot for this minute -echo "-I- Creating ${filesystem}@${datetime}" -[ $test == 0 ] && zfs snapshot "${filesystem}@${datetime}" +snapshot "${filesystem}@${datetime}" minutes=$(echo $datetime | datetime_to_minutes) -# Check to ensure the zfs filesystem has not been locked down. -# If it has, we cannot perform any snapshot destroy operations. -if [ -d "$lockdir" ]; then - echo "-W- The zfs filesystem has been locked down. Skipping snapshot cleanup." - exit 0 +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) @@ -91,9 +133,8 @@ for snapshot in $snapshots; do 0) ;; *) snapname=${filesystem}$(echo "$snapshot" | - sed 's,/\(.*\)/.zfs/snapshot/\(.*\),@\2,') - echo "-I- Destroying $snapname" - [ $test == 0 ] && zfs destroy "$snapname" + sed 's,/\(.*\)/.zfs/snapshot/\(.*\),@\2,') + destroy "$snapname" ;; esac break @@ -102,10 +143,7 @@ for snapshot in $snapshots; do done if [ $maxagedays -gt 0 ] && [ $age -gt $((maxagedays * 24 * 60)) ]; then snapname=${filesystem}$(echo "$snapshot" | - sed 's,/\(.*\)/.zfs/snapshot/\(.*\),@\2,') - echo "-I- Destroying old $snapname" - [ $test == 0 ] && zfs destroy "$snapname" + sed 's,/\(.*\)/.zfs/snapshot/\(.*\),@\2,') + destroy "$snapname" fi done - -true