From: Bastian Dehn Date: Sun, 16 Aug 2026 14:58:49 +0000 (+0200) Subject: add block device check for media X-Git-Tag: v1.0.7^2~1^2~20 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=3a638d216ed01d62c21129aaa9143b6748621f04;p=simple-backup.git add block device check for media --- diff --git a/src/backupmount b/src/backupmount index 51ba770..65c257f 100755 --- a/src/backupmount +++ b/src/backupmount @@ -31,25 +31,45 @@ exists_config_file() || return $? } +exists_block_device() +{ + local media="$1" + local device="$2" + + [ -b "$device" ] \ + || errlog "block device $device for $media not found" \ + || return $? +} + +mount_device() +{ + local config="$1" + local media="$2" + + device=$(echo "$config" \ + | jq --raw-output ".media.\"$one_media\".device") + [ "$device" != "null" ] \ + || errlog "missing device for media $one_media in $CONFIG_FILE" \ + || return $? + exists_block_device "$media" "$device" || return $? +} + mount_backup() { local config="$1" local media= mapfile -t media < <(echo "$config" \ - | jq --raw-output ".media | keys[]" 2> /dev/null) + | jq --raw-output ".media | keys_unsorted[]" 2> /dev/null) [ "${#media[@]}" -gt 0 ] \ || errlog "missing media in $CONFIG_FILE" \ || return $? - local device= local one_media= for one_media in "${media[@]}"; do - device=$(echo "$config" \ - | jq --raw-output ".media.\"$one_media\".device") - [ "$device" != "null" ] \ - || errlog "missing device for media $one_media in $CONFIG_FILE" \ - || return $? + mount_device "$config" "$one_media" && return 0 done + + errlog "no device found" } umount_backup() diff --git a/src/config-example.yaml b/src/config-example.yaml index c30cb77..fcba470 100644 --- a/src/config-example.yaml +++ b/src/config-example.yaml @@ -33,14 +33,14 @@ machines: backup_volume: luks-crypt-volume media: day: - device: /dev/path/to/disk - key: /path/to/luks-key-file - partition: /dev/path/to/partition + device: /dev/path/to/day + key: /path/to/luks-key-file-day + partition: /dev/path/to/partday week: - device: /dev/path/to/disk - key: /path/to/luks-key-file - partition: /dev/path/to/partition + device: /dev/path/to/week + key: /path/to/luks-key-file-week + partition: /dev/path/to/partweek month: - device: /dev/path/to/disk - key: /path/to/luks-key-file - partition: /dev/path/to/partition \ No newline at end of file + device: /dev/path/to/month + key: /path/to/luks-key-file-month + partition: /dev/path/to/partmonth \ No newline at end of file diff --git a/tests/backupmount_tests.bats b/tests/backupmount_tests.bats index 0823c9b..f809780 100755 --- a/tests/backupmount_tests.bats +++ b/tests/backupmount_tests.bats @@ -131,4 +131,16 @@ teardown() { assert_failure assert_line --index 0 "⭐ START: backupmount" assert_line --index 1 "❌ ERROR: missing device for media day in src/config-example.yaml" +} + +# bats test_tags=backupmount:mount +@test "failure no block device found" { + run main "mount" + + assert_failure + assert_line --index 0 "⭐ START: backupmount" + assert_line --index 1 "❌ ERROR: block device /dev/path/to/day for day not found" + assert_line --index 2 "❌ ERROR: block device /dev/path/to/week for week not found" + assert_line --index 3 "❌ ERROR: block device /dev/path/to/month for month not found" + assert_line --index 4 "❌ ERROR: no device found" } \ No newline at end of file