ZFS snapshots

Today I was messing around with ZFS at work. Normaly I don’t go further than a zfs create or zfs set property=. Today I discovered the usefulness of snapshots till the extend I will be redoing my entire fileserver tonight.

What it is

With ZFS you can create snapshot filesystems which hold the changes made to the actual ZFS filesystem. These snapshots can later be used to rollback to the state your ZFS filesystem was on the moment you created the snapshot or you can use the snapshot to recover (accidental) removed files.

You can also have more snapshots per filesystems. This means you can create snapshots per day and rollback to a certain state, based on the day. You could for example do the following:

  1. Create a snapshot on Sunday called ZFS_FSNAME@sunday
  2. Create a snapshot on Monday called ZFS_NAME@monday
  3. Create a snapshot on Tuesday called ZFS_NAME@tuesday
  4. Create a snapshot on Wednesday called ZFS_NAME@wednesday
  5. Create a snapshot on Thursday called ZFS_NAME@thursday
  6. Create a snapshot on Friday called ZFS_NAME@friday
  7. Create a snapshot on Saturday called ZFS_NAME@saturday
  8. Create a snapshot on the next coming Sunday called ZFS_NAME@sunday-new
  9. After successfully created ZFS_NAME@sunday-new, destroy ZFS_NAME@sunday.
  10. Rename ZFS_NAME@sunday-new to ZFS_NAME@sunday.
  11. Redo step 8,9,10 for the other days.

In this way you can recover one week of data, by typing the command: “zfs rollback ZFS_NAME@day.”

You can also recover selected objects. As you cant mount snapshot filesystems these are the steps to take.

  1. Clone the snapshot filesystem you wish to recover from.
  2. Mount the cloned snapshot filesystem.
  3. Copy the files and directories you wish to recover.
  4. Destroy the ZFS clone of the snapshot filesystem.

To put things in practice, here are two examples.

ZFS Snapshot and rollback

## Current list of ZFS filesystems
# zfs list
NAME                                   USED  AVAIL  REFER  MOUNTPOINT
nemesis-vm00                          13.2G  2.45G    36K  /nemesis-vm00
nemesis-vm00/NEMESIS-JDSDEV01         11.8M  2.45G    18K  none
nemesis-vm00/NEMESIS-JDSDEV01/backup  11.8M  2.45G  11.8M  /backup

## Show the contents of /backup (current)
# cd /backup
# ls
1  2  3  4

## Snapshot devices are in the format $NAME@unique-string
# zfs snapshot  nemesis-vm00/NEMESIS-JDSDEV01/backup@12Sept2008

## Snapshot device is now listed in the ZFS filesystem list
# zfs list
NAME                                              USED  AVAIL  REFER  MOUNTPOINT
nemesis-vm00                                     13.2G  2.45G    36K  /nemesis-vm00
nemesis-vm00/NEMESIS-JDSDEV01                    11.8M  2.45G    18K  none
nemesis-vm00/NEMESIS-JDSDEV01/backup             11.8M  2.45G  11.8M  /backup
nemesis-vm00/NEMESIS-JDSDEV01/backup@12Sept2008      0      -  11.8M  -

## Destroy the contents of the ZFS filesystem
# pwd
/backup
# rm -rf *
# ls

## Rollback to the state as of when the snapshot is taken
# zfs rollback nemesis-vm00/NEMESIS-JDSDEV01/backup@12Sept2008
# pwd
/backup
# ls
1  2  3  4

ZFS snapshot and recover only some objects

## Current list of ZFS filesystems
# zfs list
NAME                                   USED  AVAIL  REFER  MOUNTPOINT
nemesis-vm00                          13.2G  2.45G    36K  /nemesis-vm00
nemesis-vm00/NEMESIS-JDSDEV01         11.8M  2.45G    18K  none
nemesis-vm00/NEMESIS-JDSDEV01/backup  11.8M  2.45G  11.8M  /backup

## Show the contents of /backup (current)
# cd /backup
# ls
1  2  3  4

## Snapshot devices are in the format $NAME@unique-string
# zfs snapshot  nemesis-vm00/NEMESIS-JDSDEV01/backup@12Sept2008

## Snapshot device is now listen in the ZFS filesystem list
# zfs list
NAME                                              USED  AVAIL  REFER  MOUNTPOINT
nemesis-vm00                                     13.2G  2.45G    36K  /nemesis-vm00
nemesis-vm00/NEMESIS-JDSDEV01                    11.8M  2.45G    18K  none
nemesis-vm00/NEMESIS-JDSDEV01/backup             11.8M  2.45G  11.8M  /backup
nemesis-vm00/NEMESIS-JDSDEV01/backup@12Sept2008      0      -  11.8M  -

## Destroy the contents of the ZFS filesystem
# pwd
/backup
# rm -rf *
# ls

## Only recover directories 1 and 2
# zfs clone nemesis-vm00/NEMESIS-JDSDEV01/backup@12Sept2008 nemesis-vm00/NEMESIS-JDSDEV01/backup-RECOVERY
# zfs list
NAME                                              USED  AVAIL  REFER  MOUNTPOINT
nemesis-vm00                                     13.2G  2.46G    36K  /nemesis-vm00
nemesis-vm00/NEMESIS-JDSDEV01                    11.8M  2.46G    18K  none
nemesis-vm00/NEMESIS-JDSDEV01/backup             11.8M  2.46G    22K  /backup
nemesis-vm00/NEMESIS-JDSDEV01/backup@12Sept2008  11.8M      -  11.8M  -
nemesis-vm00/NEMESIS-JDSDEV01/backup-RECOVERY        0  2.46G  11.8M  none

# zfs set mountpoint=/mnt nemesis-vm00/NEMESIS-JDSDEV01/backup-RECOVERY

# zfs list
NAME                                              USED  AVAIL  REFER  MOUNTPOINT
nemesis-vm00                                     13.2G  2.46G    36K  /nemesis-vm00
nemesis-vm00/NEMESIS-JDSDEV01                    11.8M  2.46G    18K  none
nemesis-vm00/NEMESIS-JDSDEV01/backup             11.8M  2.46G    22K  /backup
nemesis-vm00/NEMESIS-JDSDEV01/backup@12Sept2008  11.8M      -  11.8M  -
nemesis-vm00/NEMESIS-JDSDEV01/backup-RECOVERY        0  2.46G  11.8M  /mnt

# cd /mnt
# ls
1  2  3  4

# cp -r 1 /backup/
# 

# cd /
# zfs destroy nemesis-vm00/NEMESIS-JDSDEV01/backup-RECOVERY
#


Sun, Sun Microsystems, and the Sun Logo are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.

2 Comments

okSeptember 25th, 2008 at 8:44 am

good site arpqod

LTO Ultrium TapeFebruary 17th, 2010 at 10:01 am

good post! thanks

Leave a comment

Your comment