ZFS snapshots in a cron job


OK, so at work there is a backup server that has various cronjob scripts that run nightly to backup various production servers. This machine was recently upgrade to accommodate larger hard disks. We took the opportunity to use Solaris 11 with ZFS. Since we already use cronjobs, I wanted to have the automatic snapshots and backup scripts all controlled from one place, rather than using svcs.

At midnight every night the backup scripts launch. So just before this, at half 11, the snapshot_daily script will launch.

#!/bin/bash
unset PATH

DATE=/usr/bin/date
GREP=/usr/bin/grep
XARGS=/usr/bin/xargs
ZFS=/usr/sbin/zfs
HEAD=/usr/gnu/bin/head

RETENTION=7
POOL=datapool

#take daily snapshot
$ZFS snapshot ${POOL}@daily_`$DATE +%m%d%Y`

#remove any daily snapshots older than RETENTION days
$ZFS list -rt snap -H -o name ${POOL} | $GREP daily | $HEAD -n -${RETENTION} | $XARGS -n 1 zfs destroy

The general idea is that it makes the daily snapshot and lists the daily snapshots in order (newest to oldest), but skips the first few denoted by the RETENTION variable (in this case set to 7). Then it deletes them.

I know there are other more sophisticated ways of doing this, but I like the simplicity.

ZFS Install on Centos

Quick notes for myself really. I wanted to install zfs-fuse on centos. This is how I did it.

sudo rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm
yum install zfs-fuse