Beginners Guide to ZFS Snapshots
This guide is intended to show a new user the capabilities of the ZFS snapshots feature. It describes the steps necessary to set up a ZFS filesystem and the use of snapshots including how to create them, use them for backup and restore purposes, and how to migrate them between systems. After reading this guide, the user will have a basic understanding of how snapshots can be integrated into system administration procedures.
ZFS Snapshots: Overview
A zfs snapshot is a read-only copy of a Solaris ZFS file system or volume. Snapshots can be created almost instantly and initially consume no additional disk space within the pool. They are a valuable tool both for system administrators needing to perform backups and other users who need to save the state of their file system at a particular point in time and possibly recreate it later on the same machine or some other. It is also possible to extract individual files from a snapshot. These tasks can be performed with ZFS without the need for any additional software. In this short guide, we take a look at the simple command syntax necessary to achieve these tasks.
Setting up the File System
1. First, we create a pool (which we call pool ) and display it:
# zpool create -f pool c0d0s5
# zpool list
NAME SIZE USED AVAIL CAP HEALTH ALTROOT
pool 3.11G 75K 3.11G 0% ONLINE -
2. Then we create a file system (called file system) in our pool and confirm that we have done so:
# zfs create pool/filesystem
# zfs list
NAME USED AVAIL REFER MOUNTPOINT
pool 97.5K 3.06G 18K /pool
pool/filesystem 18K 3.06G 18K /pool/filesystem
3. Now to illustrate our example we fill the file system with some data:
# cd /platform
# du -h -s .
261M .
# find . -print | cpio -pd /pool/filesystem
536032 blocks
# zfs list
NAME USED AVAIL REFER MOUNTPOINT
pool 206M 2.86G 19K /pool
pool/filesystem 206M 2.86G 206M /pool/filesystem
We are now ready to start working with snapshots.
Taking and Using a Snapshot
1. Snapshots are named with the syntax pool/fs@something, where something can be a fairly arbitary name, but ideally one that means something to the creator.
# zfs snapshot pool/filesystem@thursday
2. The snapshot is then visible using zfs list:
# zfs list
NAME USED AVAIL REFER MOUNTPOINT
pool 262M 2.81G 19K /pool
pool/filesystem 262M 2.81G 262M /pool/filesystem
pool/filesystem@thursday 0 - 262M -
3. However, the snapshot does not appear as a file system using df:
# df -h
Filesystem SIZE USED AVAIL CAP MOUNTED ON
pool 3.1G 19K 2.8G 1% /pool
pool/filesystem 3.1G 262M 2.8G 9% /pool/filesystem
The reason it is hidden from normal Solaris utilities such as ls, tar, cpio, and others is to prevent the snapshot from appearing in backups.
Restoring From a Snapshot
1. Our snapshot can now be used as a recovery mechanism. First, we “accidentally” delete all the files in our file system:
# cd /pool/filesystem
# ls
i86hvm i86pc i86xpv
# rm -rf *
# ls
# df -h /pool/filesystem
Filesystem SIZE USED AVAIL CAP MOUNTED ON
pool/filesystem 3.1G 18K 2.8G 1% /pool/filesystem
We see that the files have been removed and the size of the data reported for our file system has decreased appropriately.
2. Rolling back the snapshot to restore all our missing files is trivial:
# zfs list
NAME USED AVAIL REFER MOUNTPOINT
pool 262M 2.81G 19K /pool
pool/filesystem 262M 2.81G 18K /pool/filesystem
pool/filesystem@thursday 262M - 262M -
# zfs rollback pool/filesystem@thursday
# cd /pool/filesystem
# ls
i86hvm i86pc i86xpv
# df -h /pool/filesystem
Filesystem SIZE USED AVAIL CAP MOUNTED ON
pool/filesystem 3.1G 262M 2.8G 9% /pool/filesystem
We can see that the files have been returned and the space consumed again.
Restoring Individual Files
1. It is possible to copy individual files from a snapshot by changing into the hidden .zfs directory of the pool that has been snapped:
# cd /pool
# ls -la
total 8
drwxr-xr-x 3 root root 3 Sep 11 15:33 .
drwxr-xr-x 23 root root 512 Sep 11 15:30 ..
drwxr-xr-x 2 root root 2 Sep 11 17:23 filesystem
# cd filesystem
# ls -la
total 6
drwxr-xr-x 2 root root 2 Sep 11 17:23 .
drwxr-xr-x 3 root root 3 Sep 11 15:33 ..
# cd .zfs
# ls snapshot
# cd snapshot
# ls
thursday
# cd thursday
# ls
i86hvm i86pc i86xpv
Moving the Snapshot to Another System
1. You can move the snapshot to another system and install it there as a usable filesystem. First, create a pool to receive the snapshot on the target system:
otherhost# zpool create -f otherpool c0d0s7
otherhost# zpool list
NAME SIZE USED AVAIL CAP HEALTH ALTROOT
otherpool 6.22G 75K 6.22G 0% ONLINE -
2. Then send the snapshot over the network and receive it into the pool using a combination of the ZFS send/receive commands and a network pipe:
# zfs send pool/filesystem@thursday | ssh otherhost "/usr/sbin/zfs receive otherpool/myfs"
3. The received snapshot is then visible in the pool on the other host:
otherhost# zfs list
NAME USED AVAIL REFER MOUNTPOINT
otherpool 262M 5.87G 19K /otherpool
otherpool/myfs 262M 5.87G 262M /otherpool/myfs
otherpool/myfs@thursday 0 - 262M -
Archiving and Restoring Snapshots
Another use of snapshots is to create archives for long-term storage elsewhere. In the following sequence of commands, we send the snapshot into a file and then compress it. It can then be retrieved from the file when required. This is also shown:
# zfs send pool/filesystem@thursday > /var/tmp/thursday.snap
# gzip -9 -v /var/tmp/thursday.snap
# zfs create pool/thursday
# gzip -d -c /var/tmp/thursday.snap.gz | zfs receive -F pool/thursday