ZFS is possibly one of the best filesystems out there. Yet still, Linux is only recently getting good support for it. The real ZFS is available on UNIX, like Solaris, and for this article, FreeBSD. Because Solaris has gone under recently, FreeBSD is undoubtedly the best OS for ZFS. However, if you’re new to FreeBSD and perhaps come from the Linux community, you may not understand how to fully handle this beast. Primarily how to do one of the best properties of ZFS: snapshotting. In this article, I will teach you how to back up and restore ZFS datasets on FreeBSD.
Also good to know: How to Speed up FreeBSD Ports with Synth
How to Snapshot ZFS Datasets on FreeBSD
Before you can restore a snapshot, you’ll want to back it up before anything bad happens. Doing that is quite simple:
# zfs snapshot datastore/yourfiles@SNAPSHOT-NAME
That will create a ZFS snapshot, which you can later restore on FreeBSD. You can list your snapshots with:
Back up ZFS on a regular basis
It’s essential to back up regularly-used folders on a regular basis, as that removes having to do the above process regularly before editing anything.
To back things up on a regular basis, you will need to use cron:
# crontab -e
Then, write the following (we use daily, I think most people should use daily, the use case for otherwise is only for giant instances with little storage):
Then, create the file:
# vim /usr/local/bin/take-snapshots
And finally, write the script to take the snapshots:
Restore ZFS Snapshot on FreeBSD
Now that you’ve set up the above, the question arises of what to do when you mess up. Luckily, you can now restore snapshots using zfs clone because you’ve been backing up.
# zfs clone datastore/yourfiles@snapshot datastore/yourfiles-RESTORE
That will take the snapshot and turn it into a writable directory. You can access the directory and take out what you need from there.
We hope this article will save everyone a little bit of data. Just don’t forget: Your ZFS snapshots will not help you in the event of a total disk failure, like a fire that burns down your servers. Please have backups:
- On your local machine (like your ZFS snapshots)
- On another machine on the network (like a NAS or other computer)
- In the cloud (so that if there’s a fire or something else horrible, the data won’t get lost either)
The FreeBSD handbook is also a good resource on the subject of all things ZFS/FreeBSD: https://docs.freebsd.org/en/books/handbook/