]> gitweb.hhaalo.de Git - simple-backup.git/commitdiff
add first test start restore
authorBastian Dehn <hhaalo@arcor.de>
Sat, 15 Aug 2026 07:35:53 +0000 (09:35 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 15 Aug 2026 07:35:53 +0000 (09:35 +0200)
Makefile
src/restore
tests/backup_tests.bats
tests/restore_tests.bats [new file with mode: 0755]

index f636cce5575ae2dc0042888c9050c7fdd46826c1..8b463c3ee23fc8df1fa3dea11002bf0183090603 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,7 @@ MANDIR=$(BUILDDIR)/$(PACKAGENAME)/$(PREFIX)/share/man
 all: test package
 
 test:
+       ./tests/restore_tests.bats
        ./tests/backup_tests.bats
 
 build: $(BINDIR)/backup \
index fe3285b5da4b0b747630f40f6ba5e367ef89e30c..8df979b0aeb1357e493188484a10a52e98d49c15 100755 (executable)
@@ -1,184 +1,9 @@
 #!/bin/bash
 
-errlog()
-{
-       local lastexit=$?
-       local msg="$1"
-
-       echo "❌ ERROR: $msg"
-       echo "🛑 EXIT: $lastexit"
-       return $lastexit
-}
-
-useage()
-{
-       echo "$0 -d <date> -n <number> -r <host> -p <path> -o"
-       echo
-       echo " -d    date from backup"
-       echo " -n    number of backup"
-       echo " -r    host <user>@<rechner>:<port>"
-       echo " -p    path"
-       echo " -o    restore origin (delete mode)"
-       echo
-}
-
-print_line()
-{
-       printf "#%.0s" {1..80}
-       printf "\n"
-}
-
-restore()
-{
-       local date="$1"
-       local number="$2"
-       local machine="$3"
-       local path="$4"
-       local origin="$5"
-       local backup_path=
-       local backup_name=
-       local remote_user=
-       local remote_machine=
-       local remote_port=
-       local rsyncargs=("--archive" \
-               "--verbose" \
-               "--sparse" \
-               "--acls" \
-               "--hard-links" \
-               "--human-readable")
-
-       backup_path=$(yq --raw-output ".backup_path" config.yaml)
-       backup_name=$(yq --raw-output ".backup_name" config.yaml)
-       machine=$(yq --raw-output \
-               ".machines | keys[] | select(. == \"$machine\")" config.yaml)
-       remote_user=$(yq --raw-output ".machines.\"$machine\".remote_user" \
-               config.yaml)
-       remote_machine=$(yq --raw-output ".machines.\"$machine\".host" \
-               config.yaml)
-       remote_port=$(yq --raw-output ".machines.\"$machine\".port" config.yaml)
-
-       [ -d "$backup_path" ] \
-               || errlog "could not find directory $backup_path" \
-               || return $?
-       [ -n "$machine" ] \
-               || errlog "config.yaml missing machine" \
-               || return $?
-       [ "$backup_name" != "null" ] \
-               || errlog "config.yaml missing backup_name" \
-               || return $?
-       [ "$remote_user" != "null" ] \
-               || errlog "config.yaml missing remote_user" \
-               || return $?
-       [ "$remote_machine" != "null" ] \
-               || errlog "config.yaml missing host" \
-               || return $?
-       [ "$remote_port" != "null" ] \
-               || errlog "config.yaml missing port" \
-               || return $?
-       rsyncargs+=("--rsh=ssh -p $remote_port")
-
-       local backup_dir="$date"_"$number"_"$backup_name"
-
-       [ -d "$backup_path/$backup_dir" ] \
-               || errlog "could not find directory $backup_path/$backup_dir" \
-               || return $?
-
-       local restore_prefix_path="$backup_path/$backup_dir/$machine"
-       [ -d "$restore_prefix_path" ] \
-               || errlog "could not find directory $restore_prefix_path" \
-               || return $?
-
-       local restore_path="$restore_prefix_path$path"
-
-       local hostpath="$remote_user@$remote_machine:$path"
-       [ "$remote_machine" == "localhost" ] && local hostpath="$path"
-
-       local mode="directory"
-       [[ "$path" =~ /$ ]] || mode="file"
-
-       [ -f "$restore_path" ] || [ -d "$restore_path" ] \
-               || errlog "could not find $restore_path" \
-               || return $?
-       rsyncargs+=("$restore_path")
-       rsyncargs+=("$hostpath")
-
-       echo "ℹ️  INFO: restore mode $mode"
-       echo
-
-       echo "ℹ️  INFO: restore path $restore_path"
-       echo "ℹ️  INFO: remote path $hostpath"
-       echo
-
-       [ "$origin" == "deletemode-on" ] \
-               && rsyncargs+=("--delete") \
-               && echo "⚠️  WARN: delete mode on" && echo
-
-       local ask_restore="N"
-       read -rp "❓ QUESTION: Do you want restore? [N/y] " ask_restore
-       local ask_restore=${ask_restore:-N}
-       local ask_restore=${ask_restore,,}
-       echo "${ask_restore,,}"
-
-       [ "$ask_restore" == "y" ] \
-               || errlog "you want not do restore" \
-               || return $?
-
-       echo "ℹ️  INFO: restoring..."
-
-       rsync "${rsyncargs[@]}" \
-               || errlog "could not restore" \
-               || return $?
-
-       echo "ℹ️  INFO: restored"
-}
-
 main()
 {
-       local date=
-       local number=
-       local machine=
-       local path=
-       local origin="deletemode-off"
-
        echo "⭐ START: restore"
-
-       while getopts "d:n:r:p:ho" optname; do
-               case "$optname" in
-               "d") date="$OPTARG" ;;
-               "n") number="$OPTARG" ;;
-               "r") machine="$OPTARG" ;;
-               "p") path="$OPTARG" ;;
-               "o") origin="deletemode-on" ;;
-               "h") useage && return 0 ;;
-               *) usage && return 0 ;;
-               esac
-       done
-
-       command -v yq > /dev/null \
-               || errlog "command yq not found" \
-               || return $?
-       command -v rsync > /dev/null \
-               || errlog "command rsync not found" \
-               || return $?
-       command -v ssh > /dev/null \
-               || errlog "command ssh not found" \
-               || return $?
-
-       [ -f "config.yaml" ] \
-               || errlog "file config.yaml not found" \
-               || return $?
-       [ -n "$date" ] || errlog "missing date (-d)" || return $?
-       [ -n "$number" ] || errlog "missing number (-n)" || return $?
-       [[ "$number" =~ [[:digit:]]{2} ]] \
-               || errlog "number must two digits" \
-               || return $?
-       [ -n "$machine" ] || errlog "missing machine (-r)" || return $?
-       [ -n "$path" ] || errlog "missing path (-p)" || return $?
-
-       restore "$date" "$number" "$machine" "$path" "$origin" \
-               || return $?
-
-       echo "✅ SUCCESS: restore"
 }
 
+[ "$TESTMODE" == "ON" ] && return 0
 main "$@"
index d48cc4fab1a3a45945ba4083e8f7a69c9321589d..20d502e4eefacc712325f6dde930208d902d4b2e 100755 (executable)
@@ -32,6 +32,7 @@ teardown() {
        unset NOCOLOR
        unset CONFIG_FILE
        unset RSYNC_LOG
+       unset RSYNC_LOG_RUNS
 }
 
 # bats test_tags=backup:start
diff --git a/tests/restore_tests.bats b/tests/restore_tests.bats
new file mode 100755 (executable)
index 0000000..c4565aa
--- /dev/null
@@ -0,0 +1,38 @@
+#!/usr/bin/bats
+
+setup() {
+       bats_load_library 'bats-support'
+       bats_load_library 'bats-assert'
+       export TESTMODE="ON"
+       export NOCOLOR="ON"
+       export CONFIG_FILE="src/config-example.yaml"
+       export RSYNC_LOG="build/test/rsync.log"
+       export RSYNC_LOG_RUNS="0"
+       source src/restore
+       rsync() {
+               [ "$RSYNC_LOG_RUNS" -eq "0" ] && return 0
+               ((RSYNC_LOG_RUNS--))
+               [ "$RSYNC_LOG_RUNS" -eq "0" ] || return 0
+
+               echo "${#@}" > "$RSYNC_LOG"
+               printf "%s\n" "$@" >> "$RSYNC_LOG"
+       }
+}
+
+teardown() {
+       rm --recursive --force "build/test"
+       rm --recursive --force "$RSYNC_LOG"
+       unset TESTMODE
+       unset NOCOLOR
+       unset CONFIG_FILE
+       unset RSYNC_LOG
+       unset RSYNC_LOG_RUNS
+}
+
+# bats test_tags=restore:start
+@test "success start message" {
+       run main
+
+       assert_success
+       assert_line --index 0 "⭐ START: restore"
+}
\ No newline at end of file