Skip to content

ZFS snapshots meet automounter and have a happy time together

December 1, 2005

There is a thread going over on zfs-interest where the following question was posed by biker (I live in hope that this is really “cyclist”):

How can I make a snapshot of home in zfs containing the data including the stuff within the user homes (home/ann, home/bob) – like a recursive snapshot.  The only way so far I could think of was  – copy the directory structure (home/ann, home/bob) to /snap  – initiate a snapshot of every dataset (home/ann, home/bob)  – mount each snapshot to the counterpart under /snap  – run the backup  – remove the mounts  – release the snapshots  – clear /snap  If there is something like a recursive snapshot or user and group quota in the classical sense, the efford needed could be minimized, …

It got me thinking that in the absence of a real solution this should be doable with a script. For the recursive backup script we have:

#!/bin/ksh -p  for filesystem in $(zfs list -H -o name -t filesystem) do         zfs snapshot $filesystem@$1 done

No prizes there but what biker wanted was a copy of the file system structure. The problem is that all those snapshots are each under the individual file systems .zfs/snapshot directory so are spread about.

If only we could mount all of them under one tree? By adding this line to /etc/auto_master:

/backup /root/auto_snapshot

and then this script as /root/auto_snapshot:

#!/bin/ksh -p  PATH=/usr/sbin:/sbin:$PATH  for filesystem in $(zfs list -H -o name -t filesystem) do if [[ -d /$filesystem/.zfs/snapshot/$1 ]] then         fs=${filesystem#*/}         if [[ ${fs} = ${filesystem} ]]         then                 fs=""         fi         ANS="${ANS:-}${ANS:+ }/$fs localhost:/$filesystem/.zfs/snapshot/$1" fi done echo $ANS

Suddenly I can do this:

1071 # ./backup fullbackup 1072 # (cd /backup/fullbackup ; tar cf /mypool/root/backup.tar . ) 1073 # df -h Filesystem             size   used  avail capacity  Mounted on /dev/dsk/c0d0s0        7.3G   6.7G   574M    93%    / /devices                 0K     0K     0K     0%    /devices ctfs                     0K     0K     0K     0%    /system/contract proc                     0K     0K     0K     0%    /proc mnttab                   0K     0K     0K     0%    /etc/mnttab swap                   829M   704K   829M     1%    /etc/svc/volatile objfs                    0K     0K     0K     0%    /system/object /usr/lib/libc/                        7.3G   6.7G   574M    93%    /lib/ fd                       0K     0K     0K     0%    /dev/fd swap                   829M   136K   829M     1%    /tmp swap                   829M    44K   829M     1%    /var/run mypool                 9.5G   5.8G   2.9G    67%    /mypool mypool/jfg             9.5G     8K   2.9G     1%    /mypool/jfg mypool/keg             9.5G    16M   2.9G     1%    /mypool/keg mypool/rlg             9.5G     8K   2.9G     1%    /mypool/rlg mypool/stc             9.5G    14M   2.9G     1%    /mypool/stc /mypool/cg13442        8.8G   5.8G   2.9G    67%    /home/cg13442 /mypool/.zfs/snapshot/fullbackup                        8.8G   3.9G   4.8G    45%    /backup/fullbackup /mypool/rlg/.zfs/snapshot/fullbackup                        4.8G     8K   4.8G     1%    /backup/fullbackup/rlg /mypool/jfg/.zfs/snapshot/fullbackup                        4.8G     8K   4.8G     1%    /backup/fullbackup/jfg /mypool/stc/.zfs/snapshot/fullbackup                        4.8G    14M   4.8G     1%    /backup/fullbackup/stc 1074 #

The tar backup file now contains the whole of the “fullback snapshot” and apart from the snapshot not really being atomic, since each file system is snapped in sequence this pretty much does what is wanted.

If you were really brave/foolish you could have the automounter executable maps generate the snapshots for you but that would be a receipe for filling the pool with snapshots. Deleting the snapshots is also a snip:

#!/bin/ksh -p  for filesystem in $(zfs list -H -o name -t filesystem) do         zfs destroy $filesystem@$1 done

Tags: topic:[OpenSolaris] topic:[Autofs] topic:[automount] topic:[ZFS]


From → Solaris

One Comment
  1. David A. Dempsey permalink

    This was incredibly helpful, but needed a couple of tweaks to the auto_snapshot script to make it work in my environment:
    1. Instead of -o name, use -o mountpoint
    2. Change all /$filesystem occurrences to $filesystem
    Now it builds the cascading structure whether or not the mount points are directly mounted under the pool, which ours are not.
    Thanks for the great scripts, I never would have figured this out without them.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: