btrbk

From Gentoo Wiki
Jump to:navigation Jump to:search

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

Snapshots are locally (on the same filesystem) stored Btrfs snapshots, backups are snapshots copied to a folder or over SSH and 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.

FILE /etc/btrbk/btrbk.confExample configuration
transaction_log            /var/log/btrbk.log
lockfile                   /run/lock/btrbk.lock
timestamp_format           long
snapshot_dir               btrbk_snapshots
snapshot_create            onchange

snapshot_preserve          24h 7d 0w 0m 0y
snapshot_preserve_min      latest
target_preserve            0h 14d 6w 4m 1y
target_preserve_min        latest
archive_preserve           0h 1d 1w 1m 1y
archive_preserve_min       latest

volume /mnt/btr_pool
  subvolume @root
  subvolume @home

With the example configuration above, btrbk will only create snapshots (in /mnt/btr_pool/btrbk_snapshots) if the data has changed. The long timestamps include the time in addition to the date.

The *_preserve* keywords mean that:

  • Snapshots are kept for 7 days, up to 24 hourly and 7 daily snapshots
  • There will be no hourly backups stored, daily backups will be deleted after 14 days, weekly backups after 6 weeks, monthly backups after 4 months and yearly backups after 1 year
  • When archiving a backup, only 1 daily, weekly, monthly and yearly snapshot will be kept
  • The newest snapshot/backup/archive will always be kept, because *_preserve_min is set to latest


The example assumes that the Btrfs subvolume containing @root and @home is mounted at /mnt/btr_pool. This is usually the Btrfs root subvolume, which always has subvolid=5.

Note
Mounting subvolid=5 is mandatory to backup the root filesystem /.
FILE /etc/fstabfstab 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

Backups

To be able to create backups, add a target:

FILE /etc/btrbk/btrbk.confBackups to a local disk
volume /mnt/btr_pool
  target /media/backup/larry-desktop
    subvolume @root
    subvolume @home
FILE /etc/btrbk/btrbk.confBackups to a remote computer
ssh_identity               /etc/btrbk/ssh/id_ed25519
ssh_user                   root

volume /mnt/btr_pool
  target ssh://backup.example.org:22/media/backup/larry-desktop
    subvolume @root
    subvolume @home

Pull backups from remote computers

This is an example configuration for multiple clients to backup onto a server:

FILE /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
Note
Enable root login on the remote computer in order for this to work.
FILE /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.

FILE /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

FILE /etc/cron.hourly/btrbkLocal snapshots once an hour
#!/bin/sh
exec /usr/bin/btrbk -q snapshot


FILE /etc/cron.daily/btrbkBackup once a day
#!/bin/sh
exec /usr/bin/btrbk -q run