BITPHONIC

Scheduling nightly ZFS snapshots

with one comment

It’s not often that I post stuff like this on my blog, but it is quite often that I find myself digging around the ‘net for this kind of information.

I needed to make a little shell script to make nightly ZFS snapshots, with a two week retention time. The great thing about Solaris is that all of it’s core utilities, like date are, well, egregiously out of date. If this were Linux, I could throw date a “–date ’14 days ago'” argument and be done with it, but instead I must ask Perl for help!

There’s some additional logic in there to check to see if the host is the active partition in a Solaris Cluster (it checks for the existence of the “/mailstore” partition).

#!/bin/bash
# Cron job to keep two weeks worth of 
# daily ZFS snapshots on mailstore, calendar.

# Today's date
DATE=`date +%m%d%Y`

# Munge the 14 days ago date from perl. Note the 14.
AGO=`perl -e 'use POSIX qw(strftime); print strftime "%m%d%Y", localtime(time()-3600*24*14);'`

CHK=`df -k | grep mailstore | cut -d ' ' -f 1`

# Am I the active host? Exits quietly if false.
if [ -n "$CHK" ]; then

        CHKAGO=`zfs list | grep mailstore@$AGO | cut -d ' ' -f 1`

        # Are there two week old snaps?
        if [ -n "$CHKAGO" ]; then
                # Destroy two week old snaps
                zfs destroy mailstore@$AGO
                zfs destroy calendar@$AGO 
        fi

        # Create today's snaps
        zfs snapshot mailstore@$DATE
        zfs snapshot calendar@$DATE
fi

Advertisements

Written by Adam N. Copeland

April 30, 2008 - 9:14 pm at 9:14 pm

Posted in work

One Response

Subscribe to comments with RSS.

  1. Hello Adam
    Thanks a lot for sharing this useful script which helped me to get a beginning work to take snapshot for my new environment.
    Certainly i measure the difference between what you provided and what i want to do.I know that we’ve not same aims but adaptation can be possible.
    So here is my config zones:
    Output:zfs list
    am 6,41G 13,2G 6,36G //zones/am
    am@20080922 38,5M – 6,33G –
    am@02102008 2,70M – 6,36G –
    am@28092008 1,47M – 6,36G –
    convergence 6,24G 13,3G 6,24G //zones/convergence
    im 5,96G 13,6G 5,96G //zones/im
    im@28092008 1,13M – 5,96G –
    ldapmaster 6,36G 42,6G 6,36G //zones/ldapmaster
    ldapslave 6,75G 42,2G 6,74G //zones/ldapslave
    messagestore1 18,0G 470G 18,0G //messagestore1
    messagestore2 92K 488G 24,5K //messagestore2
    messaging1 7,52G 21,8G 7,52G //zones/messaging1
    messaging2 3,31G 26,0G 3,31G //zones/messaging2
    mmp 3,57G 16,0G 3,57G //zones/mmp
    nasdata 93K 744G 24,5K //nasdata
    nasserver 3,31G 1,95T 3,31G //zones/nasserver
    rproxy 3,57G 25,7G 3,57G //zones/rproxy
    uwc 6,60G 13,0G 6,60G //zones/uwc

    Output:zpool list

    am 19,9G 6,41G 13,5G 32% ONLINE /
    convergence 19,9G 6,24G 13,6G 31% ONLINE /
    im 19,9G 5,96G 13,9G 29% ONLINE /
    ldapmaster 49,8G 6,36G 43,4G 12% ONLINE /
    ldapslave 49,8G 6,75G 43,0G 13% ONLINE /
    messagestore1 496G 18,0G 478G 3% ONLINE /
    messagestore2 496G 95K 496G 0% ONLINE /
    messaging1 29,8G 7,52G 22,2G 25% ONLINE /
    messaging2 29,8G 3,31G 26,4G 11% ONLINE /
    mmp 19,9G 3,57G 16,3G 17% ONLINE /
    nasdata 756G 96K 756G 0% ONLINE /
    nasserver 1,98T 3,31G 1,98T 0% ONLINE /
    rproxy 29,8G 3,57G 26,2G 12% ONLINE /
    uwc 19,9G 6,60G 13,3G 33% ONLINE /

    Output:zoneadm list
    global
    im
    uwc
    messaging2
    ldapmaster
    messaging1
    ldapslave
    nasserver
    mmp
    convergence
    rproxy
    am
    So starting from you works, i adapted the script as followed to take too daily snapshot for all above zones:
    #!/bin/bash
    # Today’s date
    set -x
    DATE=`date +%d%m%Y`

    # Munge the 5 days ago date from perl. Note the 5.
    AGO=`perl -e ‘use POSIX qw(strftime); print strftime “%d%m%Y”, localtime(time()-3600*24*5);’`
    echo $AGO

    CHK=`zpool list -H -o name`

    # Am I the active host? Exits quietly if false.
    if [ -n “$CHK” ]; then

    CHKAGO=`zfs list | egrep “[a-z]+@$AGO” | cut -d ‘ ‘ -f 1`
    #CHKAGO=`zfs list -H -t snapshot | egrep ‘[a-z]+@$AGO’ | cut -d ‘ ‘ -f 1`
    echo $CHKAGO

    # Are there five days old snaps?
    if [ -n “$CHKAGO” ]; then
    Destroy the five days old snaps
    /usr/sbin/zfs destroy $CHKAGO
    fi

    # Create today’s snaps
    #zfs snapshot $CHK@$DATE

    while (zones in $CHK);

    do
    zfs /opt/SUNWexplo/bin/snapshot $CHK@$DATE;

    done
    fi

    Here is errors i get after running the script:
    root@jesmail absi > ./Test_issa
    +++ date +%d%m%Y
    ++ DATE=03102008
    +++ perl -e ‘use POSIX qw(strftime); print strftime “%d%m%Y”, localtime(time()-3600*24*5);’
    ++ AGO=28092008
    ++ echo 28092008
    28092008
    +++ zpool list -H -o name
    ++ CHK=’am
    convergence
    im
    ldapmaster
    ldapslave
    messagestore1
    messagestore2
    messaging1
    messaging2
    mmp
    nasdata
    nasserver
    rproxy
    uwc’
    ++ ‘[‘ -n ‘am
    convergence
    im
    ldapmaster
    ldapslave
    messagestore1
    messagestore2
    messaging1
    messaging2
    mmp
    nasdata
    nasserver
    rproxy
    uwc’ ‘]’
    +++ zfs list
    +++ egrep ‘[a-z]+@28092008’
    +++ cut -d ‘ ‘ -f 1
    ++ CHKAGO=’am@28092008
    im@28092008’
    ++ echo am@28092008 im@28092008
    am@28092008 im@28092008
    ++ ‘[‘ -n ‘am@28092008
    im@28092008’ ‘]’
    ++ Destroy the five days old snaps
    ./Test_issa: line 24: Destroy: command not found
    ++ /usr/sbin/zfs destroy am@28092008 im@28092008
    too many arguments
    usage:
    destroy [-rRf]

    For the property list, run: zfs set|get
    ++ zones in am convergence im ldapmaster ldapslave messagestore1 messagestore2 messaging1 messaging2 mmp nasdata nasserver rproxy uwc
    ./Test_issa: line 32: zones: command not found.

    Sure that i’ll continue working on it.
    I’m new in solaris system and i just need to know if i’m on good way .If yes what i’m missing!!!

    Regards

    issa kandji

    October 3, 2008 - 7:30 am at 7:30 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: