Herr Bischoff

Use BorgBackup to Backup FreeBSD ZFS Jails

A little known fact is that Borg can be used on block devices as well as files. For most platforms it’s a rather involved process — not so for FreeBSD. ZFS snapshots make it really convenient to back up running jails without stopping them first.

Using Borg efficiently and automated requires SSH keys to be set up as well as using empty passwords for the Borg encryption keys.

First off, create Borg repositories for all jails you plan to back up on the remote machine. For each jail, run the following:

borg init -e keyfile backup@server.example.com:/backup/jails/<JAIL NAME>

Then, modify and safe the following script to /root/bin/backup-jails:

#!/usr/bin/env bash

JAILS=(jail1 jail2 jail3)

for NAME in "${JAILS[@]}"; do

    echo Snapshotting $NAME...
    zfs snap $JAILPATH/$NAME@now

    echo Backing up $NAME...
    zfs send $JAILPATH/$NAME@now | borg create --stats --list --compression lzma,6 $REPOSITORY::`date +%s` -
    zfs destroy $JAILPATH/$NAME@now

    echo Pruning $NAME...
    borg prune --list --stats $REPOSITORY --keep-daily=7

This script requires Bash to be installed, as tcsh has some unique difficulties with command substitution.

Just put this script in your root crontab and enjoy automatic jail backups.