30.4. Snapshots, Clones, and Replication

Snapshots are the foundation of ZFS data protection, but they are not all the same thing. A snapshot is a read-only point-in-time view within a pool; a clone is a writable dataset whose initial contents come from a snapshot; a send stream is a serialized snapshot that can be stored or transferred to another system. Understanding which object you have prevents the common mistake of treating a snapshot on the same pool as an independent backup.

30.4.1. Creating Snapshots and Restoring Individual Files

Create a file as john, then take a recursive snapshot of the parent dataset:

$ echo original > /tank/data/docs/report.txt

The echo writes the file and prints nothing. In the root shell, take the recursive snapshot:

# zfs snapshot -r tank/data@baseline

The snapshot command is silent. The -r flag snapshots tank/data and all of its descendants, including tank/data/docs. List the snapshots:

# zfs list -t snapshot -o name,used,refer
NAME                      USED  REFER
tank/data@baseline          0B    96K
tank/data/docs@baseline     0B  8.17M

USED is the space charged to the snapshot itself; a just-created snapshot usually shows 0B because it shares all blocks with the live dataset. REFER is the amount of data the snapshot references. As the live dataset changes, the snapshot's USED grows to account for blocks that would otherwise be freed.

The .zfs directory provides a read-only view of snapshots. Its visibility is controlled by the snapdir property:

# zfs get -o property,value,source snapdir tank/data/docs
PROPERTY  VALUE     SOURCE
snapdir   hidden    default

With snapdir=hidden, .zfs is not listed by ls but is accessible by path. Read the old version of the file:

$ cat /tank/data/docs/.zfs/snapshot/baseline/report.txt
original

Change the live file and compare it with the snapshot:

$ echo changed > /tank/data/docs/report.txt

The change is silent. In the root shell, compare the dataset with the snapshot:

# zfs diff tank/data/docs@baseline
M   /tank/data/docs/report.txt

zfs diff reports M for modified, + for added, - for removed, and R for renamed. It compares the snapshot with the current dataset, not with another snapshot; to compare two snapshots, pass both names. The output is a quick way to see what changed before deciding whether to roll back or restore a single file.

Restoring one file does not require rolling back the dataset. The snapshot directory is an ordinary read-only tree, so a normal file copy from it retrieves the old version. This example copies the snapshot's file to a new name, leaving the changed live file in place for the rollback exercise in the next section:

$ cp /tank/data/docs/.zfs/snapshot/baseline/report.txt /tank/data/docs/report-restored.txt

The copy is silent. Read the restored file:

$ cat /tank/data/docs/report-restored.txt
original

Restoring in place is the same copy with the live file as the destination: cp /tank/data/docs/.zfs/snapshot/baseline/report.txt /tank/data/docs/report.txt overwrites the changed version and leaves every other change in the dataset alone. For a large tree, use rsync with the snapshot path as the source, or receive a copy of the snapshot on another host and read it there; do not try to make the .zfs view writable.

Snapshots are cheap but not free. Each snapshot retains blocks that the live dataset no longer references, and a long chain of snapshots on a busy dataset can consume significant space. A snapshot schedule should match the recovery objective: frequent short-lived snapshots for accidental deletions, less frequent longer-lived snapshots for version history, and replication to another system for disaster recovery.

30.4.2. Clones, Rollback, and Their Effects on Later Changes

Rollback returns a dataset to a snapshot and destroys snapshots newer than the target. The -r and -R flags are often misunderstood with recursive snapshots. Roll back the parent:

# zfs rollback -r tank/data@baseline

The command is silent. The child dataset still shows the changed file:

$ cat /tank/data/docs/report.txt
changed

zfs rollback -r tank/data@baseline destroyed newer snapshots of tank/data, but it did not recursively roll back the child dataset's data. A recursive snapshot is a set of independent snapshots with the same name; each child must be rolled back separately. Roll back the child:

