Using Rsync with ZFS & Btrfs for Snapshot Backups
π
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