Continuing from part 5, we’ll learn about ZFS snapshots and ZFS clones in this post:
In last post, we created a ZFS data set ‘myPool/myData’. Let’s see how we can backup and restore data using ZFS snapshots and ZFS clones.
1. ZFS snapshot is a point-in-time checkpoint of a dataset. ZFS snapshot is read-only and cannot be modified. You can create a ZFS snapshot of a ZFS data set using the following command –
sudo zfs snapshot <ZFS data set name>@<ZFS snapshot name>
In our case, we’ll create a snapshot called monday by issuing the following command –
sudo zfs snapshot myPool/myData@monday
In similar way we have created more snapshots for tuesday, wednesday, thursday and friday. Note that it will be a better idea to name the snapshot according to the date and time the snapshot was created.
2. To list all the ZFS data sets and ZFS snapshots and ZFS clones, issue the following command at the terminal –
sudo zfs list -t all
You see that we have 5 ZFS snapshots listed for ZFS data set ‘myPool/myData’.
3. To do a simple file / folder restore, we will use ZFS clone to mount a ZFS snapshot. Once mounted, once can read/write from/to a ZFS clone but all the changes to the ZFS clone will be lost once the ZFS clone is unmounted/destroyed. Let’s say we want a file from monday’s ZFS snapshot. Hence, to mount monday’s snapshot we’ll clone it using the following command –
sudo zfs clone <ZFS snapshot path/name> <ZFS clone path/name>
Hence, in our case, the command will be –
sudo zfs clone myPool/myData@monday myPool/mondayClone
This will create a clone of the ‘myPool/myData@monday’ snapshot and mount it to ‘/myPool/mondayClone’ directory. You can now browse the directory using the terminal or file manager and restore any file/folder that you need to.
4. When you are done restoring the file/folder, you can destroy the ZFS clone using the following command –
sudo zfs destroy <ZFS clone path/name>
In our case –
sudo zfs destroy myPool/mondayClone
You can verify the clone was destroyed by running ‘sudo zfs list -t all’ command again.
5. If you need to rollback the whole ZFS data set back to a point-in-time using a snapshot, you can use the following command –
sudo zfs rollback -r <ZFS snapshot path/name>
In our case, to rollback the ZFS data set back to wednesday’s snapshot, we’ll issue the following command –
sudo zfs rollback -r myPool/myData@wednesday
This will rollback the ZFS data set back to the state when the wednesday snapshot was taken, however, following things should be kept in mind –
a) All the newer snapshots after wednesday would be destroyed as the parent ZFS data set ‘myPool/myData’ will change after the rollback.
b) Any changes made after wednesday snapshot in the ZFS data set ‘myPool/myData’ will lost.
To confirm the above two points, run ‘sudo zfs list -t all’ at the terminal and note that the ZFS snapshots for thursday and friday are gone. Also, note the change in amount of data referred by ZFS dataset ‘myPool/myData’.
[To be continued in Part 7…]