From: Alan J. Pippin Date: Sat, 10 Jan 2009 23:34:55 +0000 (-0700) Subject: Added new script to link the GIT scripts to another location X-Git-Url: http://git.pippins.net/embedvideo/.git/?a=commitdiff_plain;h=ffae499fdb9dc7c65114534338857efc6e8b0c87;p=zfs-ubuntu%2F.git Added new script to link the GIT scripts to another location Picked up bug fixes from Carl for zfs-autosnap --- diff --git a/update_links b/update_links new file mode 100755 index 0000000..8370aca --- /dev/null +++ b/update_links @@ -0,0 +1,4 @@ +#!/bin/bash + +git ls-files | grep -v .gitignore | grep -v wrapper | xargs -I % ln -s zfs/% /etc/bin/% 2>/dev/null + diff --git a/zfs-autosnap b/zfs-autosnap index 795c802..e7d939e 100755 --- a/zfs-autosnap +++ b/zfs-autosnap @@ -5,9 +5,11 @@ # It also employs an intelligent algorithm to roll off, # or destroy, old snapshots. -test=0 +PATH=/usr/sbin:/sbin:$PATH -[ $test == 0 ] && exec >> /var/log/zfs/zfs-autosnap.log 2>&1 +if [ -z "$SNAP_UNDER_TEST" ]; then + exec >> /var/log/snap.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 +23,34 @@ 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" +lockdir="/tmp/${filesystem}.lock" pool=`echo "$filesystem" | awk -F '/' '{ print $1 }'` if [ -z "$filesystem" ] || [ -z "$mountpoint" ] || [ -z "$numsnapshots" ] || [ -z "$maxagedays" ]; then @@ -43,40 +58,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 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 + 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 +lockdir="/tmp/zfs-admin-lock" +if ! mkdir "$lockdir" >/dev/null 2>&1; then + exit 0 fi +cleanup() { rm -rf "$lockdir"; } +trap cleanup EXIT # Trim them down snapshots=$(ls -d ${snapshotdir}/????-??-??.??.?? 2>/dev/null) @@ -90,10 +130,9 @@ for snapshot in $snapshots; do case $((snapminutes2 % window)) in 0) ;; *) - snapname=${filesystem}$(echo "$snapshot" | - sed 's,/\(.*\)/.zfs/snapshot/\(.*\),@\2,') - echo "-I- Destroying $snapname" - [ $test == 0 ] && zfs destroy "$snapname" + snapname=$(echo "$snapshot" | + sed 's,/\(.*\)/.zfs/snapshot/\(.*\),\1@\2,') + destroy "$snapname" ;; esac break @@ -101,11 +140,8 @@ for snapshot in $snapshots; do window=$((window*2)) 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" + snapname=$(echo "$snapshot" | + sed 's,/\(.*\)/.zfs/snapshot/\(.*\),\1@\2,') + destroy "$snapname" fi done - -true