btrbk
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.
Terminology
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.
Installation
root #
emerge --ask app-backup/btrbk
Configuration
A commented example configuration file is found in /etc/btrbk/btrbk.conf.example. Refer to the man page for more details.
To backup subvolumes etc, var/log, and var/lib to /media/backup/btrbk, with snapshots to .btrbk_snapshots:
/etc/btrbk/btrbk.conf
Basic configuration# Enable transaction logging
transaction_log /var/log/btrbk.log
# Use a lockfile so only one btrbk instance can run at a time
lockfile /run/lock/btrbk.lock
# Use sudo if btrbk or lsbtr is run by regular user
backend_local_user btrfs-progs-sudo
# Enable stream buffering
stream_buffer 256m
# Store snapshots under .btrbk_snapshots under the root of the volume
snapshot_dir .btrbk_snapshots
# Only create new snapshots when changes have been made
snapshot_create onchange
# 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
snapshot_preserve_min latest
# 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 7d 5w 4m 1y
# Preserve the latest snapshot, regardless of the preservation policy
target_preserve_min latest
# Preserve one archive of each type but hourly
archive_preserve 0h 1d 1w 1m 1y
archive_preserve_min latest
volume /
target /media/backup/btrbk
subvolume etc
subvolume var/lib
subvolume var/log
Create snapshots for accidental deletion
Note that this particular method is not strictly a backup as it's not stored on another machine.
root #
mkdir /home/.btrbk_snapshots
root #
btrbk --dry-run --verbose run
/etc/btrbk/btrbk.conf
'"`UNIQ--pre-00000002-QINU`"'
Backing up the root subvolume
To backup the root subvolume, subvolid=5
, it must be mounted:
/etc/fstab
fstab example with the root subvolume mounted at /mnt/btr_pool/dev/sda1 / btrfs subvol=@root 0 0
/dev/sda1 /mnt/btr_pool btrfs subvolid=5 0 0
Create the directory for the snapshots and test the configuration:
root #
mkdir /mnt/btr_pool/.btrbk_snapshots
root #
btrbk --dry-run --verbose run
/etc/btrbk/btrbk.conf
'"`UNIQ--pre-00000007-QINU`"'
Additional Targets
To backup more targets, add a target:
/etc/btrbk/btrbk.conf
Backup the home subvolume to /media/backup/home_backupsvolume /
target /media/backup/home_backups
subvolume home
/etc/btrbk/btrbk.conf
Backup homedirs to backup.example.org using SSHssh_identity /etc/btrbk/ssh/id_ed25519
ssh_user root
volume /
target ssh://backup.example.org:22/media/backup/home_backups
subvolume @home
Pull backups from remote computers
This is an example configuration for multiple clients to backup onto a server:
/etc/btrbk/btrbk.conf
ssh_identity /etc/btrbk/ssh/id_ed25519
ssh_user root
volume ssh://larry-desktop.example.org:22/mnt/btr_pool
target /media/backup/larry-desktop
subvolume @root
subvolume @home
volume ssh://larry-laptop.example.org:22/mnt/btr_pool
target /media/backup/larry-laptop
subvolume @root
For more examples, take a look at the official documentation hyperlinked at the top right of this page.
SSH configuration
To backup to or from a remote computer via SSH, generate a key and add it to ~root/.ssh/authorized_keys on the targets.
root #
ssh-keygen -t ed25519 -f /etc/btrbk/id_ed25519
root #
ssh-copy-id -i /etc/btrbk/id_ed25519.pub root@backup.example.org
Enable root login on the remote computer in order for this to work.
/etc/ssh/sshd_config
PermitRootLogin prohibit-password
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.
/etc/ssh/sshd_config
Match Address fd69::6:9
PermitRootLogin prohibit-password
Match All
Usage
To create snapshots and backup (if a target was configured), run:
root #
btrbk run
To only create snapshots even if a target is configured, run:
root #
btrbk snapshot
Automation with cron
Don't forget to mark the cron scripts executable:
user $
chmod +x /etc/cron.hourly/btrbk-snapshot /etc/cron.daily/btrbk-run
/etc/cron.hourly/btrbk-snapshot
Local snapshots once an hour#!/bin/sh
exec /usr/bin/btrbk -q snapshot
/etc/cron.daily/btrbk-run
Backup once a day#!/bin/sh
exec /usr/bin/btrbk -q run