Contained WithinFind More DocumentationFeatured Support Resources | Download this book in PDF (1709 KB)
Chapter 7 Working With ZFS Snapshots and ClonesThis chapter describes how to create and manage ZFS snapshots and clones. Information about saving snapshots is also provided in this chapter. The following sections are provided in this chapter: Overview of ZFS SnapshotsA snapshot is a read-only copy of a file system or volume. Snapshots can be created almost instantly, and initially consume no additional disk space within the pool. However, as data within the active dataset changes, the snapshot consumes disk space by continuing to reference the old data and so prevents the space from being freed. ZFS snapshots include the following features:
Snapshots of volumes cannot be accessed directly, but they can be cloned, backed up, rolled back to, and so on. For information about backing up a ZFS snapshot, see Sending and Receiving ZFS Data. Creating and Destroying ZFS SnapshotsSnapshots are created by using the zfs snapshot command, which takes as its only argument the name of the snapshot to create. The snapshot name is specified as follows:
The snapshot name must satisfy the naming conventions defined in ZFS Component Naming Requirements. In the following example, a snapshot of tank/home/ahrens that is named friday is created.
You can create snapshots for all descendent file systems by using the -r option. For example:
Snapshots have no modifiable properties. Nor can dataset properties be applied to a snapshot.
Snapshots are destroyed by using the zfs destroy command. For example:
A dataset cannot be destroyed if snapshots of the dataset exist. For example:
In addition, if clones have been created from a snapshot, then they must be destroyed before the snapshot can be destroyed. For more information about the destroy subcommand, see Destroying a ZFS File System. Holding ZFS SnapshotsIf you have different automatic snapshot policies so that older snapshots are being inadvertently destroyed by zfs receive because they no longer exist on the sending side, you might consider using the snapshots hold feature. Holding a snapshot prevents it from being destroyed. In addition, this feature allows a snapshot with clones to be deleted pending the removal of the last clone by using the zfs destroy -d command. Each snapshot has an associated user-reference count, which is initialized to zero. This count increases by one whenever a hold is put on the snapshot and decreases by one whenever a hold is released. In the previous Solaris release, snapshots could only be destroyed by using the zfs destroy command if it had no clones. In this Solaris release, the snapshot must also have a zero user-reference count. You can put a hold a snapshot or set of snapshots. For example, the following syntax puts a hold tag, keep, on tank/home/cindys/snap@1.
You can use the -r option to recursively hold the snapshots of all descendent file systems. For example:
The above syntax adds a single reference, keep, to the given snapshot or snapshots. Each snapshot has its own tag namespace and tags must be unique within that space. If a hold exists on a snapshot, attempts to destroy that snapshot by using the zfs destroy command will fail. For example:
If you want to destroy a held snapshot, use the -d option. For example:
Use the zfs holds command to display a list of held snapshots. For example:
You can use the zfs release command to release a hold on a snapshot or set of snapshots. For example:
If the snapshot is released, the snapshot can be destroy with the zfs destroy command. For example:
Two new properties identify snapshot hold information:
Renaming ZFS SnapshotsYou can rename snapshots but they must be renamed within the same pool and dataset from which they were created. For example:
In addition, the following shortcut syntax provides equivalent snapshot renaming syntax as the example above.
The following snapshot rename operation is not supported because the target pool and file system name are different from the pool and file system where the snapshot was created.
You can recursively rename snapshots with the zfs rename -r command. For example:
Displaying and Accessing ZFS SnapshotsYou can enable or disable the display of snapshot listings in the zfs list output by using the listsnapshots pool property. This property is enabled by default. If you disable this property, you can use the zfs list -t snapshot command to display snapshot information. Or, enable the listsnapshots pool property. For example:
Snapshots of file systems are accessible in the .zfs/snapshot directory within the root of the containing file system. For example, if tank/home/ahrens is mounted on /home/ahrens, then the tank/home/ahrens@thursday snapshot data is accessible in the /home/ahrens/.zfs/snapshot/thursday directory.
You can list snapshots as follows:
You can list snapshots that were created for a particular file system as follows:
Snapshot Space AccountingWhen a snapshot is created, its space is initially shared between the snapshot and the file system, and possibly with previous snapshots. As the file system changes, space that was previously shared becomes unique to the snapshot, and thus is counted in the snapshot's used property. Additionally, deleting snapshots can increase the amount of space unique to (and thus used by) other snapshots. A snapshot's space referenced property is the same as the file system's was when the snapshot was created. You can identify additional information about how the values of the used property are consumed. New read-only file system properties describe space usage for clones, file systems, and volumes. For example:
For a description of these properties, see Table 6–1. Rolling Back a ZFS SnapshotThe zfs rollback command can be used to discard all changes made since a specific snapshot. The file system reverts to its state at the time the snapshot was taken. By default, the command cannot roll back to a snapshot other than the most recent snapshot. To roll back to an earlier snapshot, all intermediate snapshots must be destroyed. You can destroy earlier snapshots by specifying the -r option. If clones of any intermediate snapshots exist, the -R option must be specified to destroy the clones as well. Note – The file system that you want to roll back must be unmounted and remounted, if it is currently mounted. If the file system cannot be unmounted, the rollback fails. The -f option forces the file system to be unmounted, if necessary. In the following example, the tank/home/ahrens file system is rolled back to the tuesday snapshot:
In the above example, the wednesday and thursday snapshots are removed because you rolled back to the previous tuesday snapshot.
Overview of ZFS ClonesA clone is a writable volume or file system whose initial contents are the same as the dataset from which it was created. As with snapshots, creating a clone is nearly instantaneous, and initially consumes no additional disk space. In addition, you can snapshot a clone. Clones can only be created from a snapshot. When a snapshot is cloned, an implicit dependency is created between the clone and snapshot. Even though the clone is created somewhere else in the dataset hierarchy, the original snapshot cannot be destroyed as long as the clone exists. The origin property exposes this dependency, and the zfs destroy command lists any such dependencies, if they exist. Clones do not inherit the properties of the dataset from which it was created. Use the zfs get and zfs set commands to view and change the properties of a cloned dataset. For more information about setting ZFS dataset properties, see Setting ZFS Properties. Because a clone initially shares all its disk space with the original snapshot, its used property is initially zero. As changes are made to the clone, it uses more space. The used property of the original snapshot does not consider the disk space consumed by the clone. Creating a ZFS CloneTo create a clone, use the zfs clone command, specifying the snapshot from which to create the clone, and the name of the new file system or volume. The new file system or volume can be located anywhere in the ZFS hierarchy. The type of the new dataset (for example, file system or volume) is the same type as the snapshot from which the clone was created. You cannot create a clone of a file system in a pool that is different from where the original file system snapshot resides. In the following example, a new clone named tank/home/ahrens/bug123 with the same initial contents as the snapshot tank/ws/gate@yesterday is created.
In the following example, a cloned workspace is created from the projects/newproject@today snapshot for a temporary user as projects/teamA/tempuser. Then, properties are set on the cloned workspace.
Destroying a ZFS CloneZFS clones are destroyed by using the zfs destroy command. For example:
Clones must be destroyed before the parent snapshot can be destroyed. Replacing a ZFS File System With a ZFS CloneYou can use the zfs promote command to replace an active ZFS file system with a clone of that file system. This feature facilitates the ability to clone and replace file systems so that the origin file system becomes the clone of the specified file system. In addition, this feature makes it possible to destroy the file system from which the clone was originally created. Without clone promotion, you cannot destroy an origin file system of active clones. For more information about destroying clones, see Destroying a ZFS Clone. In the following example, the tank/test/productA file system is cloned and then the clone file system, tank/test/productAbeta, becomes the tank/test/productA file system.
In the above zfs list output, you can see that the space accounting of the original productA file system has been replaced with the productAbeta file system. Complete the clone replacement process by renaming the file systems. For example:
Optionally, you can remove the legacy file system. For example:
Sending and Receiving ZFS DataThe 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 send ZFS snapshot data and receive ZFS snapshot data and file systems with these commands. See the examples in the next section. The following backup solutions for saving ZFS data are available:
Sending a ZFS SnapshotYou can use the zfs send command to send a copy of a snapshot and receive the snapshot in another pool on the same system or in another pool on a different system that is used to store backup data. For example, to send the snapshot on a different pool on the same system, use syntax similar to the following:
If you are sending the snapshot stream to a different system, pipe the zfs send output through the ssh command. For example:
When sending a full stream, the destination file system must not exist. You can send incremental data by using the zfs send -i option. For example:
Note that the first argument is the earlier snapshot (snap1) and the second argument (snap2) is the later snapshot. In this case, the newtank/dana file system must exist for the incremental receive to be successful. The incremental snap1 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 snap1, which is assumed to be from the same file system as snap2. For example:
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:
If you need to store many copies, you might consider compressing a ZFS snapshot stream representation with the gzip command. For example:
Receiving a ZFS SnapshotKeep the following key points in mind when you receive a file system snapshot:
For example:
You can use zfs recv as an alias for the zfs receive command. If you make a change to the destination 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:
And you do an incremental send of tank/dana@snap3, 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:
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:
The following checks are performed before the -F option is successful:
Sending and Receiving Complex ZFS Snapshot StreamsThis 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:
Example 7–1 Examples—Sending and Receiving Complex ZFS Snapshot StreamsA group of incremental snapshots can be combined into one snapshot by using the zfs send -I option. For example:
Remove snapshots B, C, and D.
Receive the combined snapshot.
You can also use the zfs send -I command to combine a snapshot and a clone snapshot to create a combined dataset. For example:
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.
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.
Remote Replication of ZFS DataYou can use the zfs send and zfs recv commands to remotely copy a snapshot stream representation from one system to another system. For example:
This command sends the tank/cindy@today snapshot data and receives it into the sandbox/restfs file system and also creates a restfs@today snapshot on the newsys system. In this example, the user has been configured to use ssh on the remote system. Saving ZFS Data With Other Backup ProductsIn 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 10 release notes or the ZFS FAQ, here: http://hub.opensolaris.org/bin/view/Community+Group+zfs/faq/#backupsoftware |
|||||||||||||||||||||||||||||||||||||||||||||||||