| | | | | |

How to Back up ZFS and Restore Data on FreeBSD

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

Beastie, The Mascot of BSD Operating Systems
The is Beastie, the mascot of BSD Operating Systems. Isn’t he so adorable?

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:

How to list zfs snapshots

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):

@daily /usr/local/bin/take-snapshots

Then, create the file:

# vim /usr/local/bin/take-snapshots

And finally, write the script to take the snapshots:

Script to automate zfs snapshots
Of course, change the shell at the top and the name of 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.

See also  How to Use the Execute Command in Minecraft

Conclusion

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:

  1. On your local machine (like your ZFS snapshots)
  2. On another machine on the network (like a NAS or other computer)
  3. 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/

Similar Posts

2 Comments

  1. “The real ZFS is available on UNIX, like Solaris, and for this article, FreeBSD.”

    BSD (Berkeley Software Distribution) and UNIX are closely related, but they are not the same thing. UNIX is a family of operating systems that share common design principles and adhere to a set of standards defined by The Open Group, an industry consortium.

    BSD, on the other hand, is a specific branch or variant of the UNIX operating system that originated from the University of California, Berkeley. BSD was developed as an extension and enhancement of the original UNIX system developed at Bell Labs. It introduced significant improvements and innovations, including the TCP/IP networking stack, the virtual memory system, and the vi text editor, among others.

    The correct term for what you are describing is “UNIX-like operating systems”

    1. It depends on how you define UNIX. If you define UNIX as allowed to use the trademark from The Open Group, which owns the name UNIX, macOS is considered UNIX. macOS is based on Apple Darwin, which is based on NeXTSTEP, which is based on FreeBSD. How can something be UNIX while being based off an operating system which is considered “not unix” and having no UNIX code.

      The Open Group is merely a holding company that purchased the trademark UNIX from AT&T and charges companies to be able to use the name UNIX.

      If I develop a crappy Minecraft mod in Java and shovel over enough money to The Open Group, that could be considered UNIX too.

      The difference between FreeBSD and say, Linux, is that FreeBSD while it does not meet the legal definition for UNIX, and no longer contains any UNIX code, that code is modified in minor ways to fulfill legal requirements for open source, but at runtime is hardly different at all than any other form of UNIX, except for things that have been added or changed by the developers since the project has been in existence.

      Linux, on the other hand, is an attempted rewrite of not UNIX, but instead MINIX, which is another attempted rewrite of UNIX with the intentions of being minimal. It is twice rewritten and removed from true UNIX code in the form of “replica software.” Neither Linus nor the author of MINIX had any knowledge of the code of the software they were rewriting.

      While a lawyer could reasonably make the argument that FreeBSD is not “true UNIX,” in 2023, where all UNIX operating systems have seen significant deviation from the last release of UNIX made in the 90’s, and where the name UNIX is decided by a holding company who owns trademarks and “sets standards,” I do not believe from a programming point of view it makes any difference what “true UNIX” is or not, especially as even Solaris in 2010 modified their entire source code to become open source (Solaris 10 was open, they reclosed the source after Solaris 11). While there are other forms of UNIX, the last real UNIX that anyone ever used rewrote their source code in 2010, and the UNIX source code is a thing of the past.

Leave a Reply

Your email address will not be published. Required fields are marked *