Jump to content
Search

The new Ubuntu Wiki is live. Legacy content may be unavailable and page links may have changed. Read the announcement on Discourse.

Get started with ZFS snapshots and clones

From Ubuntu Wiki

In this tutorial you’ll learn about ZFS snapshots and ZFS clones, what they are and how to use them.[1]

What you’ll need

Warning
ZFS support is experimental in the Ubuntu installer.

ZFS snapshots

A snapshot is a read-only copy of a file system taken at a moment in time. Snapshots only record differences between the snapshot and the current file system. This means that, until you start making changes to the active file system, snapshots don’t take up any additional storage.

You can clone snapshots, back them up and roll back to them. They are persistent and consume disk space from the same storage pool in which they were created.

You can access the read-only content of a snapshot in the hidden .zfs/snapshot/ directory at its mount point, and you can copy the archived files from there.

Snapshot naming

Snapshot names consist of the name of the file system, followed by an @ and the name of the snapshot.

For example, the snapshot snapname of the file system filesystem would be filesystem@snapname.

List snapshots

You can list snapshots using the zfs list command and specifying the type as snapshot:

zfs list -t snapshot

Create a snapshot

Snapshots are created using the zfs snapshot command, or zfs snap for short. Pass it the name of the snapshot that you want to create.

For example, to create a snapshot of tank/example called snap1, use:

zfs snapshot tank/example@snap1
Note
In these examples, replace tank with the name of your ZFS pool. Managing snapshots and clones on your root file system (rpool) requires changes to your boot loader, which is out of scope for this tutorial.

Now, list all your snapshots:

zfs list -t snapshot

You should see the newly created snapshot in the list:

NAME                   USED  AVAIL  REFER  MOUNTPOINT
...
tank/example@snap1        -      -      -  -
...

Delete a snapshot

Snapshots are deleted using the zfs destroy command.

Note
Limitations:
  • You can’t delete a snapshot that has been cloned. You must delete all clones of a snapshot before deleting it. We’ll go over cloning in the next sections.
  • You can’t delete a dataset that has had snapshots made from it. You must delete all snapshots originating from a dataset before deleting it.

For example, to delete the tank/example@snap1 snapshot from earlier, specify its name in the zfs destroy command:

zfs destroy tank/example@snap1

List your snapshots again:

zfs list -t snapshot

The deleted snapshot no longer appears.

Rename a snapshot

Snapshots can be renamed using the zfs rename command. Pass in the current name, along with the new name.

For example, rename the snap1 snapshot to snap2:

zfs rename tank/example@snap1 tank/example@snap2
Note
The file system must be the same. This means that you could not run the following command:
zfs rename tank/example@snap1 tank/something@snap2

Since the file system must remain the same, for the example above, you could simply run the following command to rename the snapshot to snap2:

zfs rename tank/example@snap1 snap2

Roll back a snapshot

You can roll back the active file system to a snapshot using the zfs rollback command. This deletes all changes made since the snapshot was created, reverting the active file system to that point in time.

To roll back to the tank/example@snap1 snapshot from the examples above, run the following command:

zfs rollback -r tank/example@snap1
Note
All snapshots newer than the snapshot that you want to roll back to must be deleted. The -r flag of the zfs rollback command removes the newer snapshots automatically.

ZFS clones

A clone is similar to a snapshot, although you can write to it. Much like snapshots, they only start taking up space once changes are made.

Clones can only be created from snapshots.

Note
This section uses the snapshot made in the last section. We will use tank/example@snap1 like in the last section.

Create a clone

You can create a clone using the zfs clone command. The clone is based on an existing snapshot.

Pass in the snapshot name, followed by the clone name. For example, create a clone called tank/clone based on the tank/example@snap1 snapshot:

zfs clone tank/example@snap1 tank/clone

Delete a clone

You can delete a clone using the zfs destroy command, just like snapshots. Pass the clone name:

zfs destroy tank/clone
Note
All clones of a snapshot must be deleted before you can delete the snapshot. You wouldn’t be able to delete the tank/example@snap1 snapshot before deleting the tank/clone clone.

Replace a file system with a clone

You can replace an active file system with a clone of the same file system using the zfs promote command. This causes the original file system to become the clone of the file system. This also allows you to delete the file system from which a clone was created.

For example, say you created a snapshot of tank/example called tank/example@snap1, then you created a clone from that snapshot called tank/clone. The following command swaps tank/example (the original file system) with tank/clone (the clone of the file system):

zfs promote tank/clone

This swaps the two, causing tank/example to become a clone of tank/clone.

At this point you could rename the new file system or delete the old one to completely replace the original with the clone.

Automation tools

You can manage and automate ZFS snapshots and clones using several tools available for Ubuntu:

Further reading

Notes