Tool for creating snapshots and remote backups of btrfs subvolumes
Btrbk is a backup tool for btrfs subvolumes, taking advantage of btrfs specific capabilities to create atomic snapshots and transfer them incrementally to your backup locations.
The source and target locations are specified in a config file, which allows to easily configure simple scenarios like "laptop with locally attached backup disks", as well as more complex ones, e.g. "server receiving backups from several hosts via ssh, with different retention policy".
Btrbk is designed to run as a cron job for triggering periodic snapshots and backups, as well as from the command line (e.g. for instantly creating additional snapshots).
Btrbk is a single perl script, and does not require any special installation procedures or libraries. Download the latest btrbk source tarball, or try latest master:
wget https://raw.githubusercontent.com/digint/btrbk/master/btrbk chmod +x btrbk sudo ./btrbk ls /
For more information, read the installation documentation.
Please consult the btrbk(1) man-page provided with this package for a full description of the command line options.
btrbk, you will need to create a configuration file. You might want to take a look at
btrbk.conf.exampleprovided with this package. For a detailed description, please consult the btrbk.conf(5) man-page.
After a configuration change, it is highly recommended to check it by running btrbk with the
btrbk -c /path/to/myconfig -v -n run
This will read all btrfs information on the source/target filesystems and show what actions would be performed (without writing anything to the disks).
The examples below assume that the btrfs subvolume containing
rootfsis mounted at
/mnt/btr_pool. This is usually the btrfs root subvolume, which always has
/dev/sda1 /mnt/btr_pool btrfs subvolid=5,noatime 0 0
Note that many linux distributions use subvolume names
@for rootfs (mounted at
/home. If this is the case on your file system, replace the subvolume options in the examples accordingly.
The simpliest use case is to only create snapshots of your data. This will obviously not protect it against hardware failure, but can be useful for:
Let's assume you need regular snapshots of your home directory, which is located in the subvolume
homeof the volume
/mnt/btr_pool. The snapshots are to be stored in
btrbk_snapshots(on the same volume).
timestamp_format long snapshot_preserve_min 18h snapshot_preserve 48h
volume /mnt/btr_pool snapshot_dir btrbk_snapshots subvolume home
Notice that the
targetoption is not provided, and btrbk will only manage snapshots located on the same volume in
snapshot_dir. Btrbk does not create subdirs by default, the snapshot directory must first be created manually:
sudo mkdir /mnt/btr_pool/btrbk_snapshots
Start a dry run:
sudo btrbk run -n
Create the first snapshot:
sudo btrbk run
If it works as expected, configure a cron job to run btrbk hourly:
#!/bin/sh exec /usr/bin/btrbk -q run
Snapshots will now be created every hour, kept for 48h (
snapshot_preserve), then automatically removed.
With this setup, the snapshots will be kept at least for 18 hours (
snapshot_preserve_min). This can be useful to create manual snapshots by calling
sudo btrbk runon the command line and keep them around for a while, in addition to the regular snapshots.
In this example, we assume you have a laptop with:
/mnt/btr_pool, containing a subvolume
rootfsfor the root filesystem (i.e. mounted on
/) and a subvolume
homefor the user data,
/mnt/btr_pool/btrbk_snapshotswhich will hold the btrbk snapshots,
/mnt/btr_backup, containing a subvolume or directory
mylaptopfor the incremental backups.
snapshot_preserve_min 2d snapshot_preserve 14d
target_preserve_min no target_preserve 20d 10w *m
volume /mnt/btr_pool target /mnt/btr_backup/mylaptop subvolume rootfs subvolume home [...]
#!/bin/sh exec /usr/bin/btrbk -q -c /etc/btrbk/btrbk-mylaptop.conf run
If you want the snapshots to be created only if the backup disk is attached, simply add the following line to the config:
For a quick additional snapshot of your home, run:
# btrbk snapshot home
Let's say you have a fileserver at "myserver.mydomain.com" where you want to create backups of your laptop disk, the config would look like this:
volume /mnt/btr_pool subvolume rootfs target /mnt/btr_backup/mylaptop target ssh://myserver.mydomain.com/mnt/btr_backup/mylaptop
In addition to the backups on your local usb-disk mounted at
/mnt/btr_backup/mylaptop, incremental backups would also be pushed to
If you're a sysadmin and want to trigger backups directly from your fileserver, the config would be something like:
volume ssh://alpha.mydomain.com/mnt/btr_pool target /mnt/btr_backup/alpha subvolume rootfs subvolume home
volume ssh://beta.mydomain.com/mnt/btr_pool target /mnt/btr_backup/beta subvolume rootfs subvolume dbdata
This will pull backups from alpha/beta.mydomain.com and locally create:
Let's say we have a host (at 192.168.0.42) running btrbk with the setup of the time-machine example above, and we need a backup server to only fetch the snapshots.
/etc/btrbk/btrbk.conf (on backup server):
target_preserve_min no target_preserve 0d 10w *m
volume ssh://192.168.0.42/mnt/btr_pool target /mnt/btr_backup/my-laptop subvolume home snapshot_dir btrbk_snapshots snapshot_preserve_min all snapshot_create no
If the server runs btrbk with this config, 10 weeklies and all monthlies are received from 192.168.0.42. The source filesystem is never altered because of
Common virtual machine setups have multiple volume sections with same host, but distinct port numbers for each machine.
# This propagates to all subvolume sections: target /mnt/btr_backup/
volume ssh://localhost:2201/mnt/btr_pool group vm vm01 subvolume home snapshot_name vm01-home subvolume data snapshot_name vm01-data
volume ssh://localhost:2202/mnt/btr_pool group vm vm02 subvolume home snapshot_name vm02-home
volume ssh://localhost:2203/mnt/btr_pool [...]
This will create
Note that btrbk holds a single reference to every btrfs filesystem tree, regarding UUID's as "globally unique". If the configured subvolumes point to the same filesystem on different machines (ports), you will see log lines like this when running
Assuming same filesystem: "ssh://localhost:2201/dev/sda1", "ssh://localhost:2202/dev/sda1"
If you want to make backups from a filesystem other than btrfs (e.g. ext4 or reiserfs), you need to create a synchronization subvolume on the backup disk:
# btrfs subvolume create /mnt/btr_backup/myhost_sync
Configure btrbk to use
myhost_syncas source subvolume:
volume /mnt/btr_backup subvolume myhost_sync snapshot_name myhost
snapshot_preserve_min latest snapshot_preserve 14d 20w *m
The btrbk package provides the "btrbk-mail" script, which automates the synchronization using rsync, and can be run as cron job or systemd timer unit. For configuration details, see the config section in "/contrib/cron/btrbk-mail".
Alternatively, you can run any synchronization software prior to running btrbk. Something like:
#!/bin/sh rsync -az --delete \ --inplace --numeric-ids --acls --xattrs \ -e 'ssh -i /etc/btrbk/ssh/id_rsa' \ myhost.mydomain.com:/data/ \ /mnt/btr_backup/myhost_sync/
exec /usr/bin/btrbk -q run
This will produce snapshots
/mnt/btr_backup/myhost.20150101, with retention as defined with the snapshot_preserve option.
If your backup server does not support btrfs, you can send your subvolumes to a raw file.
This is an experimental feature: btrbk supports "raw" targets, meaning that similar to the "send-receive" target the btrfs subvolume is being sent using
btrfs send(mirroring filesystem level data), but instead of instantly being received (
btrfs receive) by the target filesystem, it is being redirected to a file, optionally compressed and piped through GnuPG.
raw_target_compress xz raw_target_encrypt gpg gpg_keyring /etc/btrbk/gpg/pubring.gpg gpg_recipient [email protected]
volume /mnt/btr_pool subvolume home target raw ssh://cloud.example.com/backup ssh_user btrbk # incremental no
This will create a GnuPG encrypted, compressed files on the target host. For each backup, two files are created:
/backup/home.YYYYMMDD.btrfs.xz.gpg: main data file containing the btrfs send-stream,
/backup/home.YYYYMMDD.btrfs.xz.gpg.info: sidecar file containing metadata used by btrbk.
I you are using raw incremental backups, please make sure you understand the implications (see btrbk.conf(5), TARGET TYPES).
Since btrbk needs root access, it is very advisable to take all the security precautions you can. In most cases backups are generated periodically without user interaction, so it is not possible to protect your ssh key with a password. The steps below will give you hints on how to secure your ssh server for a backup scenario. Note that the btrbk package is not required on the remote side, but you will need the
btrfsexecutable from the btrfs-progs package.
On the client side, create a ssh key dedicated to btrbk, without password protection:
ssh-keygen -t rsa -b 4096 -f /etc/btrbk/ssh/id_rsa -C [email protected] -N ""
The content of the public key (/etc/btrbk/ssh/idrsa.pub) is used for authentication in "authorizedkeys" on the server side (see sshd(8) for details).
The most straight forward setup is to allow root login on the remote host. If this is not an option for you, refer to the more complex "Dedicated Btrbk User Login" section below.
Add your btrbk public key to "/root/.ssh/authorized_keys" on the server, and you are good to go.
Restrict ssh access to a static IP address within your network. On the remote host, either add a "Match" block in:
Match Address 192.168.0.42
Or restrict in authorized_keys:
Consult the sshd_config(5) man-page for a detailed explanation and more options.
If allowing root login is not an option for you, there are several ways to restrict SSH access to a regular user.
On the client side, configure btrbk use the sudo backend. This changes the ssh calls to btrfs commands to
On the remote host, grant root permissions for the "btrfs" command groups (subcommands) in "/etc/sudoers". If you are using sshfilterbtrbk(1), also add the
ssh_filter_btrbk.sh --sudooption in "authorized_keys" (see below).
Instead of using the all-inclusive
btrfscommand, "btrfs-progs-btrbk" allows you to restrict privileges to its subcommands using linux capabilities(7) or setuid.
Note that the "btrfs-progs-btrbk" package is not available on all linux distributions, you might need to build and install it on your own (refer to btrfs-progs-btrbk on GitHub for more details).
Make sure that only the required binaries with elevated privileges can be called by the btrbk user. For example, on a server acting as "btrbk source", allow only the following binaries for the "btrbk" group:
# getcap /usr/bin/btrfs-* /usr/bin/btrfs-send cap_dac_read_search,cap_fowner,cap_sys_admin=ep /usr/bin/btrfs-subvolume-delete cap_dac_override,cap_sys_admin=ep /usr/bin/btrfs-subvolume-list cap_dac_read_search,cap_fowner,cap_sys_admin=ep /usr/bin/btrfs-subvolume-show cap_dac_read_search,cap_fowner,cap_sys_admin=ep /usr/bin/btrfs-subvolume-snapshot cap_dac_override,cap_dac_read_search,cap_fowner,cap_sys_admin=ep
ls -l /usr/bin/btrfs-*
-rwx--x--- 1 root btrbk /usr/bin/btrfs-send -rwx--x--- 1 root btrbk /usr/bin/btrfs-subvolume-delete -rwx--x--- 1 root btrbk /usr/bin/btrfs-subvolume-list -rwx--x--- 1 root btrbk /usr/bin/btrfs-subvolume-show -rwx--x--- 1 root btrbk /usr/bin/btrfs-subvolume-snapshot
Btrbk comes with a shell script "sshfilterbtrbk.sh", which restricts ssh access to sane calls to the "btrfs" command needed for snapshot creation and send/receive operations (see sshfilterbtrbk(1)).
Copy "sshfilterbtrbk.sh" to "/backup/scripts/", and configure sshd to run it whenever the key is used for authentication. Example "/root/.ssh/authorized_keys":
# example backup source (also allowing deletion of old snapshots) command="/backup/scripts/ssh_filter_btrbk.sh -l --source --delete" ...
example backup target (also allowing deletion of old snapshots)
command="/backup/scripts/ssh_filter_btrbk.sh -l --target --delete" ...
example fetch-only backup source (snapshot_preserve_min=all, snapshot_create=no),
restricted to subvolumes within /home or /data
command="/backup/scripts/ssh_filter_btrbk.sh -l --send -p /home -p /data" ...
Btrbk does not provide any mechanism to restore your backups, this has to be done manually. In the instructions below, we assume that you have a btrfs volume mounted at
/mnt/btr_pool, and the subvolume you want to restore is at
Important: don't use
btrfs property setto make a subvolume read-write after restoring. This is a low-level command, and leaves "Received UUID" in a false state which causes btrbk to fail on subsequent incremental backups. Instead, use
btrfs subvolume snapshot(without
-rflag) as described below.
# list snapshots managed by btrbk btrbk list snapshots
alternative: list all subvolumes
btrbk ls / btrbk ls -L /
From the list, identify the snapshot you want to restore. Let's say it's
(skip this step if you restore from a snapshot)
# locally mounted backup disk btrfs send /mnt/btr_backup/data.20150101 | btrfs receive /mnt/btr_pool/
from / to remote host
Hint: Try to send-receive backups incrementally if possible. In case you still have common snapshot / backup pairs (i.e. both "snapshotsubvol" and "targetsubvol" are listed above), use
btrfs send -p.
From this point on,
data.20150101on both disks can be used as parents for subsequent send-receive operations, and a received_uuid relationship is established (see below).
# if still present, move broken subvolume away mv /mnt/btr_pool/data /mnt/btr_pool/data.BROKEN
create read-write subvolume
btrfs subvolume snapshot /mnt/btr_pool/data.20150101 /mnt/btr_pool/data
datasubvolume is restored, you can carry on with incremental backups to
# if everything went fine, delete the broken subvolume btrfs subvolume delete /mnt/btr_pool/data.BROKEN
Make sure to keep
data.20150101subvolumes on both disks at least until you created a new backup using btrbk, in order to keep the incremental chain alive.
btrbk origin -t /mnt/btr_backup/data.20150101 btrbk ls -L /mnt/btr_pool /mnt/btr_backup
received_uuid relationship: correlated, identical read-only subvolumes, cross-filesystem.
a.received_uuid = b.received_uuid a.received_uuid = b.uuid
btrfs send | btrfs receive.
/mnt/btr_pool/data.20150101 === /mnt/btr_backup/data.20150101
parent_uuid relationship: "is-snapshot-of"
a.parent_uuid = b.uuid
btrfs subvolume snapshotor
btrfs send -p | btrfs receive.
Make sure to also read the btrbk FAQ page. Help improve it by asking!
So btrbk saved your day?
I will definitively continue developing btrbk for free, but if you want to support me you can do so:
The source code for btrbk is managed using Git.
git clone https://dev.tty0.ch/btrbk.git
Mirror on GitHub:
git clone https://github.com/digint/btrbk.git
Your contributions are welcome!
If you would like to contribute or have found bugs:
Any feedback is appreciated!
btrbk is free software, available under the GNU General Public License, Version 3 or later.