Sending btrfs snapshots to TrueNAS Core using btrbk

TLDR: Run a debian VM and mount a btrfs formatted zvol to the VM.

If you’re like me, you use Linux for desktop and TrueNAS Core for storage. Given all the strengths of OpenZFS, one might think it is a snap to run a ZFS linux desktop, but that has not been my experience. Sure, you can follow a guide to configure OpenZFS, but its kind of a pain. Personally, I use Fedora which ships root on btrfs by default, providing snapshot capabilities right out of the box. If only there was an easy way to send them to TrueNAS Core…

Introducing btrbk: btrbk manages btrfs snapshots and sends them to a remote backup location. This can be over SSH, onto an external USB hard drive, etc. It is also possible to manage btrfs snapshots manually.

This guide will show you how to setup a debian VM on TrueNAS Core to receive btrbk snapshots. For this guide, it will be assumed that only 32GB of storage is needed for OS files, and that 1TB of storage is needed for btrbk storage. Resizing the btrbk storage is theoretically possible but TBD for this guide. Clients are assumed to connect via SSH and use the btrfs-progs-btrbk backend on btrbk. These are choices not requirements, so feel free to adjust anything to suit your needs.

Outline

  • Create a 32 GB zvol for debian root
  • Create a 1 TB zvol for btrbk storage
  • Create a debian buster VM and add the zvol as a virtual disk to the VM
  • Choose expert installation for debian
  • Format the 1TB virtual disk for btrfs files
  • Install btrbk, setup btrbk user and btrfs-progs-btrbk backend
  • Configure btrbk on client machine

Creating zvols

Bhyve (FreeBSD’s hypervisor) can present a zvol to the guest as a virtual disk. To make VM creation easy, we want to create the zvol in advance of the vm. I will create two zvols: debian_10_os_boot (32 GB) to hold the OS files and btrbk_storage (1 TB) to hold the snapshots.

Since the only thing that is stored on these zvols is time-stamped snapshots, I recommend excluding these zvols from any ZFS snapshot tasks.

Configuring the VM

Setup a VM using The TrueNAS Core Virtual Machine Wizard. I will choose to have 1 core and 1 GB of memory. Use an existing disk image as VirtIO disk type and select debian_10_os_boot or whatever you named your 32GB zvol. Then, choose the debian-10.9.0-amd64-netinst.iso (10.9 at time of writing) as the installation media.

After completing the wizard, but before starting the virtual machine, go to your VM’s devices and edit the VNC device. Change the resolution to 800x600. Then, add a device to the VM. Choose type Disk, the btrbk_storage zvol (or whatever name you chose), with VirtIO Mode, default sector size, and 1004 for the device order.

Start up your VM, connect over VNC, and choose Advanced Options … Expert Install. Follow the installation guide using the defaults (or your preferences) until you reach Partition Disks.

I use the Guided - use entire disk option for the 32GB virtual disk (vda) with all files in one parition. Then, manually create a gpt partition on the 1TB virtual disk (vdb) and use the Create a new partition option to format the partition. Use all of the free space as a btrfs file system and specify the mount point to be /mnt/btrbk_storage (or your prefered mount location). In the end, your partitions should look like:

Continue using default settings, until software selection. Note that for this guide you do not need non-free, contrib, or source repositories. For software, choose only SSH server and standard system utilities.

Continue with the expert installer and make sure to select Yes when asked “Force GRUB installation to the EFI removable media path?” to prevent a GRUB issue on your first boot.

After installation completes: power off the VM and remove the CDROM device, then power on the VM.

First boot

Sometimes Linux guests on Bhyve get confused about the network location and you may need to run

sudo sed -i 's/enp0s5/enp0s4/' /etc/network/interfaces

and reboot in order to get network access.

Install dependencies

Since the virtual disk is a zvol, there is no need to scrub or worry about data integrity - this is taken care of by ZFS on the underlying system. Only a few tools are needed to handle sending and receiving btrbk snapshots.

Since btrbk will be used to run backups, need just a few dependancies on the server

sudo date
sudo apt update
sudo apt upgrade

sudo apt install btrbk

sudo adduser --system --shell /bin/sh --group btrbk

switch to the btrbk user with sudo su btrbk and run ssh-keygen to generate a key pair for the btrbk user then return to your normal user with exit.

use sudo visudo to edit the /etc/sudoers file and append the lines

# Allow btrbk to run btrfs, readlink, and test as root
btrbk   ALL=NOPASSWD:/usr/bin/btrfs,/usr/bin/readlink,/usr/bin/test

to enable the btrfs-progs-btrbk backend for btrbk.

Create a subvolume for each client like

cd /mnt/btrbk_storage
sudo btrfs subvolume create client_hostname

where client_hostname is the name of your desktop that will send snapshots.

Lastly, add your client’s public key to /home/btrbk/.ssh/authorized_keys - you may want to create a dedicated btrbk ssh key pair in /etc/btrbk/ssh on the client machine

Client setup

Whatever ssh key you choose to add to your VM, the associated private key should specified in /etc/btrbk.conf on the client machine.

Setup /etc/btrbk.conf to your desired config - check man btrbk.conf for details. Here is an example configuration on a fedora 33 desktop (after ssh setup).

transaction_log		/var/log/btrbk/btrbk.log

snapshot_preserve_min   latest

target_preserve		24h 7d 1m 1y

target_preserve_min     7d

ssh_user		btrbk
ssh_identity		/etc/btrbk/ssh/id_ed25519

# btrfs commands are prefixed with "sudo -n" (e.g. "sudo -n btrfs subvolume show")
backend			btrfs-progs-sudo

volume /home
  snapshot_create  ondemand
  subvolume .
    target ssh://btrbk.localdomain/mnt/btrbk_storage/desktop-linux

volume /
  snapshot_create ondemand
  subvolume .
    target ssh://btrbk.localdomain/mnt/btrbk_storage/desktop-linux

be sure to test the configuration with sudo btrbk -c /etc/btrbk/btrbk.conf dryrun.

Lastly add a systemd timer to run once per day at midnight, or when the computer turns on if the computer is off/suspended at that time.

[Unit]
Description=Daily btrbk backup creation

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

and an associated service file, /etc/systemd/system/btrbk-daily.service, with contents

[Unit]
Description=Daily btrbk backup creation

[Service]
Type=simple
ExecStart=/usr/sbin/btrbk -q -c /etc/btrbk/btrbk.conf run

Good luck and have fun!

1 Like