A basic script that will use a btrbk configuration file to store backups on an encrypted Direct Attached Storage (DAS) device, e.g. a USB drive. The DAS unit will only be mounted during the backup process.


  • btrbk, and a working btrbk configuration file
  • A LUKS encryption key; and a DAS unit, that was LUKS-encrypted using that key.
  • On top, a btrfs file system, with all target directories for snapshots. (Alternatively, add mkdir statements to the script, where indicated).
  • An empty mount point for the DAS, on the computer. (Do neither create an entry in /etc/fstab, nor automount the device).
  • All configuration variables at the top of the script have been adapted to your specific context.

What the script will try to do:

  1. Unlock the DAS and mount the btrfs file system
  2. Remove earlier, but failed backups
  3. Back up, using the btrbk configuration given
  4. Balance the btrfs file system
  5. Log an overview of the backups made
  6. Log an overview on file system usage
  7. Unmount and close the ecrypted DAS

How to call it:

To download this script, first hover with your mouse over the listing below and press »Open code in new window«. Copying and pasting the colored and formatted listing below, as-is, won’t work.

# Configure these:
LUKS_UUID="258088ac-009f-485c-a74a-ccd773500ce2" # LUKS uuid of the DAS (find via: sudo blkid --match-token TYPE=crypto_LUKS)

echo "Backup to DAS start: $(date -Iseconds)" | tee "$LOG_FILE"

if /usr/bin/mountpoint -q "$MOUNTPOINT"
    echo "WARNING: $MOUNTPOINT already mounted, should have been automatically unmounted" | tee -a "$LOG_FILE" | tee /dev/stderr
    if /usr/bin/lsblk "/dev/mapper/$MAPPED" >> "$LOG_FILE" 2>&1
        echo "WARNING: $MAPPED already mapped, should have been automatically closed" | tee -a "$LOG_FILE" | tee /dev/stderr
        echo "Now mapping $MAPPED ..." | tee -a "$LOG_FILE"
        /sbin/cryptsetup luksOpen --key-file="$KEY_FILE" --type luks "/dev/disk/by-uuid/$LUKS_UUID" "$MAPPED" >> "$LOG_FILE" 2>&1

    if ! /usr/bin/lsblk "/dev/mapper/$MAPPED" >> "$LOG_FILE" 2>&1
        echo "ERROR: Could not open /dev/disk/by-uuid/$LUKS_UUID" | tee -a "$LOG_FILE" | tee /dev/stderr
        exit 1
        echo "Now mounting to $MOUNTPOINT ..." | tee -a "$LOG_FILE"
        /usr/bin/mount -t btrfs -o subvol=@,space_cache=v2,noatime "/dev/mapper/$MAPPED" "$MOUNTPOINT" >> "$LOG_FILE" 2>&1

if ! /usr/bin/mountpoint -q "$MOUNTPOINT"
    echo "ERROR: $MOUNTPOINT could not be mounted, exiting" | tee -a "$LOG_FILE" | tee /dev/stderr
    exit 1

# In case you want to auto-initialize every new backup DAS with target directories as specified in $BTRBK_CONF,
# add mkdir statements here, e.g.:
# mkdir -p "$MOUNTPOINT/fileserver/photos"

echo "Now removing failed backups ..." | tee -a "$LOG_FILE"
/usr/bin/btrbk -v -c "$BTRBK_CONF" clean >> "$LOG_FILE" 2>&1

echo "Now backing up ..." | tee -a "$LOG_FILE"
/usr/bin/btrbk -v -c "$BTRBK_CONF" resume >> "$LOG_FILE" 2>&1

echo "Now balancing ..." | tee -a "$LOG_FILE"
/usr/bin/btrfs balance start -musage=5 -dusage=10 "$MOUNTPOINT" >> "$LOG_FILE" 2>&1

echo "Now logging latest backups overview ..." | tee -a "$LOG_FILE"
/usr/bin/btrbk -c "$BTRBK_CONF" list latest >> "$LOG_FILE" 2>&1

echo "Now logging usage report ..." | tee -a "$LOG_FILE"
/usr/bin/btrbk -c "$BTRBK_CONF" usage >> "$LOG_FILE" 2>&1

echo "Now unmounting $MOUNTPOINT ..." | tee -a "$LOG_FILE"
/usr/bin/umount "$MOUNTPOINT"

echo "Now closing "$MAPPED" ..."  | tee -a "$LOG_FILE"
/sbin/cryptsetup luksClose "$MAPPED" >> "$LOG_FILE" 2>&1

echo "Backup to DAS end: $(date -Iseconds)" | tee -a "$LOG_FILE"

Image Credits:
Papirus icon for Terminal (modified) | GNU General Public License, version 3

This content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
For your attributions to us please use the word »tuxwise«, and the link

Back to top button