# zfs rollback -r tank/data/docs@baseline

The rollback is silent. Back in john's shell, read the file:

$ cat /tank/data/docs/report.txt
original

The cat output confirms the original content. -r destroys snapshots newer than the target in the named dataset; -R also destroys clones that depend on those snapshots. Neither flag makes a parent rollback recursive for child snapshots. If you need a consistent rollback of a hierarchy, roll back each dataset explicitly, or use a replication/clone workflow that replaces the whole hierarchy.

Create a clone from the baseline snapshot:

# zfs clone tank/data@baseline tank/clone
# zfs get -o property,value,source origin tank/clone
PROPERTY  VALUE               SOURCE
origin    tank/data@baseline  -

The clone command is silent. The clone is writable and initially shares blocks with the snapshot; its origin property records the dependency. The snapshot cannot be destroyed while the clone exists:

# zfs destroy tank/data@baseline
cannot destroy 'tank/data@baseline': snapshot has dependent clones
use '-R' to destroy the following datasets:
tank/clone

This is a safety feature: destroying the snapshot would remove the clone's origin. You can destroy the clone first, or use zfs destroy -R to destroy the clone and the snapshot together after verifying that the clone is expendable. Promote the clone to reverse the dependency:

# zfs promote tank/clone
# zfs get -o property,value,source origin tank/clone
PROPERTY  VALUE   SOURCE
origin    -       -

The promote command is silent. The promoted clone no longer has an origin. The original snapshot now belongs to the clone's history, and the original dataset becomes dependent on the promoted clone. Trying to destroy the clone's snapshot now reports the original datasets as dependents:

# zfs destroy tank/clone@baseline
cannot destroy 'tank/clone@baseline': snapshot has dependent clones
use '-R' to destroy the following datasets:
tank/data/docs@baseline
tank/data/docs
tank/data

This is the same relationship viewed from the other side. Promotion is useful when a clone becomes the new production dataset and the original should become the historical copy, but it changes which dataset owns the shared blocks. Record the new relationship before making further snapshots or destroying either side.

Snapshot, clone, and promote relationships

Figure 30.4. Conceptual snapshot and clone dependency graph. A clone depends on its origin snapshot. Promotion reverses the dependency: the clone becomes independent and the original dataset becomes the dependent one. Destroying either side without understanding the graph can destroy data that another dataset still needs.

The commands and their scope are:

Command Effect
zfs snapshot dataset@name Creates one read-only snapshot
zfs snapshot -r parent@name Creates snapshots with the same name on the parent and descendants
zfs rollback dataset@name Discards changes after the snapshot and destroys newer snapshots
zfs rollback -r dataset@name Destroys newer snapshots of the named dataset, but does not recursively restore child data
zfs rollback -R dataset@name Also destroys clones that depend on the discarded snapshots
zfs clone snapshot newdataset Creates a writable dataset with the snapshot as origin
zfs promote clone Reverses the clone/origin relationship
zfs destroy -R dataset Destroys the dataset, its descendants, and dependent clones after checking the graph

Before any destroy -R or rollback -R, run zfs list -t snapshot -o name,creation,used and zfs get origin on the datasets involved. The error messages are explicit, but they only list the direct dependents; a larger graph may need zfs list -r -t all to see all snapshots and clones.

30.4.3. Full and Incremental Send and Receive

A send stream serializes a snapshot, including its data and metadata, into a portable representation. Write a full stream to a file:

# zfs snapshot tank/data/docs@snap1
# zfs send tank/data/docs@snap1 > /home/john/debian-handbook/zfs/snap1.zfs

Both commands are silent; the shell redirection writes the stream to the file. The file is owned by root because the command ran in the root shell. Its size is:

# ls -lh /home/john/debian-handbook/zfs/snap1.zfs
-rw-r--r-- 1 root root 17M Nov 24 14:57 /home/john/debian-handbook/zfs/snap1.zfs

