ZFS: Setting Up Automatic Snapshots
I considered writing a script to take care of this. Luckily, before I got too far down that rabbit hole, I stumbled upon Bryan Drewery’s zfstools
on GitHub.
The zfstools
installs a few Ruby (programming language) scripts that make managing automatic ZFS snapshots really easy. While you can customize the snapshot retention, the defaults will probably serve you well.
The default snapshot schedule is to snapshot your selected ZFS datasets:
- every 15 minutes – keeping the last 4 quarter-hour snapshots
- every hour – keeping the last 24 hourly snapshots
- every day – keeping 7 daily snapshots
- every week – keeping 4 weekly snapshots
- every month – keeping 12 monthly snapshots
The setup is easy on FreeBSD. It only takes 3 steps:
- install the scripts
- turn on auto snapshotting
- update the
/etc/crontab
file
Start by installing zfstools
from the FreeBSD package system:
$ sudo pkg install zfstools
Then, determine which ZFS datasets to automatically be snapshot. Turn on auto-snapshotting for these ZFS datasets:
$ sudo zfs set com.sun:auto-snapshot=true ZPOOL/DATASET
Where ZPOOL
is the name of the zpool and where DATASET
is the name of the ZFS dataset that you want to be automatically snapshot.
Next, it is time to update your machine’s /etc/crontab
file to run the script. If you have never touched your /etc/crontab
file before, take a moment to read the manual page for this magical file:
$ man 5 crontab
Next, copy the 5 crontab lines from https://github.com/bdrewery/zfstools README.md file. Look for the lines that have tons of asterisks – the manual page listed above explains what these asterisks and the rest of the fields represent.
Also, make sure the /etc/crontab
PATH line contains the directories where ruby
and zfs-auto-snapshot
live. On my machine these were:
/usr/local/bin/
/usr/local/sbin/
You can figure out what yours are by typing:
$ which ruby
and
$ which zfs-auto-snapshot
The PATH line in my /etc/crontab
file ended up looking like this:
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
Wait for the clock to pass the quarter hour then look for the automatic snapshots:
$ zfs list -t snapshot
You will see automatic snapshots start to pile up.