FreeBSD 15.1 makes it easy to protect your data with ZFS. Snapshots are fast and small. They help you get back a deleted file or undo a bad update. But a snapshot on the same disk is not a backup. If the disk fails, you lose the data and the snapshot. In this guide, you will learn to build a complete FreeBSD zfs snapshot backup.
Configure ZFS Snapshots and Automated Backups on FreeBSD 15.1
Table of Contents
- Prerequisites
- Step 1: Create Datasets
- Step 2: Take and Test a Manual Snapshot
- Step 3: Set Up the Backup Server
- Step 4: Manually Test a FreeBSD ZFS Snapshot Backup
- Step 5: Automate Snapshots and Replication
- Step 6: Clean Up Snapshots Without Breaking Backups
- Plan Space for FreeBSD ZFS Snapshots
- Understand FreeBSD ZFS Database Snapshot Risks
- Monitor FreeBSD ZFS Backups
- Test a Full Restore
- Conclusion

Prerequisites
You need a FreeBSD 15.1 server installed on ZFS, root access, and a second server with free storage for the backup pool. If your server is not ready, follow our FreeBSD 15.1 setup guide, which covers SSH keys, PF, and updates.
Run these commands to check that your pool is healthy:
The pool should show ONLINE. A broken pool will copy its problems to the backup, so a healthy pool is the first rule of any FreeBSD zfs snapshot backup.
In this guide, the source server is called primary and the backup server is called backup. Replace 192.0.2.20 with the IP address of your backup server.
Step 1: Create Datasets
Snapshots work on each dataset. So you must keep different data in different datasets. This way, you can roll back the database and leave your web files as they are.
Run these commands to create the datasets for your web files, other files, and database, then list them:
Set the recordsize of the database dataset to match the database page size. Use 8K for PostgreSQL and 16K for MySQL or MariaDB. Set it before you add data, because it only changes new files.
Step 2: Take and Test a Manual Snapshot
A snapshot is a read-only copy of a dataset at one moment. ZFS does not overwrite old data when files change. So a new snapshot is fast and uses almost no space at first. It only grows when your data changes, because the snapshot keeps the old data.
Run these commands to create a test file, take a snapshot, list it, and check that the file is inside the snapshot:
Each dataset has a hidden .zfs/snapshot folder where you can see its snapshots. You can copy single files back from it with a normal cp command.
The -r flag takes a snapshot of the dataset and all its child datasets at the same time. So all of them show the same moment.
Step 3: Set Up the Backup Server
On the backup server, you must create a pool on the free disk. On KVM, the disk name is usually vtbd1. Run geom disk list to check yours.
Run these commands to create the backup pool, add a dataset for the primary server, and make it read-only:
Use any small KVM server with enough storage as your backup target. Linux VPS hosting from PerLod can be a good option.
Now you must create a key on the primary server and allow it on the backup server:
Then, allow root login with an SSH key on the backup server. Open the SSH config file:
Add or change this line:
Once you are done, save and close. Reload SSH to apply the change:
Run this command on the primary server to test the SSH key and see the pools on the backup server:
Step 4: Manually Test a FreeBSD ZFS Snapshot Backup
At this point, you can do one full copy and one incremental copy manually. This shows that everything works before you automate it.
Run this command on the primary server to send the full snapshot to the backup server:
-Rsends the child datasets and their snapshots too.-ustops the backup server from mounting the copy.-x mountpointignores the mount path from the primary server.-dremoves the pool name, sozroot/srvbecomesbackup/primary/srv.
Run these commands to change the file and take a new snapshot. Then, check the size of the change, send it to the backup server, and list the snapshots there:
The -nvP command is a test run. It shows the size of the data without sending anything. An incremental send works only if the older snapshot exists on both servers.
Step 5: Automate Snapshots and Replication
For a repeatable FreeBSD zfs snapshot backup, a single script should perform every step in order. This script takes a snapshot, sends it, checks that the copy arrived, and only then cleans up:
Make it executable, and add the schedule to /etc/crontab with the commands below:
The lockf command stops two runs from running at the same time. The rm -f /var/db/zfs-snap.last command deletes the old state file, so the first run starts a new chain.
Run this command to start the script. It makes an hourly snapshot, sends it to the backup server, and keeps the newest 24 hourly snapshots:
Step 6: Clean Up Snapshots Without Breaking Backups
A chain is a set of snapshots. Each transfer sends only the changes since the previous snapshot.
Deleting old snapshots can break your FreeBSD zfs snapshot backup. An incremental backup needs the same older snapshot on both servers. If you delete it from the source server, the next backup fails, and you may need to send a full copy again.
The script avoids this with three rules:
- It uses
zfshold to protect the last snapshot sent, so ZFS cannot delete it. - It protects the new snapshot and releases the old one only after checking that the new copy is on the backup server.
- It keeps the newest N snapshots for each label and deletes the older ones.
Keep snapshots longer on the backup server than on the primary server. For this setup, do not use receive -F with -R. It can delete backup snapshots that you already removed from the source.
Run this command on the backup server to create the cleanup script. It keeps the newest 72 hourly snapshots and 60 daily snapshots:
Run these commands to make the script executable and schedule it to run every day at 1:15 AM:
The script keeps the newest snapshot, so it does not delete the one needed for the next backup.
Plan Space for FreeBSD ZFS Snapshots
Snapshots use more space as your data changes. The space you have limits how much FreeBSD zfs snapshot backup history you can keep. Use this formula to estimate the pool size:
Use the amount of data changed each day, not a percentage.
Dividing by 0.8 leaves about 20% free space. ZFS can slow down when the pool is nearly full. This estimate does not include compression, which may reduce space use. Use it for both the source and backup pools.
Measure your real change rate after a week:
Check the USEDSNAP column to see how much space snapshots use. If your backup pool needs more space, choose a server with more storage. PerLod offers multi-disk dedicated servers for larger backup pools.
Understand FreeBSD ZFS Database Snapshot Risks
A snapshot saves the database as it is, even if it is still writing data. Restoring it can be like restarting after a power cut. The database may need to recover before it can run. Follow these rules to reduce the risk:
- Keep database files and logs together. If they are in child datasets, snapshot their parent with
-rto capture them at the same time. - Never snapshot database files separately from their logs.
- For PostgreSQL, run
CHECKPOINTbefore the snapshot to help speed up recovery. - For MySQL or MariaDB, InnoDB supports crash recovery, but
MyISAMtables need extra care. RunFLUSH TABLES WITH READ LOCKand keep that session open until the snapshot finishes. Then, release the lock. - Keep a separate dump with
pg_dumpormysqldump. It gives you another way to restore if the snapshot does not work.
Test the restored database before you rely on it. Run it on a different port and check that it starts.
Monitor FreeBSD ZFS Backups
You can enable FreeBSD’s built-in checks to include ZFS health in the daily report and check the pool for errors on a schedule.
Run this command to enable daily ZFS health reports and set pool scrubs to run about every 30 days:
Add a check for pool problems and backup delays:
Run these commands to make the script executable and run it as root at 5 minutes past every hour:
Cron sends any output to root by email. No email may mean there are no problems, but check that email delivery works. You can also forward root’s emails to your own address.
Test a Full Restore
You can only trust a FreeBSD ZFS snapshot backup after testing a restore. Run this test once a month. It restores from the backup host into a new dataset and compares file hashes.
Record the current state of the data:
Pull the latest copy from the backup host into a new dataset:
Check that the data matches:
If files changed after the last snapshot, the diff will show them. For a clean test, run the test right after a fresh snapshot, or stop writes for a moment.
Then, clean up with the command below:
For a real disaster, follow the same steps. Boot the FreeBSD installer in live shell mode, create a new pool, and use zfs receive to restore data from the backup host.
To recover a deleted file, copy it from /srv/www/.zfs/snapshot/<name>/.
To restore the whole dataset to an earlier snapshot, run:
Be careful: This discards changes made since that snapshot and deletes all newer snapshots of the dataset.
Conclusion
Now you have a FreeBSD ZFS backup setup with scheduled backups and checks. Test a full restore each month and make sure email alerts work. A backup is only useful if you can restore your data when you need it.
We hope you enjoy this guide. For more details, check the FreeBSD Docs for ZFS.