The stream is larger than the logical file size because it includes ZFS metadata, block pointers, and the snapshot's properties. Receive it into a new dataset in the same pool:

# zfs receive tank/restored < /home/john/debian-handbook/zfs/snap1.zfs
# zfs list -o name,used,origin tank/restored
NAME            USED  ORIGIN
tank/restored  8.36M  -

The receive is silent; the list shows the new dataset. The received dataset has no origin because it was created from a full stream, not a clone. Read the file to confirm the content:

$ cat /tank/restored/report.txt
original

Now create a second snapshot after a change and send only the difference:

$ echo second > /tank/data/docs/second.txt

The write is silent. In the root shell, snapshot and send the increment:

# zfs snapshot tank/data/docs@snap2
# zfs send -i tank/data/docs@snap1 tank/data/docs@snap2 > /home/john/debian-handbook/zfs/inc1.zfs

The snapshot and send commands are silent. The incremental stream is much smaller than the full stream:

# ls -lh /home/john/debian-handbook/zfs/inc1.zfs
-rw-r--r-- 1 root root 12K Nov 24 14:58 /home/john/debian-handbook/zfs/inc1.zfs

Receive the incremental stream into the existing dataset:

# zfs receive -F tank/restored < /home/john/debian-handbook/zfs/inc1.zfs

The receive is silent. Read the new file as john:

$ cat /tank/restored/second.txt
second

-F forces the destination to roll back to its most recent snapshot before applying the incremental stream. This is necessary when the destination has changed since the last receive. Reading a received filesystem can update access times and make it differ from its snapshot; without -F, zfs receive would refuse to overwrite those changes. On a replication target, setting atime=off avoids this kind of incidental modification, but -F is still the correct option when the target may have diverged for any reason. It is destructive to target changes after the last snapshot; verify that the target has nothing you need before using it.

The send and receive options that matter most are:

Option Description
zfs send snapshot Full stream of one snapshot
zfs send -i earlier@snap later@snap Incremental stream containing changes between two snapshots
zfs send -I earlier@snap later@snap Incremental stream that also includes all intermediate snapshots
zfs send -R dataset@snap Replication package with descendants, properties, snapshots, and clones
zfs send -w Sends encrypted data without decrypting it
zfs send -p Includes dataset properties in the stream
zfs send -t token Resumes a previously interrupted receive
zfs receive dataset Creates or updates a dataset from a stream
zfs receive -F dataset Rolls back the destination to its latest snapshot before receiving
zfs receive -s dataset Saves partial state after an interrupted receive for later resume
zfs receive -u dataset Receives without mounting the dataset
zfs receive -d parent Uses the stream's dataset name under the given parent
zfs receive -n Performs a dry run and reports what would be received

An incremental stream requires the destination to have the earlier snapshot. If the destination is missing that snapshot, the receive fails; send a full stream or use an intermediate snapshot that both sides share. -I is useful for catching up a target that is several snapshots behind, because it includes all intermediate snapshots in one stream. -R is the right choice for replicating a whole hierarchy, but it also replicates properties and clones and therefore needs more planning than a single-dataset stream.

A stream file is not a backup until it is stored independently and verified. A stream on the same pool as its source shares the pool's failure domain; a stream on a second disk in the same host is better but still not independent. Store streams on another host, tape, or object storage, and test a receive before relying on them.

30.4.4. Replication over SSH, Interrupted Transfers, and Resume

Replication over SSH sends a stream through an encrypted connection to a zfs receive process on another host. The target needs a pool with enough free space, the OpenZFS tools, and an account that can run the receive. Prefer a dedicated replication account and key, restrict it with authorized_keys options, and keep host-key verification enabled. The commands below address the target by hostname; the practice hosts resolve zfs1 and zfs2 through /etc/hosts entries for their management addresses, and any working name resolution - DNS, /etc/hosts, or the address itself - serves the same purpose.

