ZFS Tutorials : Creating ZFS snapshot and clones

por | 24 agosto, 2016

ZFS Tutorials : Creating ZFS snapshot and clones

ZFS snapshots

zfs snapshot is a read-only copy of zfs file system or volume. They consume no extra space in the zfs pool and can be created instantly. They can be used to save a state of file system at particular point of time and can later be rolled back to exactly same state. You can also extract some of the files from the snapshot if required and not doing a complete roll back. Let us take a look at the steps involved in creating a snapshot of zfs file system.

Creating and destroying snapshots

To create a new snapshot of the file system geekpool/fs1

# zfs snapshot geekpool/fs1@oct2013
# zfs snapshot -r geekpool/fs1@oct2013    (to take snapshot of all FS under fs1)
# zfs list -t snapshot
NAME                   USED  AVAIL  REFER  MOUNTPOINT
geekpool/fs1@oct2013      0      -    31K  -

To destroy the snapshot :

# zfs destroy geekpool/fs1@oct2013

To rename a snapshot :

# zfs rename geekpool/fs1@oct2013 geekpool/fs1@sept2013

Rolling back to a snapshot

Now we can completely roll back to an older snapshot which will give us the point in time copy at the time snapshot was taken.

zfs rollback geekpool/fs1@oct2012

You can also restore individual file by accessing the snapshot directory .zfs/snapshot/snapshot_name created under the file system for which snapshot was taken. Under the snapshot directory you can find the file or directory you want to restore to a older time.

# ls /geekpool/fs1/.zfs/snapshot/
oct2013

Space taken by a snapshots

Now when you first create any snapshot the attribute “refer” specifies the space shared between the snapshot and the file system. When you make any changes to the file system the refer space increase.

# zfs list -t all -r geekpool
NAME                   USED  AVAIL  REFER  MOUNTPOINT
geekpool               520K   975M    32K  /geekpool
geekpool/fs1            50K   975M    31K  /geekpool/fs1
geekpool/fs1@oct2013    19K      -    31K  -

Creat some files and snapshots

# mkfile 100m /geekpool/fs1/test1
# zfs snapshot geekpool/fs1@nov2013
# mkfile 100m /geekpool/fs1/test2
# zfs snapshot geekpool/fs1@dec2013
# zfs list -t all -r geekpool
NAME                   USED  AVAIL  REFER  MOUNTPOINT
geekpool               201M   775M    32K  /geekpool
geekpool/fs1           200M   775M   200M  /geekpool/fs1
geekpool/fs1@oct2013    19K      -    31K  -
geekpool/fs1@nov2013    19K      -   100M  -
geekpool/fs1@dec2013      0      -   200M  -

Now as expected nov2013 snapshot has used 100MB space where as dec2013 has used 200MB space.

ZFS send and receive

ZFS has a option to backup or move the snapshots. This is possible with the send and receive commands.
ZFS send
You can save the output stream generated by the send command to a file by redirection.

# zfs send geekpool/fs1@oct2013 > /geekpool/fs1/oct2013.bak

ZFS receive
Similar to send you can recieve a snapshot from the file we just created.

# zfs receive anotherpool/fs1 < /geekpool/fs1/oct2013.bak

We can also combine both the operations

# zfs send geekpool/fs1@oct2013 | zfs receive anotherpool/fs1

Now we can send and receive asnapshots between 2 different systems in one go as below :

node02 # zfs create testpool/testfs  (create a test file-system on another system)
node01 # zfs send geekpool/fs1@oct2013 | ssh node02 "zfs receive testpool/testfs"

To send only the incremental data

node01# zfs send -i geekpool/fs1@oct2013| ssh node02 zfs recv testpool/testfs

ZFS clones

ZFS clones as contrary to ZFS snapshots are writable copy of the file system with initial content same as the file system. Clones can only be created from snapshots. Snapshot can’t be delete until you have delete the clone created from it.

Creating ZFS clones

Now we can create a clone from a snapshot anywhere under the file system where the snapshot recides.

# zfs clone geekpool/fs1@oct2013 geekpool/fs1/clone01
# zfs list -r
NAME                   USED  AVAIL  REFER  MOUNTPOINT
geekpool               101M   875M    32K  /geekpool
geekpool/fs1           100M   875M   100M  /geekpool/fs1
geekpool/fs1@oct2013    19K      -    31K  -
geekpool/fs1/clone01     1K   875M    31K  /geekpool/fs1/clone01

Destroying ZFS clones

# zfs destroy geekpool/fs1/clone01