Skip to content

Instantly share code, notes, and snippets.

@hcartiaux
Last active September 10, 2025 16:02
Show Gist options
  • Select an option

  • Save hcartiaux/3c618ffb7661f4ab123794f4bfc3a339 to your computer and use it in GitHub Desktop.

Select an option

Save hcartiaux/3c618ffb7661f4ab123794f4bfc3a339 to your computer and use it in GitHub Desktop.
Convenient backup script to detect my encrypted USB backup disks, mount them and call bontmia
#!/bin/bash
ACTION="${1:-backup}"
# Backup configuration
declare TARGET="backup-perso"
declare MNTPOINT="/mnt/usbdisk"
declare SRC=~
# Device configurations
declare -A DEVICES=(
["usb-WD_My_Passport_0748_575831314139335735353037-0:0"]="backup_home_hd"
["ata-Samsung_Portable_SSD_T5_S4B5NR0N300922V"]="backup_home_flash"
["ata-WDC_WD40NMZW-59GX6S1_WD-WX41D38AXAP4"]="backup_work_hd"
)
declare -a DETECTED_DEVICES
function detect_available_disk() {
echo "==> Scanning for available backup devices..."
for device in "${!DEVICES[@]}"; do
if [ -e "/dev/disk/by-id/$device" ]; then
DETECTED_DEVICES+=("$device")
fi
done
if (( ${#DETECTED_DEVICES[@]} == 0 )); then
echo "ERROR: No recognized backup device found!"
exit 1
fi
}
function show_config() {
device=$1
echo "==========================================="
echo " Device: ${device}"
echo " Crypt name: ${DEVICES[$device]}"
echo " Source: ${SRC}"
echo " Destination: ${MNTPOINT}/${device}/${TARGET}"
echo "==========================================="
}
function mount_usbdisk() {
device=$1
echo "==> Opening LUKS container ${DEVICES[$device]}..."
if ! sudo cryptsetup open --type luks "/dev/disk/by-id/${device}" "${DEVICES[$device]}"; then
echo "ERROR: Failed to open LUKS container"
exit 1
fi
echo "==> Mounting ${device}..."
sudo mkdir -p "${MNTPOINT}/${DEVICES[$device]}"
if ! sudo mount "/dev/mapper/${DEVICES[$device]}" "${MNTPOINT}/${DEVICES[$device]}"; then
echo "ERROR: Failed to mount device"
sudo cryptsetup luksClose "${DEVICES[$device]}"
exit 1
fi
echo "==> /dev/mapper/${device} mounted to ${MNTPOINT}/${DEVICES[$device]}"
}
function backup() {
device=$1
echo "==> Starting backup with bontmia..."
if [ ! -d "${MNTPOINT}/${DEVICES[$device]}" ] || ! mountpoint -q "${MNTPOINT}/${DEVICES[$device]}"; then
echo "ERROR: Mount point ${MNTPOINT}/${DEVICES[$device]} is not mounted"
exit 1
fi
mkdir -p "${MNTPOINT}/${DEVICES[$device]}/$TARGET"
if bontmia --dest "${MNTPOINT}/${DEVICES[$device]}/$TARGET" \
--rotation 0minutes0hours7days4weeks6month0years \
$SRC
then
echo "==> Backup completed successfully"
else
echo "ERROR: Backup failed"
exit 1
fi
}
function umount_usbdisk() {
device=$1
echo "==> Unmounting ${device}..."
sync
if ! sudo umount "${MNTPOINT}/${DEVICES[$device]}" ; then
echo "WARNING: Failed to unmount cleanly, forcing..."
sudo umount -f "${MNTPOINT}/${DEVICES[$device]}" || true
fi
echo "==> Closing LUKS container ${DEVICES[$device]}..."
if ! sudo cryptsetup luksClose "${DEVICES[$device]}"; then
echo "WARNING: Failed to close LUKS container"
return 1
fi
echo "==> Device safely disconnected"
}
function usage() {
echo "Usage: $0 [ACTION]"
echo "Actions:"
echo " backup - Loop on available backup devices, mount, perform backup, unmount (default)"
echo " mount - Mount device only"
echo " umount - Unmount device only"
echo " detect - Show detected device and configuration"
echo
echo "Supported devices:"
for device in "${!DEVICES[@]}"; do
echo " - $device (${DEVICES[$device]})"
done
}
case "$ACTION" in
"backup")
detect_available_disk
for device in "${DETECTED_DEVICES[@]}"; do
show_config "${device}"
mount_usbdisk "${device}"
backup "${device}"
umount_usbdisk "${device}"
done
;;
"mount")
detect_available_disk
for device in "${DETECTED_DEVICES[@]}"; do
show_config "${device}"
mount_usbdisk "${device}"
done
;;
"umount")
detect_available_disk
for device in "${DETECTED_DEVICES[@]}"; do
show_config "${device}"
umount_usbdisk "${device}"
done
;;
"detect")
detect_available_disk
for device in "${DETECTED_DEVICES[@]}"; do
show_config "${device}"
done
;;
"help"|"-h"|"--help")
usage
;;
*)
echo "ERROR: Unknown action '$ACTION'"
echo
usage
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment