Solaris ZFS Administration Guide
Previous Next

Saving and Restoring ZFS Data

The zfs send command creates a stream representation of a snapshot that is written to standard output. By default, a full stream is generated. You can redirect the output to a file or to a different system. The zfs receive command creates a snapshot whose contents are specified in the stream that is provided on standard input. If a full stream is received, a new file system is created as well. You can save ZFS snapshot data and restore ZFS snapshot data and file systems with these commands. See the examples in the next section.

The following solutions for saving ZFS data are provided:

  • Saving ZFS snapshots and rolling back snapshots, if necessary.

  • Saving full and incremental copies of ZFS snapshots and restoring the snapshots and file systems, if necessary.

  • Remotely replicating ZFS file systems by saving and restoring ZFS snapshots and file systems.

  • Saving ZFS data with archive utilities such as tar and cpio or third-party backup products.

Consider the following when choosing a solution for saving ZFS data:

  • File system snapshots and rolling back snapshots – Use the zfs snapshot and zfs rollback commands if you want to easily create a copy of a file system and revert back to a previous file system version, if necessary. For example, if you want to restore a file or files from a previous version of a file system, you could use this solution.

    For more information about creating and rolling back to a snapshot, see Overview of ZFS Snapshots.

  • Saving snapshots – Use the zfs send and zfs receive commands to save and restore a ZFS snapshot. You can save incremental changes between snapshots, but you cannot restore files individually. You must restore the entire file system snapshot.

  • Remote replication – Use the zfs send and zfs receive commands when you want to copy a file system from one system to another. This process is different from a traditional volume management product that might mirror devices across a WAN. No special configuration or hardware is required. The advantage of replicating a ZFS file system is that you can re-create a file system on a storage pool on another system, and specify different levels of configuration for the newly created pool, such as RAID-Z, but with identical file system data.

Saving ZFS Data With Other Backup Products

In addition to the zfs send and zfs receive commands, you can also use archive utilities, such as the tar and cpio commands, to save ZFS files. All of these utilities save and restore ZFS file attributes and ACLs. Check the appropriate options for both the tar and cpio commands.

For up-to-date information about issues with ZFS and third-party backup products, please see the Solaris Express Developer Edition release notes.

http://opensolaris.org/os/community/zfs/faq/#backupsoftware

Saving a ZFS Snapshot

The most common use of the zfs send command is to save a copy of a snapshot and receive the snapshot on another system that is used to store backup data. For example:

host1# zfs send tank/[email protected] | ssh host2 zfs recv newtank/dana

When sending a full stream, the destination file system must not exist.

You can save incremental data by using the zfs send -i option. For example:

host1# zfs send -i tank/[email protected] tank/[email protected] | ssh host2 zfs recv newtank/dana

Note that the first argument is the earlier snapshot and the second argument is the later snapshot. In this case, the newtank/dana file system must exist for the incremental receive to be successful.

The incremental snapshot1 source can be specified as the last component of the snapshot name. This shortcut means you only have to specify the name after the @ sign for snapshot1, which is assumed to be from the same file system as snapshot2. For example:

host1# zfs send -i snap1 tank/[email protected] > ssh host2 zfs recv newtank/dana

This syntax is equivalent to the above example of the incremental syntax.

The following message is displayed if you attempt to generate an incremental stream from a different file system snapshot1:

cannot send 'pool/[email protected]': not an earlier snapshot from the same fs

If you need to store many copies, you might consider compressing a ZFS snapshot stream representation with the gzip command. For example:

# zfs send pool/[email protected] | gzip > backupfile.gz

Restoring a ZFS Snapshot

Keep the following key points in mind when you restore a file system snapshot:

  • The snapshot and the file system are restored.

  • The file system and all descendent file systems are unmounted.

  • The file systems are inaccessible while they are being restored.

  • The original file system to be restored must not exist while it is being restored.

  • If a conflicting file system name exists, zfs rename can be used to rename the file system.

For example:

# zfs send tank/[email protected] > /bkups/gozer.083006
# zfs receive tank/[email protected] < /bkups/gozer.083006
# zfs rename tank/gozer tank/gozer.old
# zfs rename tank/gozer2 tank/gozer

You can use zfs recv as an alias for the zfs receive command.

If you make a change to the file system and you want to do another incremental send of a snapshot, you must first rollback the receiving file system.

For example, if you make a change to the file system as follows:

host2# rm newtank/dana/file.1

And you do an incremental send of tank/[email protected], you must first rollback the receiving file system to receive the new incremental snapshot. You can eliminate the rollback step by using the -F option. For example:

host1# zfs send -i tank/[email protected] tank/[email protected] | ssh host2 zfs recv -F newtank/dana

When you receive an incremental snapshot, the destination file system must already exist.

If you make changes to the file system and you do not rollback the receiving file system to receive the new incremental snapshot or you do not use the -F option, you will see the following message:

host1# zfs send -i tank/[email protected] tank/[email protected] | ssh host2 zfs recv newtank/dana
cannot receive: destination has been modified since most recent snapshot

The following checks are performed before the -F option is successful:

  • If the most recent snapshot doesn't match the incremental source, neither the rollback nor the receive is completed, and an error message is returned.

  • If you accidentally provide the name of different file system that doesn't match the incremental source to the zfs receive command, neither the rollback nor the receive is completed, and the following error message is returned.

    cannot send 'pool/[email protected]': not an earlier snapshot from the same fs

Sending and Receiving Complex ZFS Snapshot Streams

This section describes how to use the zfs send -I and -R options to send and receive more complex snapshot streams.

Keep the following points in mind when sending and receiving ZFS snapshot streams:

  • Use the zfs send -I option to send all incremental streams from one snapshot to a cumulative snapshot. Or, use this option to send an incremental stream from the origin snapshot to create a clone. The original snapshot must already exist on the receiving side to accept the incremental stream.

  • Use the zfs send -R option to send a replication stream of all descendent file systems. When received, all properties, snapshots, descendent file systems, and clones are preserved.

  • Or use both options to send an incremental replication stream.

    • Changes to properties and snapshot and file system renames and destroys are preserved.

    • If zfs recv -F is not specified when receiving the replication stream, dataset destroys are ignored. The zfs recv -F syntax in this case also retains its rollback if necessary meaning.

    • As with other (non zfs send -R) -i or -I cases, if -I is used, all snapshots between snapA and snapD are sent. If -i is used, only snapD (for all descendents) are sent.

  • To receive any of these new types of zfs send streams, the receiving system must be running a software version capable of sending them. The stream version is incremented.

  • However, you can access streams from older pool versions by using a newer software version, which can also access newer pool versions. For example, you can send and receive streams created with the newer options to and from a version 3 pool. But, you must be running recent software to receive a stream sent with the newer options.

Example 6-1 Examples—Sending and Receiving Complex ZFS Snapshot Streams

A group of incremental snapshots can be combined into one snapshot by using the zfs send -I option. For example:

# zfs send -I pool/[email protected] pool/[email protected] > /snaps/[email protected]

Remove snapshots B, C, and D.

# zfs destroy pool/[email protected]
# zfs destroy pool/[email protected]
# zfs destroy pool/[email protected]

Restore the combined snapshot.

# zfs receive -d -F pool/fs < /snaps/[email protected]
# zfs list
NAME                      USED  AVAIL  REFER  MOUNTPOINT
pool                      428K  16.5G    20K  /pool
pool/fs                    71K  16.5G    21K  /pool/fs
pool/[email protected]              16K      -  18.5K  -
pool/[email protected]              17K      -    20K  -
pool/[email protected]              17K      -  20.5K  -
pool/[email protected]                0      -    21K  -

You can also use the zfs send -I command to combine a snapshot and a clone snapshot to create a combined dataset. For example:

# zfs create pool/fs
# zfs snapshot pool/[email protected]
# zfs clone pool/[email protected] pool/clone
# zfs snapshot pool/[email protected]
# zfs send -I pool/[email protected] pool/[email protected] > /snaps/fsclonesnap-I
# zfs destroy pool/[email protected]
# zfs destroy pool/clone
# zfs receive -F pool/clone < /snaps/fsclonesnap-I

Use the zfs send -R command to replicate a ZFS file system and all descendent file systems, up to the named snapshot. When received, all properties, snapshots, descendent file systems, and clones are preserved.

In the following example, snapshots are created of user file systems. One replication stream is created of all user snapshots. Then, the original file systems and snapshots are destroyed and recovered.

# zfs snapshot -r [email protected]
# zfs list
NAME                USED  AVAIL  REFER  MOUNTPOINT
users               187K  33.2G    22K  /users
[email protected]            0      -    22K  -
users/user1          18K  33.2G    18K  /users/user1
users/[email protected]      0      -    18K  -
users/user2          18K  33.2G    18K  /users/user2
users/[email protected]      0      -    18K  -
users/user3          18K  33.2G    18K  /users/user3
users/[email protected]      0      -    18K  -
# zfs send -R [email protected] > /snaps/users-R
# zfs destroy -r users
# zfs receive -F -d users < /snaps/users-R
# zfs list
NAME                USED  AVAIL  REFER  MOUNTPOINT
users               196K  33.2G    22K  /users
[email protected]            0      -    22K  -
users/user1          18K  33.2G    18K  /users/user1
users/[email protected]      0      -    18K  -
users/user2          18K  33.2G    18K  /users/user2
users/[email protected]      0      -    18K  -
users/user3          18K  33.2G    18K  /users/user3
users/[email protected]      0      -    18K  -

You can use the zfs send R command to replicate the users dataset and its descendents and send the replicated stream to another pool, users2.

# zfs create users2 mirror c0t1d0 c1t1d0
# zfs receive -F -d users2 < /snaps/users-R
# zfs list
NAME                 USED  AVAIL  REFER  MOUNTPOINT
users                224K  33.2G    22K  /users
[email protected]             0      -    22K  -
users/user1           33K  33.2G    18K  /users/user1
users/[email protected]     15K      -    18K  -
users/user2           18K  33.2G    18K  /users/user2
users/[email protected]       0      -    18K  -
users/user3           18K  33.2G    18K  /users/user3
users/[email protected]       0      -    18K  -
users2               188K  16.5G    22K  /users2
[email protected]            0      -    22K  -
users2/user1          18K  16.5G    18K  /users2/user1
users2/[email protected]      0      -    18K  -
users2/user2          18K  16.5G    18K  /users2/user2
users2/[email protected]      0      -    18K  -
users2/user3          18K  16.5G    18K  /users2/user3
users2/[email protected]      0      -    18K  -
Remote Replication of ZFS Data

You can use the zfs send and zfs recv commands to remotely copy a snapshot stream representation from one system to another system. For example:

# zfs send tank/[email protected] | ssh newsys zfs recv sandbox/[email protected]

This command saves the tank/[email protected] snapshot data and restores it into the sandbox/restfs file system and also creates a [email protected] snapshot on the newsys system. In this example, the user has been configured to use ssh on the remote system.

Previous Next