Scheduling automated ZFS scrubs

and emailing yourself the results

Brian Smith
@brismuth’s blog

--

One of the big benefits to using ZFS is its ability to repair silent data corruption. There are two ways it can do this.

  1. When it reads data from disk, it will check the data against its stored checksum. If the data doesn’t pass this check, it will pull the same data from the other disk in a mirror configuration or from the rest of the disk array. If that data matches the checksum, it will repair the other copy of the data. If that data doesn’t match either, then the data is corrupted and you’re out of luck.
  2. You can manually run a “ZFS scrub”. This checks the integrity all of your stored data against your stored checksums.

The second way is preferable because you can repair corruption quickly even on rarely accessed files. It is relatively easy to schedule this to happen regularly without any effort on your part. Note that if you don’t care about getting emailed results, you can skip straight to Step 2.

Step 1: configure your email preferences

I think the easiest way to configure email is to set your server up to use your gmail account. You can follow this guide to set that up: https://easyengine.io/tutorials/linux/ubuntu-postfix-gmail-smtp/.

After you’ve set up your server to use your gmail account, you’ll want to set your personal email address to be the recipient for cron jobs. You can do this by adding something like this to your /etc/aliases file:

root:           brismuth
brismuth: brismuth@gmail.com

You’ll want to replace “brismuth” with your username and you’ll want to use your own email address at the end. This file tells your computer that all email intended for the “root” user should be sent to the “brismuth” user, and that all email intended for the “brismuth” user should be sent to “brismuth@gmail.com”.

Step 2: schedule the ZFS scrub

The email configuration was the hard part. Now all you have to do is add some scheduled commands to your crontab. You can edit your crontab like this:

sudo crontab -e

You’ll just want to add something like the following lines at the bottom of the file:

# zpool scrub every month
0 2 1 * * /sbin/zpool scrub files
0 13 1 * * /sbin/zpool status

The above lines will schedule a zpool scrub to take place at 2 am on the first of every month. At 1 pm (13:00), it will check the pool status to make sure the scrub went well and has completed. In this case, “files” is the name of my zpool, and you’ll want to replace it with that of your own. If you aren’t sure what the name of yours is, you can see a list of pools by checking zpool status:

zpool status

That’s it! Now, on the first of every month, you’ll get an email that looks something like this:

pool: files
state: ONLINE
scan: scrub repaired 0 in 2h27m with 0 errors on Fri Apr 1 04:27:04 2016
config:

NAME STATE READ WRITE CKSUM
files ONLINE 0 0 0
mirror-0 ONLINE 0 0 0
sdb ONLINE 0 0 0
sdc ONLINE 0 0 0

errors: No known data errors

Congratulations, you now have the peace of mind that can only come from monthly emails letting you know that your files are safe!

:)

--

--

Some things I love: my family, building things, helping people, tinkering.