ZFS snapshots meet automounter and have a happy time together

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/libc_hwcap1.so.1
                       7.3G   6.7G   574M    93%    /lib/libc.so.1
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:

Comments:

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.

Posted by David A. Dempsey on August 02, 2006 at 12:32 PM BST #

Post a Comment:
Comments are closed for this entry.
About

This is the old blog of Chris Gerhard. It has mostly moved to http://chrisgerhard.wordpress.com

Search

Archives
« April 2014
MonTueWedThuFriSatSun
 
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
    
       
Today