Blogs Blogs


Tip of the day: Snapshots in ZFS, part I

You probably already know how useful filesystem snapshots can be. ZFS, in particular, has a very flexible and powerful implementation of this mechanism. Quoting the zfs(1M) man page:

        A snapshot is a read-only copy of a file system  or  volume.  Snapshots
        can  be  created extremely quickly, and initially consume no additional
        space within the pool. As data within the active dataset  changes,  the
        snapshot  consumes  more  data  than would otherwise be shared with the
        active dataset.

Creating snapshots is easy:

zfs snapshot [-r] [-o property=value]...

Of special interest is the -r option. Quoting again the man page,

                Recursively  create snapshots of all descendent datasets. Snap-
                shots are taken atomically, so  that  all  recursive  snapshots
                correspond to the same moment in time.

For example, to take a snapshot of all filesystems in the pool zroot, you'd execute:

zfs snapshot -r zroot@somesnapshotname

The importance of the atomical nature of the process cannot be emphasized well enough. A complete pool consisting of tens of filesystems (actually, the number doesn't matter much) is processed in an instant. This makes creating backups of complete systems a breeze (I'll cover zfs send-ing and receive-ing in a separate blog entry, but don't hesitate to check the man page again).

Listing the snapshots on a system is possible with the zfs list subcommand:

        zfs list [-rH] [-o property[,...]] [ -t type[,...]]
        [ -s property ] ... [ -S property ] ... [filesystem|volume|snapshot]

Here's the simplest way to use it, which shows all snapshots on all filesystems and volumes:

zfs list -t snapshot

Deleting snapshots is also easy. However, be very careful when you specify the name of the snapshot. If you accidentally omit the snapshot part (@snapshotname), and especially if you're using the -r option, you could easily lose lots of information without any way to get it back (save from a backup). Again, double check that you've specified a snapshot name (and the correct one), and not the filesystem name alone.

Here's how to destroy all snapshots we created earlier:

zfs destroy -r zroot@somesnapshotname

And for a third time, DO NOT do this, unless it is what you want (and it means virtually emptying the pool):

zfs destroy -r zroot

I'll leave the topic on how to actually use snapshots for the next blog entry, but you might check the relevant entry in the ZFS Best Practices Guide meanwhile.

Trackback URL: