#!/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 "$@"