On zfs2, obtain the server host key fingerprint through a trusted channel - a local console, a management session, or the provider's console - and compare it with what zfs1 records:

# ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub
256 SHA256:EUBzKxMNrOCGmYAWTjl2c3JW5ItZiwZZ+wzQy48f57M root@trixie (ED25519)

On zfs1, the known-host entry for zfs2 shows the same fingerprint:

# ssh-keygen -lf /root/.ssh/known_hosts
256 SHA256:EUBzKxMNrOCGmYAWTjl2c3JW5ItZiwZZ+wzQy48f57M zfs2 (ED25519)

The fingerprints match, so the key was recorded from a verified source. Debian's client defaults hash known-host entries (HashKnownHosts yes in /etc/ssh/ssh_config); on a host whose entry was stored by an ordinary first connection, the same ssh-keygen -lf call shows that hash in place of the hostname, and the fingerprint comparison is unchanged. Do not obtain a host key only with ssh-keyscan (Chapter 20 covers SSH host and user authentication): it collects whatever key the network returns and does not establish authenticity. If a known key changes, investigate before removing the old entry. The first interactive login compares the fingerprint and asks for confirmation; automation can use a dedicated known_hosts file seeded with the verified key and StrictHostKeyChecking=yes.

Test the automated path with a noninteractive login:

# ssh -o BatchMode=yes zfs2 hostname
zfs2

BatchMode=yes prevents password prompts and makes a failed key authentication exit instead of hanging. It is appropriate for a replication script; it is not a substitute for verifying the host key. The login succeeds because zfs1's root key is authorized on zfs2 and the host key is already known. For an interactive session, ssh zfs2 gives a shell on the target and exit returns to zfs1; the first connection asks you to confirm the fingerprint before the known-host entry is stored.

Create the target pool on zfs2:

# zpool create -o ashift=12 backup /dev/vdb

The command is silent. On zfs1, snapshot the source and replicate it:

# zfs snapshot tank/data/docs@repl1
# zfs send tank/data/docs@repl1 | ssh -o BatchMode=yes zfs2 zfs receive -F backup/replica

The snapshot is silent; the pipeline sends the stream through SSH and the remote receive. A successful transfer usually prints nothing. On zfs2, the received snapshot is present:

# zfs list -t snapshot -o name,used backup/replica
NAME                   USED
backup/replica@repl1     0B

On zfs2, as john, read the file from the replica:

$ cat /backup/replica/report.txt
original

On zfs1, create a change in the source dataset:

$ echo replchange > /tank/data/docs/repl.txt

The write is silent. In the root shell, snapshot and replicate the increment:

# zfs snapshot tank/data/docs@repl2
# zfs send -i tank/data/docs@repl1 tank/data/docs@repl2 | ssh -o BatchMode=yes zfs2 zfs receive -F backup/replica

The snapshot and pipeline are silent on success. On zfs2, the target now has both snapshots:

# zfs list -t snapshot -o name,used backup/replica
NAME                   USED
backup/replica@repl1    64K
backup/replica@repl2     0B

The first snapshot's USED grew because blocks changed in the second snapshot are now retained by the first. On zfs2, the new file is present:

$ cat /backup/replica/repl.txt
replchange

Full and incremental replication with resumable receive

Figure 30.5. Conceptual full and incremental replication flow. A snapshot stream crosses SSH to a receiving dataset. An interrupted receive with -s leaves a partial dataset and a resume token; the sender uses that token to continue rather than restarting the stream.

A transfer can be interrupted by a network failure, a full target, or an operator stopping the command. Receiving with -s saves the partial state and records a resume token. To observe the mechanism without waiting for a real failure, an optional exercise can throttle the stream and stop it after a short time. Create a source dataset large enough that the transfer does not finish instantly:

# zfs create -o compression=off tank/big
# chown john:john /tank/big

The create and chown commands are silent. In john's shell, write a 300 MiB random file:

