Backup for Btrfs with BtrBk

For my Linux laptop, I was looking for a replacement of my previous backup solution, which used rsync and was therefore quite slow, as it had to sift through the whole filesystem. As I migrated from ext4 to btrfs during my last OS upgrade, I switched my backup system to use the (quite simple) btrbk backup script.

My goals for this are:

  • Use an encrypted external SSD drive
  • Run snapshots regularly in the background through cron / systemd
  • Backup the snapshots to the external SSD automatically whenever I dock my laptop
  • Unmount the external SSD after the backup finished to I can un-dock my laptop

I attached an external SSD drive to my screen’s USB hub to keep things tidy, formatted it using LUKS and btrfs and set the following options to configure btrbk in /etc/btrbk/btrbk.conf:

lockfile                   /var/lock/btrbk.lock
# Snapshots are for locally retained snapshots
snapshot_preserve_min   2d
snapshot_preserve       14d
# Target is the actual backup drive
target_preserve_min     no
target_preserve         20d 10w *m

# Backup to external disk mounted on /media/backup-usb
volume /media/btrfs-root
  # Create snapshots in /mnt/btr_pool/btrbk_snapshots
  snapshot_dir btrbk_snapshots

  # Target for all subvolume sections:
  target /media/backup-usb

  subvolume @
  subvolume @home

You can read-up on the various options for snapshot/backup retention in the btrfs manpage. The other options basically do:

  • Tell btrbk to create snapshots of the subvolumes @ and @home from the root of by btrfs filesystem mounted at /media/btrfs-root
  • Use /media/backup-usb (the external SSD drive) as the target for backing up the snapshots

Then I changed the default btrbk systemd unit file to only create the snapshots (triggered through the default btrbk.timer unit).

/etc/systemd/system/btrbk.service.d/override.conf edited using sudo systemctl edit btrbk.service:

[Service]
ExecStart=/usr/bin/btrbk snapshot

And I added a mount point for the external drive in /etc/fstab:

/dev/mapper/luks-24e9cb66-62f4-4382-8bfe-92eae9f8eaba /media/backup-usb btrfs subvol=@,acl,noatime 0    1

Then I added an override for the (on the fly generated) mount point service: /etc/systemd/system/media-backup\x2dusb.mount.d/override.conf edited using sudo systemctl edit media-backup\\x2dusb.mount:

[Unit]
# Propagate start/stop/restart of the BindsTo service to this one. I.e. unmount the drive after completing the backup
BindsTo=btrbk-backup.service

And finally created a new systemd unit to run the backup part (as opposed to the snapshot part) of btrbk. /etc/systemd/system/btrbk-backup.service.

# Starts a 'btrbk resume' run (i.e. puts all snapshots onto the backup volume)
# after the backup volume has been mounted in the system
# ATTENTION:
# Also depends on /etc/systemd/system/btrbk.service.d/override.conf setting 'ExecStart=/usr/bin/btrbk snapshot' to only run the snapshot part from a timer
# And 'systemctl edit media-backup\\x2dusb.mount' setting BindsTo=btrbk-backup.service in order to start the backup and unmount the drive afterwards

[Unit]
Description=btrbk backup to external disk
Documentation=man:btrbk(1)
# After means If both this and the After service are started, wait for the After service before starting this one
After=media-backup\x2dusb.mount

[Service]
Type=oneshot
ExecStart=/usr/bin/btrbk resume

[Install]
# WantedBy means start this service when the WantedBy is started
WantedBy=media-backup\x2dusb.mount

After adding the new service you need to run:

$ sudo systemctl daemon-reload
$ sudo systemctl enable btrbk-backup.service

Now you can watch the logs for the relevant services and mount the drive to test it out:

$ sudo journalctl -f -u btrbk-backup.service -u media-backup\x2dusb.mount

It should run the btrbk resume command and unmount the drive straight away. What remains for me is setting up auto-mounting with gnome. I do store the LUKS password in my keyring, but it would be nice to auto-mount the external drive whenever I dock my laptop. Sure I can enable auto-mounting in general in gnome, but that’s not what I want.

Leave a Reply

Your email address will not be published. Required fields are marked *