[OpenIndiana-discuss] Finding the "last" snapshot for a zfs volume

Discussion list for OpenIndiana openindiana-discuss at openindiana.org
Wed May 14 00:54:30 UTC 2014


> From: Discussion list for OpenIndiana [mailto:openindiana-
> discuss at openindiana.org]
> 
> On our backup servers I have a "backup-check" script that checks to see
> what the last snapshot for a particular volume is (and compares it with the
> current date)

This is how I do it:
By doing "zfs list" once and caching in tmpfile, I can easily parse with grep many times over.

Obviously, according to the script below, my snapshots are named @AutoRotate-`date '+%F'`

#!/usr/bin/bash
export PATH=/usr/gnu/bin:/usr/bin:/usr/sbin:/sbin
export TODAY=`date '+%F'`
TMPFILE=`mktemp /tmp/tmp.XXXXXXXX`    # mktemp will randomize XXXXXXX
zfs list -H -o name -t snapshot > $TMPFILE

filesystems=`zfs list -H -o name -t filesystem,volume | egrep -v 'rpool/dump|rpool/swap'`
for fs in $filesystems ; do 
    latest=`grep "^${fs}@" $TMPFILE | tail -1`
    if ( echo $latest | grep -q $TODAY ) ; then
        echo "good      $latest"
    else
        if ( grep -q "^${fs}@" $TMPFILE ) ; then
            latest=`echo $latest | sed 's/^.*@AutoRotate-//'`
            echo "outdated $latest $fs"
        else
            echo "not found $fs"
        fi
    fi
done
rm $TMPFILE



More information about the openindiana-discuss mailing list