Using Rsync with ZFS & Btrfs for Snapshot Backups

"Stability is the goal of IT operations, but anomalies are the daily reality."
Photo by Spenser Sembrat / Unsplash

πŸ“€

Traditional Rsync backups work well, but they don't capture point-in-time snapshots, making restoration difficult in case of accidental deletions or corruption.

ZFS and Btrfs are modern file systems that support snapshots, which provide fast, space-efficient backups with Rsync for remote replication.

πŸ“Œ In this guide, you will learn:
βœ… How to create and manage snapshots with ZFS and Btrfs
βœ… How to integrate Rsync for remote snapshot backups
βœ… How to restore files from snapshots efficiently
βœ… How to automate snapshot-based backups


πŸ›‘ 1. Why Use Snapshots with Rsync?

πŸ”Ή Instant Backups – Snapshots take seconds, unlike full Rsync syncs.
πŸ”Ή Low Storage Overhead – Only changes are stored (copy-on-write).
πŸ”Ή Fast Recovery – Restore individual files or entire datasets.
πŸ”Ή Protect Against Rsync Errors – If an Rsync backup goes wrong, snapshots provide a fallback.

βœ… Solution: Use ZFS or Btrfs snapshots with Rsync for reliable, space-efficient backups.


⚑ 2. Using Rsync with ZFS Snapshots

πŸ”Ή 2.1 Setting Up ZFS (If Not Installed)

βœ… Install ZFS on Ubuntu/Debian:

sudo apt install zfsutils-linux -y

βœ… Install ZFS on CentOS/RHEL:

sudo yum install zfs -y

βœ… Verify Installation:

zfs version

πŸ”Ή 2.2 Creating a ZFS Storage Pool

βœ… Create a ZFS pool named backup-pool (Replace sdX with your actual disk):

sudo zpool create backup-pool /dev/sdX

βœ… Create a ZFS dataset (sub-volume) for backups:

sudo zfs create backup-pool/data

πŸ“Œ Now, all backups will be stored under /backup-pool/data.

βœ… Set a compression level to save space:

sudo zfs set compression=lz4 backup-pool/data

πŸ“Œ lz4 compression speeds up backups without CPU overhead.


πŸ”Ή 2.3 Taking ZFS Snapshots Before Rsync

βœ… Create a snapshot before running Rsync:

sudo zfs snapshot backup-pool/data@before-backup

πŸ“Œ This ensures that Rsync operates on a stable, unchanging dataset.

βœ… Verify the snapshot:

zfs list -t snapshot

πŸ“Œ You should see backup-pool/data@before-backup.

βœ… Send the snapshot to a remote backup server using Rsync:

rsync -avz /backup-pool/data/ user@remote:/backup/

πŸ“Œ Now, your data is safely backed up.


πŸ”Ή 2.4 Restoring from a ZFS Snapshot

βœ… If Rsync corrupted files, restore the snapshot:

sudo zfs rollback backup-pool/data@before-backup

πŸ“Œ Instantly restores the dataset to its previous state.

βœ… If only a specific file needs restoring:

cp /backup-pool/data/.zfs/snapshot/before-backup/important-file /backup-pool/data/

πŸ“Œ Restores a single file without affecting other data.


πŸ“€ 3. Using Rsync with Btrfs Snapshots

πŸ”Ή 3.1 Setting Up Btrfs (If Not Installed)

βœ… Install Btrfs on Ubuntu/Debian:

sudo apt install btrfs-progs -y

βœ… Install Btrfs on CentOS/RHEL:

sudo yum install btrfs-progs -y

βœ… Verify Installation:

btrfs --version

πŸ”Ή 3.2 Creating a Btrfs Storage Volume

βœ… Create a Btrfs filesystem on /dev/sdX:

sudo mkfs.btrfs /dev/sdX

βœ… Mount the Btrfs volume:

sudo mkdir /mnt/backup-btrfs
sudo mount /dev/sdX /mnt/backup-btrfs

πŸ“Œ Now, backups will be stored under /mnt/backup-btrfs/.

βœ… Enable compression for storage savings:

