ZFS Snapshots and Backups Part 3: Backups You Don't Have to Think About are Backups that Get Done

Last edited on 2025-07-09 Tagged under  #zfs   #bash   #shell   #programming   #freebsd   #bsd 

Part 1: Getting Started with Snapshots
Part 2: Use Snapshots to Make Backups to a Home Server
Part 3: Backups You Don't Have to Think About are Backups that Get Done

Backups are an easy task to put off but WOW do you feel the pain when a file is mistakenly deleted or a storage device fails!



Backup Bash Script

In Part 2 I used ZFS to perform a snapshot of my home directory and back up the contents from my laptop to the home server (both machines running FreeBSD). Subsequent incremental backups track any changes to that home backup.

This is a task ripe for automation!

I use a combination of ZFS tools + bash scripting + cron(8) to perform an automated daily backup of the contents of my home directory to the home server.

NOTE
Requirements for running the necessary ZFS commands as my non-root user have been pre-configured using the zfs allow command.

I wrote a bash script - zfs_backup.sh - that snapshots a SOURCE_DATASET and replicates the data on a BACKUP_DATASET. This backup can reside either on local storage or on a remote host.

The script performs these actions:

  • Source an SSH key if receiving BACKUP_DATASET resides on a REMOTE_HOST (see: Passwordless Logins to Servers)
  • Find the last snapshot received by BACKUP_DATASET
  • Find the matching snapshot sent from SOURCE_DATASET
  • Create snapshot in SOURCE_DATASET
  • Replicate the differences between snapshots on BACKUP_DATASET
  • Clean up old snapshots on both SOURCE_ and BACKUP_DATASET
  • Log messages to a LOG_FILE

Automate Backups Using Cron

My zfs_backup.sh script includes an -r <ip_address> option to signal the backup is intended for a remote host and its IP address.

I save a one-line command to another script that calls zfs_backup.sh with the particular details of the backup to be executed:

/path/to/zfs_backup.sh -r [ip address] [source dataset] [backup dataset]

EXAMPLE
A command string that performs a backup of my user's home directory to my remote home server. Details are saved to backup_home.sh:

#!/bin/sh

${HOME}/bin/zfs_backup.sh -r 192.168.1.23 zroot/home/dwa zroot/backup/oumuamua/home/dwa

I use the cron utility to automate performing daily backups.

Create a new job in my user's crontab(5):

$ crontab -e

To run the backup script backup_home.sh every day at 12:15, the job would be structured as so:

#minute	hour	mday	month	wday	command
15 12 * * * /home/dwa/bin/backup_home.sh

Save changes and exit.

Done! I've been running this backup process for a few days and its working as expected.

Of course, there are several professional ZFS snapshot and replication utilities available to choose from: sanoid and zrepl in particular have been recommended to me. I might use them in the future myself. In the meantime, its been a pleasure learning a bit about ZFS by putting it to work as the heart of my personal backup solution.

Resources

You can like, share, or comment on this post on the Fediverse 💬

Thanks for reading! Read other posts?

« Previous: ZFS Snapshots and Backups Part 2: Use Snapshots to Make Backups to a Home Server