From af9406e2d9f4dd75efe97c75703a98afbb541bdc Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sun, 16 Aug 2026 18:55:55 +0200 Subject: [PATCH] add check partition and backup path before start action --- src/backupmount | 21 ++++++++++ tests/backupmount_tests.bats | 79 ++++++++++++++++++++++++++++++++---- 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/src/backupmount b/src/backupmount index fbbd8a0..17dd90f 100755 --- a/src/backupmount +++ b/src/backupmount @@ -51,6 +51,12 @@ exists_key() || return $? } +mount_partition() +{ + local partition="$1" + local backup_path="$2" +} + mount_device() { local config="$1" @@ -58,6 +64,8 @@ mount_device() local device= local key= local volume= + local partition= + local backup_path= device=$(echo "$config" \ | jq --raw-output ".media.\"$media\".device") @@ -82,6 +90,17 @@ mount_device() || return $? echo "ℹ️ INFO: volume open luks-crypt-volume" + partition=$(echo "$config" \ + | jq --raw-output ".media.\"$media\".partition") + [ "$partition" != "null" ] \ + || errlog "missing partition for media $media in $CONFIG_FILE" \ + || return $? + backup_path=$(echo "$config" \ + | jq --raw-output ".backup_path") + [ "$backup_path" != "null" ] \ + || errlog "missing backup path in $CONFIG_FILE" \ + || return $? + cryptsetupargs=("open" "--key-file $key" "$device" @@ -89,6 +108,8 @@ mount_device() cryptsetup "${cryptsetupargs[@]}" \ || errlog "could not open $device with $key" \ || return $? + + mount_partition "$partition" "$backup_path" || return $? } mount_backup() diff --git a/tests/backupmount_tests.bats b/tests/backupmount_tests.bats index 7c14080..c00578a 100755 --- a/tests/backupmount_tests.bats +++ b/tests/backupmount_tests.bats @@ -31,7 +31,7 @@ teardown() { assert_line --index 0 "⭐ START: backupmount" } -# bats test_tags=backupmount:start,backupmount:cmd +# bats test_tags=backupmount:cmd @test "failure yq command not found" { command() { return 1 @@ -43,7 +43,7 @@ teardown() { assert_line --index 0 "❌ ERROR: command yq not found" } -# bats test_tags=backupmount:start,backupmount:cmd +# bats test_tags=backupmount:cmd @test "failure jq command not found" { command() { [ "$2" != "jq" ] || return 1 @@ -55,7 +55,7 @@ teardown() { assert_line --index 0 "❌ ERROR: command jq not found" } -# bats test_tags=backupmount:start,backupmount:cmd +# bats test_tags=backupmount:cmd @test "failure cryptsetup command not found" { command() { [ "$2" != "cryptsetup" ] || return 1 @@ -92,7 +92,7 @@ teardown() { assert_line --index 0 "❌ ERROR: failed to load src/config-example.yaml" } -# bats test_tags=backupmount:start +# bats test_tags=backupmount:param @test "failure missing command parameter" { run main @@ -101,7 +101,7 @@ teardown() { assert_line --index 1 "❌ ERROR: missing command" } -# bats test_tags=backupmount:start +# bats test_tags=backupmount:param @test "failure unknown command parameter" { run main "heyho" @@ -110,7 +110,7 @@ teardown() { assert_line --index 1 "❌ ERROR: unknown command" } -# bats test_tags=backupmount:mount +# bats test_tags=backupmount:config @test "failure missing media" { yq() { echo '{}' @@ -123,7 +123,7 @@ teardown() { assert_line --index 1 "❌ ERROR: missing media in src/config-example.yaml" } -# bats test_tags=backupmount:mount +# bats test_tags=backupmount:config @test "failure missing device for media day" { yq() { echo '{ @@ -152,7 +152,7 @@ teardown() { assert_line --index 4 "❌ ERROR: no device found" } -# bats test_tags=backupmount:mount +# bats test_tags=backupmount:config @test "failure missing key for media day" { yq() { echo '{ @@ -199,7 +199,7 @@ teardown() { assert_line --index 2 "❌ ERROR: key notexistskey for media day not exists" } -# bats test_tags=backupmount:mount +# bats test_tags=backupmount:config @test "failure missing volume for media day" { yq() { echo '{ @@ -227,6 +227,67 @@ teardown() { assert_line --index 3 "❌ ERROR: missing volume in src/config-example.yaml" } +# bats test_tags=backupmount:config +@test "failure missing partition for media day" { + yq() { + echo '{ + "backup_volume": "luks-crypt-volume", + "media": { + "day": { + "device": "/dev/path/to/day", + "key": "/path/to/key" + } + } + }' + } + exists_block_device() { + return 0 + } + exists_key() { + return 0 + } + + run main "mount" + + assert_failure + assert_line --index 0 "⭐ START: backupmount" + assert_line --index 1 "ℹ️ INFO: media day with device /dev/path/to/day found" + assert_line --index 2 "ℹ️ INFO: use key /path/to/key" + assert_line --index 3 "ℹ️ INFO: volume open luks-crypt-volume" + assert_line --index 4 "❌ ERROR: missing partition for media day in src/config-example.yaml" +} + +# bats test_tags=backupmount:mount +@test "failure missing backup path" { + yq() { + echo '{ + "backup_volume": "luks-crypt-volume", + "media": { + "day": { + "device": "/dev/path/to/day", + "key": "/path/to/key", + "partition": "/path/to/part" + } + } + }' + } + exists_block_device() { + return 0 + } + exists_key() { + return 0 + } + + run main "mount" + + assert_failure + assert_line --index 0 "⭐ START: backupmount" + assert_line --index 1 "ℹ️ INFO: media day with device /dev/path/to/day found" + assert_line --index 2 "ℹ️ INFO: use key /path/to/key" + assert_line --index 3 "ℹ️ INFO: volume open luks-crypt-volume" + assert_line --index 4 "❌ ERROR: missing backup path in src/config-example.yaml" +} + # bats test_tags=backupmount:mount @test "success open volume for media day" { exists_block_device() { -- 2.47.3