ZFS Autosnapshots are a way to regularly snapshot your zfs volumes. This will allow you to restore files from a point in time.

The snapshot keeps a point-in-time copy of the data on the volume. This means that you can return to it at a later time and recover a file that was altered or deleted. Each snapshot only takes the space needed to keep the the files the way they were.

Following this will set up proper Grandfather/Father/Son-style snapshots. That means that:

  • Hourly Snapshots are taken once a hour, and kept for a day
  • Daily snapshots are taken once a day, and kept for a month
  • Weekly snapshots are taken once a week, and kept for 2 months
  • Monthly snapshots are taken once a month, and kept forever

Note that if you keep snapshots forever, and you write a large file then delete it, but it gets snapshotted in between, that space will be used forever unless you delete the snapshot.

Setup

First, install “zfSnap” and create you ZFS pool and volume(s)

Next, create the file /root/auto-snap-list and put a space-separated list of the zfs filesystems that you want snapshotted, eg:

tank/test1 tank/test2

Finally, add this to /etc/crontab to set up the snapshotting:

### ZFS Snapshots ###
0 * * * *		root	/usr/sbin/zfSnap -d -a 24h -p "hourly-" -r `cat /root/auto-snap-list`
0 0 * * *		root	/usr/sbin/zfSnap -d -a 1m -p "daily-" -r `cat /root/auto-snap-list`
0 0 * * 0		root	/usr/sbin/zfSnap -d -a 2m -p "weekly-" -r `cat /root/auto-snap-list`
0 0 1 * *		root	/usr/sbin/zfSnap -a forever -p "monthly-" -r `cat /root/auto-snap-list`

Checking and restoring

To see snapshots, run:

zfs list -t snapshot

The values in the USED column tells you how much space each snapshot uses. Note that deleting a snapshot does not necessarily free up all of the USED space. Any files created before that snapshot will still be needed for the next snapshot, so they will be moved to the USED of the next snapshot (until those files were deleted).

To mount a snapshot on /mnt:

mount -t zfs tank/test1@hourly-2017-01-06_16.00.01--24h /mnt

Files will be available under /mnt until you unmount it.

Snapshots will be deleted on schedule, but if you want to delete a snapshot, just run:

zfs destroy tank/test1@hourly-2017-01-06_16.00.01--24h