$ dd if=/dev/urandom of=/tank/big/blob bs=1M count=300 status=none

dd writes the file and prints nothing because status=none suppresses its summary. In the root shell, snapshot it:

# zfs snapshot tank/big@stream1

The snapshot is silent.

Install pv on zfs1 first if it is not present. The following command is test scaffolding, not the normal production command:

# timeout 2 zfs send tank/big@stream1 | pv -q -L 5m | ssh -o BatchMode=yes zfs2 zfs receive -s backup/resume
cannot receive new filesystem stream: checksum mismatch or incomplete stream.
Partially received snapshot is saved.
A resuming stream can be generated on the sending system by running:
    zfs send -t 1-acdafea8d-c0-789c636064000310a500c4ec50360710e72765a52697303030419460caa7a515a796806416c0e5d990e4932a4b528b81f48e9c4558f597e4a79766a63030b40738f4bce13dad6c8024cf0996cf4bcc4d05d28979d9fa4999e90ec52545a989b9860c480000476f1936

On zfs2, the target's receive_resume_token property holds the token needed to resume:

# zfs get -H -o value receive_resume_token backup/resume
1-acdafea8d-c0-789c636064000310a500c4ec50360710e72765a52697303030419460caa7a515a796806416c0e5d990e4932a4b528b81f48e9c4558f597e4a79766a63030b40738f4bce13dad6c8024cf0996cf4bcc4d05d28979d9fa4999e90ec52545a989b9860c480000476f1936

The token is long and specific to the partial stream. Back on zfs1, read the token from the target over SSH into a shell variable, then pass it to zfs send -t:

# token=$(ssh -o BatchMode=yes zfs2 zfs get -H -o value receive_resume_token backup/resume)
# zfs send -t "$token" | ssh -o BatchMode=yes zfs2 zfs receive -s backup/resume

The assignment and the pipeline normally print nothing. The token contains only URL-safe characters, so the quoted variable is safe to pass as one argument. Verify the completed dataset and its payload on zfs2:

# zfs list -o name,used backup/resume
NAME            USED
backup/resume   300M

On zfs2, the resumed file has the full expected size:

$ wc -c /backup/resume/blob
314572800 /backup/resume/blob

The source must keep the snapshot that the token references, and the target must keep the partial dataset, until the resume finishes. If either side destroys or rolls back the relevant snapshot, the token becomes invalid and the transfer must start again. Resume is not a substitute for a stable network; it saves the work already completed, not the need to plan capacity and bandwidth.

For production replication, consider these options:

Practice Reason
Dedicate an account and key to replication Limits what a compromised source key can do on the target
Keep StrictHostKeyChecking=yes and a verified known_hosts Prevents sending data to an impostor host
Use zfs send -R for a consistent hierarchy Includes descendants, properties, and clones
Use -w for encrypted datasets Keeps data encrypted in transit and at rest on the target
Set atime=off on the target Avoids unnecessary writes that make the target diverge
Monitor exit status and target free space A failed or truncated stream must not be trusted
Test a restore from the target A replica is only useful if it can be received and read

Bandwidth limiting is useful on shared links. pv -L can throttle a pipeline, and mbuffer can smooth bursts, but neither changes the need for a verified stream and a resume plan. The throttling in the exercise only makes the interruption deterministic; a normal replication command sends the stream directly.

30.4.5. Retention, Snapshot Holds, and Restoring Without the Original Pool

Snapshots are often deleted by retention scripts. A hold prevents deletion until every hold tag is released. Create a snapshot and hold it:

# zfs snapshot tank/data/docs@holdme
# zfs hold keep tank/data/docs@holdme
# zfs holds tank/data/docs@holdme
NAME                   TAG   TIMESTAMP
tank/data/docs@holdme  keep  Tue Nov 24 15:00 2026

The snapshot and hold commands are silent; zfs holds shows the tag and timestamp. Attempt to destroy the snapshot:

