Using rsync and snapshots for efficient disk backups with btrfs

Introduction to Efficient Disk Backups

I’ve seen many Linux users struggle with backups, whether they’re sysadmins, self-hosters, or homelab enthusiasts. One approach that’s worked well for me is combining rsync with snapshots, particularly when using the btrfs filesystem. In this article, I’ll share how to leverage rsync and btrfs snapshots for robust and space-efficient backups. Don’t bother with other filesystems if you’re serious about snapshots - btrfs is the way to go.

Understanding btrfs Snapshots

btrfs is a modern filesystem that offers advanced features like snapshotting, which allows you to create a point-in-time copy of your filesystem. The real trick is understanding how snapshots work and how to use them effectively. You can create a btrfs snapshot using the following command:

btrfs subvolume snapshot /path/to/source /path/to/destination

This command creates a snapshot of the /path/to/source subvolume and stores it in /path/to/destination. In practice, I usually start with a simple snapshot setup and then refine it as needed.

Integrating rsync with btrfs Snapshots

rsync is a powerful tool for synchronizing files and directories across different locations. By combining rsync with btrfs snapshots, you can create an efficient backup system that minimizes storage usage and transfer times. Here’s an example of how you can use rsync to synchronize a btrfs snapshot with a remote backup location:

rsync -avz --progress /path/to/snapshot/ /path/to/remote/backup/

This command synchronizes the contents of the /path/to/snapshot/ directory with the /path/to/remote/backup/ directory, using the --progress option to display transfer progress. I’ve seen this go wrong when the remote backup location is not properly configured, so make sure to test your setup thoroughly.

Creating a Backup Script

To automate the backup process, you can create a script that combines rsync and btrfs snapshots. Here’s an example script that creates a daily snapshot and synchronizes it with a remote backup location:

#!/bin/bash

# Create a daily snapshot
btrfs subvolume snapshot /path/to/source /path/to/destination/daily

# Synchronize the snapshot with the remote backup location
rsync -avz --progress /path/to/destination/daily/ /path/to/remote/backup/

You can schedule this script to run daily using cron or systemd timers. This is where people usually get burned - forgetting to schedule the backup script or not testing it properly.

Security Considerations

When creating backups, security is paramount. You need to consider encrypting the backup data and restricting access to the backup location. I usually use tools like cryptsetup to encrypt the backup location, and ssh to securely transfer the backup data. For example:

# Encrypt the backup location
cryptsetup luksFormat /dev/sdb1

# Mount the encrypted backup location
cryptsetup luksOpen /dev/sdb1 backup

# Synchronize the snapshot with the remote backup location using ssh
rsync -avz --progress /path/to/destination/daily/ user@remote:/path/to/backup/

For more information on btrfs and its features, you can visit the btrfs wiki.

Troubleshooting and Caveats

When using rsync and btrfs snapshots, you may encounter issues like snapshot creation failures or rsync errors. To troubleshoot these issues, you can check the btrfs and rsync logs, and use tools like btrfs subvolume list to verify the snapshot creation. Additionally, be aware of the potential caveats, such as snapshot storage space requirements and the impact of snapshot creation on system performance.

Best Practices and Trade-Offs

When implementing a backup system using rsync and btrfs snapshots, consider the following best practices and trade-offs:

  • Use a separate disk or partition for the backup location to avoid storage space conflicts.
  • Schedule the backup script to run during periods of low system activity to minimize performance impact.
  • Use rsync options like --delete and --exclude to fine-tune the backup process and minimize storage usage.
  • Consider using other backup tools, like borg or restic, which offer additional features and flexibility.

See also