From: Bastian Dehn Date: Sat, 15 Aug 2026 07:35:53 +0000 (+0200) Subject: add first test start restore X-Git-Tag: v1.0.7^2~3^2~12 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=0d1d79650668a9dd9e0657c46d75e67735c1b87f;p=simple-backup.git add first test start restore --- diff --git a/Makefile b/Makefile index f636cce..8b463c3 100644 --- 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 \ diff --git a/src/restore b/src/restore index fe3285b..8df979b 100755 --- a/src/restore +++ b/src/restore @@ -1,184 +1,9 @@ #!/bin/bash -errlog() -{ - local lastexit=$? - local msg="$1" - - echo "❌ ERROR: $msg" - echo "🛑 EXIT: $lastexit" - return $lastexit -} - -useage() -{ - echo "$0 -d -n -r -p -o" - echo - echo " -d date from backup" - echo " -n number of backup" - echo " -r host @:" - 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 "$@" diff --git a/tests/backup_tests.bats b/tests/backup_tests.bats index d48cc4f..20d502e 100755 --- a/tests/backup_tests.bats +++ b/tests/backup_tests.bats @@ -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 index 0000000..c4565aa --- /dev/null +++ b/tests/restore_tests.bats @@ -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