# zfs destroy tank/data/docs@holdme
cannot destroy snapshot tank/data/docs@holdme: it's being held. Run 'zfs holds -r tank/data/docs@holdme' to see holders.

Release the hold and destroy the snapshot:

# zfs release keep tank/data/docs@holdme
# zfs destroy tank/data/docs@holdme

Both commands are silent. A hold is a named tag, not a lock on the dataset; it does not prevent writes, rollback, or mounting. Multiple holds can exist on one snapshot, and all must be released before destruction. Use holds for snapshots that a backup application or an auditor requires, and document who owns each tag.

Retention is a policy, not a single command. A useful policy distinguishes:

Class Typical lifetime Purpose
Frequent operational snapshots Hours to days Recover accidental edits or deletions quickly
Daily or weekly snapshots Weeks to months Restore older versions and investigate changes
Monthly or compliance snapshots Months to years Meet retention requirements or legal holds
Replicated snapshots on another host According to backup policy Survive loss of the source pool or host
Offline or off-site streams According to backup policy Survive site loss, ransomware, or a compromised host

A hold can enforce the compliance row, but it does not copy data anywhere. Pair holds with replication or stream archives so that the retained snapshot exists on more than one system.

To demonstrate restoring without the original pool, copy a stream to zfs2 and receive it there:

# scp -o BatchMode=yes /home/john/debian-handbook/zfs/snap1.zfs zfs2:/root/snap1.zfs

The scp command is silent on success. On zfs2, receive the stream into the backup pool:

# zfs receive backup/fromstream < /root/snap1.zfs
# zfs list -o name,used,origin backup/fromstream
NAME                USED  ORIGIN
backup/fromstream  8.36M  -

The receive is silent; the list shows the restored dataset. Read the file:

$ cat /backup/fromstream/report.txt
original

The source pool was not needed for this receive; the stream file contains the snapshot data and metadata. This is the essential property of a replica: it can be restored on a host that has the right OpenZFS version and enough space, even if the original pool is gone.

The same principle applies to a replicated dataset. On zfs1, create a small dataset, replicate it, then destroy the source:

# zfs create tank/protect
# chown john:john /tank/protect

The create and chown commands are silent. In john's shell, write a file:

$ echo keep-me > /tank/protect/value.txt

The write is silent. In the root shell, snapshot and replicate the dataset:

# zfs snapshot tank/protect@keep
# zfs send tank/protect@keep | ssh -o BatchMode=yes zfs2 zfs receive -F backup/protect

The snapshot and pipeline are silent. Destroying the source dataset removes its only local copy; the replica on zfs2 is what remains. Verify that it is gone on zfs1:

# zfs destroy -r tank/protect
# zfs list tank/protect
cannot open 'tank/protect': dataset does not exist

The destroy is silent; the list fails because the dataset is gone. On zfs2, the replica and its snapshot still exist:

# zfs list -t snapshot -o name,used backup/protect
NAME                  USED
backup/protect@keep     0B
$ cat /backup/protect/value.txt
keep-me

The data survived because it was on an independent pool on another host. This is the distinction that matters in an incident:

Protection Protects against Does not protect against
Mirror or RAIDZ A disk failure within the redundancy limit Pool loss, deletion, corruption propagated to all copies, site loss
Snapshot Accidental deletion or change, within the same pool Pool loss, disk failure beyond redundancy, site loss
Replica on another host Pool loss, host loss, many operational errors Site loss if both hosts are co-located, compromised credentials on both
Offline or off-site stream Site loss, ransomware, long-term archive Loss before the stream was created; untested restores

A replica is only a backup if it is independent and restorable. A second pool on the same host shares the host's power, kernel, and administrator; a second host in the same rack shares the site. Test a restore on a schedule, keep the encryption keys with the backup, and record the OpenZFS version needed to read the stream. Chapter 15 covers backup design, verification, and retention in more detail.