sudo btrfs property set /mnt/backup-btrfs compression lzo

πŸ“Œ lzo compression improves speed and efficiency.


πŸ”Ή 3.3 Taking Btrfs Snapshots Before Rsync

βœ… Create a snapshot before running Rsync:

sudo btrfs subvolume snapshot /mnt/backup-btrfs /mnt/backup-btrfs/snap_before_backup

πŸ“Œ This creates /mnt/backup-btrfs/snap_before_backup, a read-only snapshot.

βœ… Verify the snapshot:

sudo btrfs subvolume list /mnt/backup-btrfs

πŸ“Œ Shows all available snapshots.

βœ… Send the snapshot to a remote server using Rsync:

rsync -avz /mnt/backup-btrfs/snap_before_backup/ user@remote:/backup/

πŸ“Œ This ensures a clean, consistent backup.


πŸ”Ή 3.4 Restoring from a Btrfs Snapshot

βœ… Rollback the entire backup if Rsync fails:

sudo btrfs subvolume delete /mnt/backup-btrfs
sudo btrfs subvolume snapshot /mnt/backup-btrfs/snap_before_backup /mnt/backup-btrfs

πŸ“Œ Reverts the dataset to the pre-backup state.

βœ… Restore a single file from a snapshot:

cp /mnt/backup-btrfs/snap_before_backup/important-file /mnt/backup-btrfs/

πŸ“Œ Recovers a single file without affecting the entire backup.


πŸ”„ 4. Automating Snapshot-Based Rsync Backups

πŸ”Ή 4.1 Create a Snapshot & Rsync Backup Script

βœ… Save the following script as /usr/local/bin/snapshot-backup.sh:

#!/bin/bash

# Directories
BACKUP_DIR="/mnt/backup-btrfs"
SNAPSHOT_NAME="snap_$(date +%Y%m%d%H%M)"
REMOTE_SERVER="user@remote:/backup/"

# Create a new snapshot
sudo btrfs subvolume snapshot "$BACKUP_DIR" "$BACKUP_DIR/$SNAPSHOT_NAME"

# Rsync the snapshot to a remote server
rsync -avz "$BACKUP_DIR/$SNAPSHOT_NAME/" "$REMOTE_SERVER"

echo "Backup completed successfully!"

βœ… Make the script executable:

chmod +x /usr/local/bin/snapshot-backup.sh

βœ… Schedule it to run daily:

crontab -e

βœ… Add the following line:

0 2 * * * /usr/local/bin/snapshot-backup.sh

πŸ“Œ Runs the backup every night at 2 AM.


⚠️ 5. Troubleshooting Rsync with Snapshots

Issue Solution
Rsync is slow with snapshots Enable compression using lz4 (ZFS) or lzo (Btrfs).
Snapshot takes too much space Use zfs destroy or btrfs subvolume delete to clean up old snapshots.
Cannot restore a file Use .zfs/snapshot/ (ZFS) or btrfs subvolume list to locate snapshots.
Files modified during Rsync Always take a snapshot before Rsync to ensure a stable backup.
Disk space runs out quickly Use btrfs balance or zfs dedup to optimize space usage.

βœ… Debug snapshot issues:

zfs list -t snapshot
btrfs subvolume list /mnt/backup-btrfs

πŸ“Œ Shows available snapshots for rollback.


πŸ“Š 6. Summary

Feature ZFS Btrfs
Snapshot Support βœ… Yes βœ… Yes
Compression βœ… lz4, gzip βœ… lzo, zstd
Deduplication βœ… Yes ❌ No
Backup Efficiency βœ… Fast βœ… Fast
Best Use Case Large-scale backups Desktop & server snapshots

βœ… Using Rsync with ZFS or Btrfs snapshots ensures fast, reliable, and space-efficient backups.


πŸ’¬ Join the Discussion!

Do you prefer ZFS or Btrfs for snapshots?
How do you use Rsync with snapshot-based backups?

πŸ’¬ Share your experience in the comments below! πŸš€

πŸ‘‰ Next Up: Combining Rsync with Rsnapshot for Incremental Backups

Read more