Automating ZFS snapshots with the SMF auto-snapshot service

One of the nice features of ZFS is the ability to take file system snapshots, which you can then use to recover perviously deleted data. In recent opensolaris and Nevada builds, there are several auto-snapshot services that can be used to schedule hourly, daily, weekly and monthly snapshots:

$ svcs -a | grep auto-snapshot
disabled 9:28:28 svc:/system/filesystem/zfs/auto-snapshot:frequent
online 9:28:53 svc:/system/filesystem/zfssnap-roleadd:default
online 12:55:54 svc:/system/filesystem/zfs/auto-snapshot:daily
online 12:56:02 svc:/system/filesystem/zfs/auto-snapshot:weekly
online 12:56:11 svc:/system/filesystem/zfs/auto-snapshot:monthly
online 12:58:37 svc:/system/filesystem/zfs/auto-snapshot:hourly

To enable scheduled snapshots (these services are disabled by default), you can enable one or more of these services with svcadm. Once enabled, these services will create a cron entry in the zfssnap users crontab:

$ cat /var/spool/cron/crontabs/zfssnap
0 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 * * /lib/svc/method/zfs-auto-snapshot svc:/system/filesystem/zfs/auto-snapshot:daily
0 0 1,8,15,22,29 * * /lib/svc/method/zfs-auto-snapshot svc:/system/filesystem/zfs/auto-snapshot:weekly
0 0 1 1,2,3,4,5,6,7,8,9,10,11,12 * /lib/svc/method/zfs-auto-snapshot svc:/system/filesystem/zfs/auto-snapshot:monthly
0 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 * * * /lib/svc/method/zfs-auto-snapshot svc:/system/filesystem/zfs/auto-snapshot:hourly

The cron jobs are used to schedule the automated snapshots, and are added and removed when one of the services are enabled or disabled. I’m not entirely clear why the auto-snapshot author didn’t use “*” in the daily and hourly entries, but hopefully there is a good reason.

To view the list of snapshots on a host, you can use the zfs utility:

$ zfs list -r -t snapshot | head -5

NAME                                                       USED  AVAIL  REFER  MOUNTPOINT
bits@zfs-auto-snap:daily-2009-06-18-12:55                     0      -  34.4K  -
bits@zfs-auto-snap:weekly-2009-06-18-12:56                    0      -  34.4K  -
bits@zfs-auto-snap:monthly-2009-06-18-12:56                   0      -  34.4K  -
bits@zfs-auto-snap:daily-2009-06-19-00:00                     0      -  34.4K  -

If you need to recover a file that was previously deleted, you can cd into the “.zfs/snapshot” directory in the file system that contained the deleted data:

$ zfs list bits/home

bits/home   181K  1.17T   149K  /home

$ cd /home/.zfs/snapshot

Locate the correct snapshot to recover the file from with ls:

$ ls -l | tail -5
drwxr-xr-x 3 root root 3 May 19 22:43 zfs-auto-snap:hourly-2009-06-20-09:00
drwxr-xr-x 3 root root 3 May 19 22:43 zfs-auto-snap:hourly-2009-06-20-10:00
drwxr-xr-x 3 root root 3 May 19 22:43 zfs-auto-snap:hourly-2009-06-20-11:00
drwxr-xr-x 3 root root 3 May 19 22:43 zfs-auto-snap:monthly-2009-06-18-12:56
drwxr-xr-x 3 root root 3 May 19 22:43 zfs-auto-snap:weekly-2009-06-18-12:56

And then recover the file with cp (or a restore program):

$ find . -name importantfile

$ cp matty/importantfile /tmp

$ ls -la /tmp/importantfile
-rw-r–r– 1 root root 101186 Jun 20 11:30 /tmp/importantfile

This is pretty sweet, and being able to enable automated snapshots with a couple of svcadm invocations is super convenient!

4 thoughts on “Automating ZFS snapshots with the SMF auto-snapshot service”

  1. Yeah dude, this is really cool that this feature got into Navada.

    Automating snapshots is one really cool feature on NetApp’s ONTAP and their removal as well. =) I was thinking that this would be somewhat easy to script, but I think its sweet that someone made this into a SMF service with a easy tunable and cron entries.

  2. Yeah, ZFS Auto Snapshots is a killer feature of Opensolaris, I love it. Actually I’ve built a small home NAS using OS2009.06 and Samba.

    Though, by default ZFS Auto Snapshots aren’t /visible/ when accessed with Samba from a Windows client (because of the colon in their name).

    I’ve written a post about a small hack to have the snapshots appear correctly when using Samba, thus enabling access to them and recovery of old file versions from a Windows client:

  3. I’m running Solaris 10 update 7 from 5/09 but cannot find the automated ZFS snapshot manifest. Do you know where I can get it? Thanks.

  4. What about rotation?

    If you cron or automate the snapshot rotation, over time you’ll end up with a lot of snapshots, the oldest of which you might not need anymore.

    Is there a SMF facility or option to an existing facility to rotate snapshots? (i.e. remove the oldest after a certain time period)

Leave a Reply

Your email address will not be published. Required fields are marked *