From: Bastian Dehn Date: Mon, 17 Aug 2026 17:55:16 +0000 (+0200) Subject: add check directory or file exists in backup X-Git-Tag: v1.0.8^2~2 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=2a429e2bb72e40e6411e4c378de1e50e4f466ad9;p=simple-backup.git add check directory or file exists in backup --- diff --git a/src/restore b/src/restore index f47938f..636c919 100755 --- a/src/restore +++ b/src/restore @@ -77,6 +77,22 @@ exists_machines() --raw-output '.machines | keys[]' > /dev/null 2>&1 } +exists_file() +{ + local mode="$1" + local file="$2" + [ "$mode" != "file" ] && return 0 + [ -f "$file" ] +} + +exists_directory() +{ + local mode="$1" + local directory="$2" + [ "$mode" != "directory" ] && return 0 + [ -d "$directory" ] +} + ask_restore() { local prompt="$1" @@ -156,6 +172,13 @@ restore() "${backup_path}/${backup_dir}/${machine}${path}" "$hostpath") + exists_file "$mode" "${backup_path}/${backup_dir}/${machine}${path}" \ + || errlog "file $path not exists" \ + || return $? + exists_directory "$mode" "${backup_path}/${backup_dir}/${machine}${path}" \ + || errlog "directory $path not exists" \ + || return $? + ask_restore "❓ QUESTION: Do you want restore? [N/y]" \ || return $? diff --git a/tests/restore_tests.bats b/tests/restore_tests.bats index 68c288f..cba39c9 100755 --- a/tests/restore_tests.bats +++ b/tests/restore_tests.bats @@ -334,16 +334,17 @@ teardown() { # bats test_tags=restore:mode @test "success file mode" { mkdir --parents "build/test/2000-01-01_01_backup/testpc1.example.com/testpath" + touch "build/test/2000-01-01_01_backup/testpc1.example.com/testpath/testfile" - run main -d "2000-01-01" -n "01" -r "testpc1.example.com" -p "/testpath" + run main -d "2000-01-01" -n "01" -r "testpc1.example.com" -p "/testpath/testfile" assert_success assert_line --index 0 "⭐ START: restore" assert_line --index 1 "ℹ️ INFO: backup path build/test" assert_line --index 2 "ℹ️ INFO: backup name backup" assert_line --index 3 "ℹ️ INFO: restore mode file" - assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/testpc1.example.com/testpath" - assert_line --index 5 "ℹ️ INFO: remote path backupuser@www.example.com:/testpath" + assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/testpc1.example.com/testpath/testfile" + assert_line --index 5 "ℹ️ INFO: remote path backupuser@www.example.com:/testpath/testfile" } # bats test_tags=restore:mode @@ -364,16 +365,49 @@ teardown() { # bats test_tags=restore:mode @test "success file mode on localhost" { mkdir --parents "build/test/2000-01-01_01_backup/onlylocal/testpath" + touch "build/test/2000-01-01_01_backup/onlylocal/testpath/testfile" - run main -d "2000-01-01" -n "01" -r "onlylocal" -p "/testpath" + run main -d "2000-01-01" -n "01" -r "onlylocal" -p "/testpath/testfile" assert_success assert_line --index 0 "⭐ START: restore" assert_line --index 1 "ℹ️ INFO: backup path build/test" assert_line --index 2 "ℹ️ INFO: backup name backup" assert_line --index 3 "ℹ️ INFO: restore mode file" - assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/onlylocal/testpath" - assert_line --index 5 "ℹ️ INFO: remote path /testpath" + assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/onlylocal/testpath/testfile" + assert_line --index 5 "ℹ️ INFO: remote path /testpath/testfile" +} + +# bats test_tags=restore:check +@test "failure testfile not in backup" { + mkdir --parents "build/test/2000-01-01_01_backup/onlylocal/testpath" + + run main -d "2000-01-01" -n "01" -r "onlylocal" -p "/testpath/testfile" + + assert_failure + assert_line --index 0 "⭐ START: restore" + assert_line --index 1 "ℹ️ INFO: backup path build/test" + assert_line --index 2 "ℹ️ INFO: backup name backup" + assert_line --index 3 "ℹ️ INFO: restore mode file" + assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/onlylocal/testpath/testfile" + assert_line --index 5 "ℹ️ INFO: remote path /testpath/testfile" + assert_line --index 6 "❌ ERROR: file /testpath/testfile not exists" +} + +# bats test_tags=restore:check +@test "failure dir directory not in backup" { + mkdir --parents "build/test/2000-01-01_01_backup/onlylocal/testpath" + + run main -d "2000-01-01" -n "01" -r "onlylocal" -p "/testpath/dir/" + + assert_failure + assert_line --index 0 "⭐ START: restore" + assert_line --index 1 "ℹ️ INFO: backup path build/test" + assert_line --index 2 "ℹ️ INFO: backup name backup" + assert_line --index 3 "ℹ️ INFO: restore mode directory" + assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/onlylocal/testpath/dir/" + assert_line --index 5 "ℹ️ INFO: remote path /testpath/dir/" + assert_line --index 6 "❌ ERROR: directory /testpath/dir/ not exists" } # bats test_tags=restore:question @@ -383,16 +417,17 @@ teardown() { return 1 } mkdir --parents "build/test/2000-01-01_01_backup/onlylocal/testpath" + touch "build/test/2000-01-01_01_backup/onlylocal/testpath/testfile" - run main -d "2000-01-01" -n "01" -r "onlylocal" -p "/testpath" + run main -d "2000-01-01" -n "01" -r "onlylocal" -p "/testpath/testfile" assert_failure assert_line --index 0 "⭐ START: restore" assert_line --index 1 "ℹ️ INFO: backup path build/test" assert_line --index 2 "ℹ️ INFO: backup name backup" assert_line --index 3 "ℹ️ INFO: restore mode file" - assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/onlylocal/testpath" - assert_line --index 5 "ℹ️ INFO: remote path /testpath" + assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/onlylocal/testpath/testfile" + assert_line --index 5 "ℹ️ INFO: remote path /testpath/testfile" assert_line --index 6 "❓ QUESTION: Do you want restore? [N/y]" } @@ -403,33 +438,35 @@ teardown() { return 0 } mkdir --parents "build/test/2000-01-01_01_backup/onlylocal/testpath" + touch "build/test/2000-01-01_01_backup/onlylocal/testpath/testfile" - run main -d "2000-01-01" -n "01" -r "onlylocal" -p "/testpath" + run main -d "2000-01-01" -n "01" -r "onlylocal" -p "/testpath/testfile" assert_success assert_line --index 0 "⭐ START: restore" assert_line --index 1 "ℹ️ INFO: backup path build/test" assert_line --index 2 "ℹ️ INFO: backup name backup" assert_line --index 3 "ℹ️ INFO: restore mode file" - assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/onlylocal/testpath" - assert_line --index 5 "ℹ️ INFO: remote path /testpath" + assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/onlylocal/testpath/testfile" + assert_line --index 5 "ℹ️ INFO: remote path /testpath/testfile" assert_line --index 6 "❓ QUESTION: Do you want restore? [N/y]" } # bats test_tags=restore:rsync @test "success backup machine path rsync parameters" { mkdir --parents "build/test/2000-01-01_01_backup/testpc1.example.com/testpath" + touch "build/test/2000-01-01_01_backup/testpc1.example.com/testpath/testfile" RSYNC_LOG_RUNS="1" - run main -d "2000-01-01" -n "01" -r "testpc1.example.com" -p "/testpath" + run main -d "2000-01-01" -n "01" -r "testpc1.example.com" -p "/testpath/testfile" assert_success assert_line --index 0 "⭐ START: restore" assert_line --index 1 "ℹ️ INFO: backup path build/test" assert_line --index 2 "ℹ️ INFO: backup name backup" assert_line --index 3 "ℹ️ INFO: restore mode file" - assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/testpc1.example.com/testpath" - assert_line --index 5 "ℹ️ INFO: remote path backupuser@www.example.com:/testpath" + assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/testpc1.example.com/testpath/testfile" + assert_line --index 5 "ℹ️ INFO: remote path backupuser@www.example.com:/testpath/testfile" assert_line --index 6 "❓ QUESTION: Do you want restore? [N/y]" assert_line --index 7 "✅ SUCCESS: restore" assert_equal "$(awk 'NR==1 {print; exit}' "$RSYNC_LOG")" "9" @@ -440,8 +477,8 @@ teardown() { assert_equal "$(awk 'NR==6 {print; exit}' "$RSYNC_LOG")" "--hard-links" assert_equal "$(awk 'NR==7 {print; exit}' "$RSYNC_LOG")" "--human-readable" assert_equal "$(awk 'NR==8 {print; exit}' "$RSYNC_LOG")" "--rsh=ssh -p 22" - assert_equal "$(awk 'NR==9 {print; exit}' "$RSYNC_LOG")" "build/test/2000-01-01_01_backup/testpc1.example.com/testpath" - assert_equal "$(awk 'NR==10 {print; exit}' "$RSYNC_LOG")" "backupuser@www.example.com:/testpath" + assert_equal "$(awk 'NR==9 {print; exit}' "$RSYNC_LOG")" "build/test/2000-01-01_01_backup/testpc1.example.com/testpath/testfile" + assert_equal "$(awk 'NR==10 {print; exit}' "$RSYNC_LOG")" "backupuser@www.example.com:/testpath/testfile" } # bats test_tags=restore:rsync @@ -450,16 +487,17 @@ teardown() { return 1 } mkdir --parents "build/test/2000-01-01_01_backup/testpc1.example.com/testpath" + touch "build/test/2000-01-01_01_backup/testpc1.example.com/testpath/testfile" - run main -d "2000-01-01" -n "01" -r "testpc1.example.com" -p "/testpath" + run main -d "2000-01-01" -n "01" -r "testpc1.example.com" -p "/testpath/testfile" assert_failure assert_line --index 0 "⭐ START: restore" assert_line --index 1 "ℹ️ INFO: backup path build/test" assert_line --index 2 "ℹ️ INFO: backup name backup" assert_line --index 3 "ℹ️ INFO: restore mode file" - assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/testpc1.example.com/testpath" - assert_line --index 5 "ℹ️ INFO: remote path backupuser@www.example.com:/testpath" + assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/testpc1.example.com/testpath/testfile" + assert_line --index 5 "ℹ️ INFO: remote path backupuser@www.example.com:/testpath/testfile" assert_line --index 6 "❓ QUESTION: Do you want restore? [N/y]" assert_line --index 7 "❌ ERROR: could not restore" } @@ -467,17 +505,18 @@ teardown() { # bats test_tags=restore:rsync, restore:origin @test "success backup machine path rsync parameter delete" { mkdir --parents "build/test/2000-01-01_01_backup/testpc1.example.com/testpath" + touch "build/test/2000-01-01_01_backup/testpc1.example.com/testpath/testfile" RSYNC_LOG_RUNS="1" - run main -d "2000-01-01" -n "01" -r "testpc1.example.com" -p "/testpath" -o + run main -d "2000-01-01" -n "01" -r "testpc1.example.com" -p "/testpath/testfile" -o assert_success assert_line --index 0 "⭐ START: restore" assert_line --index 1 "ℹ️ INFO: backup path build/test" assert_line --index 2 "ℹ️ INFO: backup name backup" assert_line --index 3 "ℹ️ INFO: restore mode file" - assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/testpc1.example.com/testpath" - assert_line --index 5 "ℹ️ INFO: remote path backupuser@www.example.com:/testpath" + assert_line --index 4 "ℹ️ INFO: restore path build/test/2000-01-01_01_backup/testpc1.example.com/testpath/testfile" + assert_line --index 5 "ℹ️ INFO: remote path backupuser@www.example.com:/testpath/testfile" assert_line --index 6 "⚠️ WARN: DELETE MODE IS ON" assert_line --index 7 "❓ QUESTION: Do you want restore? [N/y]" assert_line --index 8 "✅ SUCCESS: restore" @@ -490,8 +529,8 @@ teardown() { assert_equal "$(awk 'NR==7 {print; exit}' "$RSYNC_LOG")" "--human-readable" assert_equal "$(awk 'NR==8 {print; exit}' "$RSYNC_LOG")" "--delete" assert_equal "$(awk 'NR==9 {print; exit}' "$RSYNC_LOG")" "--rsh=ssh -p 22" - assert_equal "$(awk 'NR==10 {print; exit}' "$RSYNC_LOG")" "build/test/2000-01-01_01_backup/testpc1.example.com/testpath" - assert_equal "$(awk 'NR==11 {print; exit}' "$RSYNC_LOG")" "backupuser@www.example.com:/testpath" + assert_equal "$(awk 'NR==10 {print; exit}' "$RSYNC_LOG")" "build/test/2000-01-01_01_backup/testpc1.example.com/testpath/testfile" + assert_equal "$(awk 'NR==11 {print; exit}' "$RSYNC_LOG")" "backupuser@www.example.com:/testpath/testfile" } # bats test_tags=restore:rsync, restore:origin