ZFS Snapshots and Clones

ZFS Snapshots and Clones


ZFS is a copy-on-write file system. This means that we can take snapshot of the volume or the entire zfs file system with no extra cost of space. The snapshot can be created instantly with virtually zero space usage. When changes are made, only the delta is written to the snapshot. We can go back to a snapshot by rolling back or we can even peek into what’s inside of the snapshot without having to perform a complete rollback.

In this guide we will demonstrate ZFS snapshots and rollbacks.

I have a dataset called /dalpool/compressed. This dataset contains a directory with some text files as follows:

Let’s take a snapshot of /dalpool/compressed.

zfs snapshot dalpool/compressed@09-03-2015

zfs list –t snapshot

Let’s delete all the /dalpool/conf/* files.

Let’s create a new file.

Let’s now roll back to the snapshot we took earlier. Note that the randfile is gone.

zfs rollback dalpool/compressed@09-03-2015

We can rename the snapshot from dalpool/compressed@09-03-2015 to something else like dalpool/compressed@mysnapshot

zfs rename dalpool/compressed@09-03-2015 dalpool/compressed@mysnapshot

We can also take a peek into the snapshot without having to rollback.

And of course we can list the contents of conf within this snapshot.

It is worth noting that the space taking by the snapshot is minimal. The attribute “REFER” tells us the amount of space shared between the snapshot and the file system.

Suppose that I have this snapshot called snap1

At this point, the REFER attribute is 236K. That is the amount of space shared between the snapshot and the file system.

Let’s create a 100M file and take another snapshot after that.

Again repeat the process to create yet another file and another snapshot.

As we can see from the above, the first time we created a 100M file and took a snapshot, the REFER attribute was 100M. After adding another 100M file, the second snapshot has a REFER value of 200M. This is what we expect to see.

Destroying a ZFS Snapshot

We can also destroy the snapshot using the following command:

zfs destroy dalpool/compressed@mysnapshot

ZFS Send and Receive

We can backup and move the snapshots using the send and receive command respectively.

ZFS Send:

zfs send dalpool/mydataset@snap1 > /home/chuong/sept2015.bak

ZFS Receive:

Let’s delete the snap1 snapshot then restore it using the receive command.

zfs receive anotherpool/mydataset < /home/chuong/sept2015.bak

The send and received commands can be combined into one:

zfs send dalpool/mydataset@sept2015 | zfs receive anotherpool/mydataset

Additionally we can send and receive snapshots between two different hosts via SSH.

remotehost# zfs create daltestpool/daldataset

myhost# zfs send dalpool/dataset@snap1 | ssh remotehost “zfs receive daltestpool/daldataset”

And to send only the delta (i.e., the incremental data), we do:

zfs send –i dalpool/dataset@snap1 | ssh remotehost “zfs receive daltestpool/daldataset”

ZFS Clones

While Snapshots are readonly copies, clones are writable. Clones have the same contents as the file system that it is created from. Clones can only be created from snapshots. Thus, these snapshots cannot be deleted until the clones have been deleted.

In the following, we list all the available snapshots. After that, clone the snap1 snapshot to be myclone1. Therefore, myclone1 should have the same amount of REFER size as snap1, which is 78.5K.

Destroying ZFS Clones

We can destroy clones exactly the same way we destroyed snapshots:

zfs destroy anotherpool/mydataset/myclone1

Note that the clone1 is gone from the list above!