From cd9f398eaa0d079ea9882c75692c436c27a26183 Mon Sep 17 00:00:00 2001 From: Eric Renfro Date: Wed, 4 Sep 2024 21:13:49 -0400 Subject: [PATCH] Added parser and module loader --- src/functions.sh | 107 +++++++++++++++++++++++++++++- src/recovery/init/100_init | 0 src/srb | 129 +++++++++++++++++++++++++++++++++++++ 3 files changed, 234 insertions(+), 2 deletions(-) create mode 100644 src/recovery/init/100_init diff --git a/src/functions.sh b/src/functions.sh index 989f9fc..59f0285 100644 --- a/src/functions.sh +++ b/src/functions.sh @@ -4,13 +4,15 @@ ### Default global configuration definitions ########################################################### +declare -r systemrescue_version="0.0.1" +systemrescue_cd_version="11.02" +backup_engine="resticprofile" restic_version="0.17.0" resticprofile_version="0.28.0" -systemrescuecd_version="11.02" -backup_engine="resticprofile" scriptPath="$(dirname "$(readlink -f "$0")")" configPath="$(readlink -f "${scriptPath}/../config")" +debug=false if [[ ! -d "${configPath}" ]]; then # Start searching for common configuration paths @@ -41,7 +43,29 @@ is_bin_in_path() { builtin type -P "$1" &>/dev/null } +echoreg() { + if [[ "$1" == "-d" ]]; then + shift + if $debug; then + echo "$*" 1>&2 + fi + else + echo "$*" 1>&2 + fi +} + echoerr() { + if [[ "$1" == "-d" ]]; then + shift + if $debug; then + echo "$*" 1>&2 + fi + else + echo "$*" 1>&2 + fi +} + +echodebug() { echo "$*" 1>&2 } @@ -53,6 +77,85 @@ exit_fail() { exit "$rc" } +run-parts() { + if [[ $# -lt 1 ]]; then + return 1 + elif [[ ! -d "$1" ]]; then + return 2 + fi + + local script + + for script in "${1%/}"/*; do + case $script in + *~ | *.bak | */#*# | *.swp) + : ;; # Ignore backup/editor files + *.new | *.rpmsave | *.rpmorig | *.rpmnew) + : ;; # Ignore package management files + *) + if [[ -x "$script" ]]; then + if ! "$script"; then + echoerr "$script failed" + return $? + fi + fi + ;; + esac + done +} + +load-parts() { + if [[ $# -lt 1 ]]; then + echoerr -d "load-parts parameters are invalid" + return 1 + elif [[ ! -d "$1" ]]; then + echoerr -d "load-parts missing directory '$1'" + return 2 + fi + + local script + + for script in "${1%/}"/*; do + case $script in + *~ | *.bak | */#*# | *.swp) + : ;; # Ignore backup/editor files + *.new | *.rpmsave | *.rpmorig | *.rpmnew) + : ;; # Ignore package management files + *) + if [[ -r "$script" ]]; then + set -e + echoreg -d "Loading script: $script" + source "$script" + set +e + fi + ;; + esac + done +} + +load_module() { + local module="$1" + local submod="$2" + + if [[ -z "$module" ]]; then + echoerr -d "load_module missing module" + return 1 + elif [[ -z "$submod" ]]; then + echoerr -d "load_module missing submodule" + return 2 + else + if [[ ! -d "${scriptPath}/${module}" ]]; then + echoerr -d "load_module missing module directory '${scriptPath}/${module}'" + return 3 + elif [[ ! -d "${scriptPath}/${module}/${submod}" ]]; then + echoerr -d "load_module missing submodule directory '${scriptPath}/${submod}'" + return 4 + fi + fi + + load-parts "${scriptPath}/${module}/${submod}" +} + ########################################################### ### BACKUP and RESTORE FUNCTIONS diff --git a/src/recovery/init/100_init b/src/recovery/init/100_init new file mode 100644 index 0000000..e69de29 diff --git a/src/srb b/src/srb index 603c561..281fbca 100755 --- a/src/srb +++ b/src/srb @@ -10,3 +10,132 @@ else exit 255 fi +showHelp() { + local section="$1" + + echo "No help yet" + echo "Usage: $0 [global-options] [options]" + echo + echo "Commands:" + echo " backup Perform system backup" + echo " restore Perform system restore" + echo " recovery Update/Generate system recovery disc image" + echo + echo "Global Options:" + echo " --debug, -d Enable Debug Logging" + echo " --help, -h Show this help" + echo + echo "Options (per command):" + + if [[ -z "$section" || "$section" == "backup" ]]; then + echo "backup:" + echo " --exclude Exclude devices matching the pattern from backup or restore." + echo " --exclude-file Exclude devices listed in the specified file from backup or restore." + echo + fi + + if [[ -z "$section" || "$section" == "restore" ]]; then + echo "restore:" + echo " --exclude Exclude devices matching the pattern from backup or restore." + echo " --exclude-file Exclude devices listed in the specified file from backup or restore." + echo " --rename Rename a device during the restore process." + echo " --no-uuid Disable restoring of UUIDs during restore." + echo + fi + + if [[ -z "$section" || "$section" == "recovery" ]]; then + echo "recovery:" + echo " --sysrecue-cd-version=ver System Rescue CD version to use: $systemrescue_cd_version" + echo + fi + + echo "Examples:" + echo " $0 backup --exclude /dev/sda1" + echo " $0 restore --rename /dev/sda /dev/sdb" + echo +} + + + +# Main function to handle command-line arguments and invoke appropriate functions +main() { + local help=false + + #if [[ "$1" == "--help" ]]; then + # showHelp + # exit 0 + #fi + + while [[ $# -gt 0 ]]; do + case $1 in + -h|--help) + help=true + shift + #showHelp + #exit 0 + ;; + -d|--debug) + debug=true; + shift + ;; + #--*|-*) + # echo "Unknown option $1" + # exit 1 + # ;; + *) + POSITIONAL_ARGS+=("$1") # save positional arg + shift + ;; + esac + done + + set -- "${POSITIONAL_ARGS[@]}" # restore positional args + + if $help; then + showHelp "$1" + exit 0 + fi + + case "$1" in + version) + echo "SystemRescue Backup version v${systemrescue_version}" + exit 0 + ;; + backup) + echo "SystemRescue Backup -- Backup Mode" + ;; + restore) + echo "SystemRescue Backup -- Restore Mode" + ;; + recovery) + shift + while [[ "$#" -gt 0 ]]; do + case "$1" in + --sysrecue-cd-version=*) + systemrescue_cd_version="${1#*=}" + shift + ;; + *) + echo "Invalid option: $1" + echo "Use --help to display the help message." + exit 1 + ;; + esac + done + + echo "SystemRescue Backup -- Recovery CD" + echo "opt: $*" + if ! load_module recovery init; then + exit_fail 200 "FATAL: Failed to load recovery init module. ($?)" + fi + ;; + *) + echo "Invalid command: $1" + echo "Use --help to display the help message." + exit 1 + ;; + esac +} + +# Call the main function with all arguments +main "$@"