btrbk is a tool for creating incremental snapshots and remote backups of Btrfs subvolumes. It is used for simple backups to an external hard drive as well as more complex scenarios, like a server pulling the backups from all computers in the network or just to make local snapshots to protect against accidental deletions.
btrbk has terms for the snapshots and backups it creates based on where they're stored and their intended purpose:
- Snapshots are locally (on the same filesystem) stored Btrfs snapshots
- Backups are snapshots copied to a folder or over SSH
- Archives are extra copies of backups.
emerge --ask app-backup/btrbk
A commented example configuration file is found in /etc/btrbk/btrbk.conf.example. Refer to the man page for more details.
btrbk will not create the target directories, it must be created manually.
To backup subvolumes etc, var/log, and var/lib to /media/backup/btrbk, with snapshots to .btrbk_snapshots:
# Enable transaction logging
# Use a lockfile so only one btrbk instance can run at a time
# Use sudo if btrbk or lsbtr is run by regular user
# Enable stream buffering
# Store snapshots under .btrbk_snapshots under the root of the volume
# Only create new snapshots when changes have been made
# Preserve hourly snapshots for up to 24 hours, and daily snapshots for up to 7 days
snapshot_preserve 24h 7d 0w 0m 0y
# The latest snapshot is always kept, regardless of the preservation policy
# Preserve daily backups for up to 14 days, weekly backups for up to 5 weeks, monthly backups for up to a month, and yearly backups for up to a year
target_preserve 0h 14d 5w 1m 1y
# Preserve the latest snapshot, regardless of the preservation policy
# Preserve one archive of each type except hourly backups
archive_preserve 0h 1d 1w 1m 1y
# Backup subvolumes at '/etc', '/var/lib' and '/var/log'
This method is not a proper backup as snapshots are stored on the same storage. Backups should be made to another system, or at least another storage device.
# Create simple snapshots of /home's subvolume 'larry' (replace as appropriate)
# These are not sent to another device or machine (no 'target').
# Make sure snapshot_preserve_min / snapshot_preserve are set in the main config section!
Backing up subvolumes
Backing up the root subvolume
To backup the root subvolume,
subvolid=5, it must be mounted:
/dev/sda1 / btrfs subvol=@root 0 0
/dev/sda1 /mnt/btr_pool btrfs subvolid=5 0 0
Backing up standard subvolumes
Enable and Restrict Root Login
Root login must be enabled on the remote computer for this to work.
To restrict the IPs/IP ranges from where root can log in, use the Match keyword. Consult the man page for sshd_config for details.
Match Address fd69::6:9
Root login should only be performed using keys, not passwords. To generate a new root SSH key, and install it on a target system:
ssh-keygen -t ed25519 -f /etc/btrbk/id_ed25519
ssh-copy-id -i /etc/btrbk/id_ed25519.pub firstname.lastname@example.org
Backing up to another host using SSH
Backups can be made over SSH:
Pull backups from another host using SSH
This is an example configuration for multiple clients to backup onto a server:
For more examples, take a look at the official documentation hyperlinked at the top right of this page.
To do a verbose dry run:
btrbk --dry-run --verbose run
Run a full backup
To create snapshots and backup (if a target was configured), run:
To only create snapshots even if a target is configured, run:
Automation with cron
Don't forget to mark the cron scripts executable:
chmod +x /etc/cron.hourly/btrbk-snapshot /etc/cron.daily/btrbk-run
exec /usr/bin/btrbk -q snapshot
exec /usr/bin/btrbk -q run