#!/bin/bash -e mount_bind_rootfs() { if ! mount | grep -q /rootfs; then mkdir -p /rootfs mount --bind / /rootfs fi } mount_efi() { if ! mount | grep -q /boot/efi; then mount /boot/efi fi } umount_bind_rootfs() { if mount | grep -q /rootfs; then umount /rootfs || lsof -n | grep /rootfs rmdir /rootfs fi } umount_efi() { if mount | grep -q /boot/efi; then umount /boot/efi || lsof -n | grep /boot/efi fi } case $1 in before) echo "Start of backup: $2 to $BORG_REPO" case $2 in rootfs) apt-get clean || true; mount_bind_rootfs;; boot-efi) mount_efi ;; esac ;; after) case $2 in rootfs) umount_bind_rootfs ;; boot-efi) umount_efi ;; esac echo "End of backup: $2 to $BORG_REPO" ;; esac