diff --git a/src/functions.sh b/src/functions.sh index b9ca92b..6124175 100644 --- a/src/functions.sh +++ b/src/functions.sh @@ -6,34 +6,26 @@ declare -r systemrescue_version="0.0.1" systemrescue_cd_version="11.02" +systemrescue_data_dir="/var/backups/systemrescue-backup" +systemrescue_cache_dir="/var/cache/systemrescue-backup" +systemrescue_temp_dir="/tmp/systemrescue-backup" backup_engine="resticprofile" -restic_version="0.17.0" -resticprofile_version="0.28.0" scriptPath="$(dirname "$(readlink -f "$0")")" configPath="$(readlink -f "${scriptPath}/../config")" debug=false -if [[ ! -d "${configPath}" ]]; then +if [[ ! -r "${configPath}/srb.cfg" ]]; then # Start searching for common configuration paths if [[ -r "$HOME/.config/systemrescue-backup/srb.cfg" ]]; then configPath="$HOME/.config/systemrescue-backup" elif [[ -r "/etc/systemrescue-backup/srb.cfg" ]]; then configPath="/etc/systemrescue-backup" else - echoerr "No configuration file path found. Defaults will be used." 1>&2 + echoerr "Warning: No configuration file path found. Defaults will be used." fi fi -if [[ -f "${configPath}/srb.cfg" ]]; then - # Fail on any issues loading configuration - trap "echo \"ERROR: Configuration failed to load without error\"" ERR - set -e - source "${configPath}/srb.cfg" - set +e - trap - ERR -fi - ########################################################### ### Utility & Common Functions @@ -77,6 +69,18 @@ exit_fail() { exit "$rc" } +load_config() { + if [[ -f "${configPath}/srb.cfg" ]]; then + # Fail on any issues loading configuration + trap "echo \"ERROR: Configuration failed to load without error\"" ERR + set -e + source "${configPath}/srb.cfg" + set +e + trap - ERR + echoreg -d "Loaded configuration file: ${configPath}/srb.cfg" + fi +} + run-parts() { if [[ $# -lt 1 ]]; then return 1 @@ -159,12 +163,34 @@ load_module() { echoreg -d "Loaded Modules in: ${scriptPath}/${module}/${submod}/" } +run_modules() { + local module="$1" + local submodules=("init" "prepare" "execute" "finalize") + local order mod + + for load in "${submodules[@]}"; do + if [[ "$load" == "finalize" ]]; then + order=("$backup_engine" "$module") + else + order=("$module" "$backup_engine") + fi + + for mod in "${order[@]}"; do + if ! load_module "$mod" "$load"; then + exit_fail 200 "FATAL: Failed to load $module $load module. ($?)" + fi + done + + if [[ "$load" == "init" ]]; then + load_config + fi + done +} ########################################################### ### BACKUP and RESTORE FUNCTIONS ########################################################### -#dumpPartitions() { backup_partitions() { local rootPart rootDisk @@ -177,7 +203,6 @@ backup_partitions() { mount | grep "$rootPart" > "${restoreDir}/mounts.dump" } -#restorePartitions() { restore_partitions() { local rootPart rootDisk @@ -203,7 +228,6 @@ restore_partitions() { echo "Partition table and UUID information have been restored." } -#restoreBtrFSSubvolumes() { restore_btrfs_subvolumes() { local rootBase subvolID subvolPath @@ -219,7 +243,6 @@ restore_btrfs_subvolumes() { done < "${restoreDir}/btrfs.dump" } -#restoreBtrFSMounts() { restore_btfs_mounts() { local rootBase mountSource mountDest diff --git a/src/recovery/finalize/100_cleanup b/src/recovery/finalize/100_cleanup new file mode 100644 index 0000000..fe941e1 --- /dev/null +++ b/src/recovery/finalize/100_cleanup @@ -0,0 +1,8 @@ +#!/bin/bash + +umount "${systemrescue_data_dir}/build/build_into_srm/var/backups/systemrescue-backup/backupdb" &>/dev/null +umount "${systemrescue_data_dir}/build/build_into_srm/etc/resticprofile" &>/dev/null + +if [[ -d "${systemrescue_data_dir}/build/build_into_srm" ]]; then + rm -rf "${systemrescue_data_dir}/build/build_into_srm" || exit 1 +fi diff --git a/src/recovery/init/001_default b/src/recovery/init/001_default index 2dba41f..023dba4 100644 --- a/src/recovery/init/001_default +++ b/src/recovery/init/001_default @@ -1 +1,8 @@ +#!/bin/bash + echoreg -d "Loaded Recovery Initialization" + +# Setup Defaults according to what's been develeoped and tested +systemrescue_cd_version="11.02" +systemrescue_cd_iso_url="https://sourceforge.net/projects/systemrescuecd/files/sysresccd-x86/{SYSRESCUECD_VERSION}/systemrescue-{SYSRESCUECD_VERSION}-amd64.iso/download" +systemrescue_cd_customize_url=""https://gitlab.com/systemrescue/systemrescue-sources/-/raw/main/airootfs/usr/share/sysrescue/bin/sysrescue-customize?inline=false"" diff --git a/src/recovery/prepare/100_prepare b/src/recovery/prepare/100_prepare new file mode 100644 index 0000000..e729eea --- /dev/null +++ b/src/recovery/prepare/100_prepare @@ -0,0 +1,21 @@ +#!/bin/bash + +mkdir -p "$systemrescue_data_dir" || return 1 +mkdir -p "$systemrescue_temp_dir" || return 1 +mkdir -p "$systemrescue_cache_dir" || return 1 + +mkdir -p "${systemrescue_data_dir}/build/build_into_srm/var/backups/systemrescue-backup/backupdb" || return 1 +#mount --bind "${systemrescue_data_dir}/backupdb" "${systemrescue_data_dir}/build/build_into_srm/var/backups/systemrescue-backup/backupdb" || exit 1 +if ! mount --bind "${systemrescue_data_dir}/backupdb" "${systemrescue_data_dir}/build/build_into_srm/var/backups/systemrescue-backup/backupdb"; then + if ! cp -a "${systemrescue_data_dir}/backupdb/" "${systemrescue_data_dir}/build/build_into_srm/var/backups/systemrescue-backup/backupdb/"; then + exit_fail 205 "FATAL: Failed to copy backupdb files over to recovery image build" + fi +fi + +# mkdir -p "${systemrescue_data_dir}/build/build_into_srm/etc/resticprofile" +# if ! mount --bind "${resticprofile_config_dir}" "${systemrescue_data_dir}/build/build_into_srm/etc/resticprofile"; then +# if ! cp -a "${resticprofile_config_dir}" "${systemrescue_data_dir}/build/build_into_srm/etc/resticprofile"; then +# exit_fail 205 "FATAL: Failed to copy resticprofile config files over to recovery image build" +# fi +# fi +#mount --bind "/etc/resticprofile" "${systemrescue_data_dir}/build/build_into_srm/etc/resticprofile" || return 1 diff --git a/src/resticprofile/finalize/100_cleanup b/src/resticprofile/finalize/100_cleanup new file mode 100644 index 0000000..f1e4915 --- /dev/null +++ b/src/resticprofile/finalize/100_cleanup @@ -0,0 +1,5 @@ +#!/bin/bash + +if [[ -d "${systemrescue_data_dir}/build/build_into_srm/usr/local/bin" ]]; then + rm -rf "${systemrescue_data_dir}/build/build_into_srm/usr/local/bin" || return 1 +fi diff --git a/src/resticprofile/init/001_default b/src/resticprofile/init/001_default new file mode 100644 index 0000000..ba7c804 --- /dev/null +++ b/src/resticprofile/init/001_default @@ -0,0 +1,58 @@ +#!/bin/bash + +echoreg -d "Loaded resticprofile Recovery Module Initialization" + +# Setup Defaults according to what's been develeoped and tested +restic_version="0.17.0" +resticprofile_version="0.28.0" +resticprofile_config_dir="/etc/resticprofile" + +resticprofile_init_find_config() { + local -a checkpaths + local path + local os + + os=$(uname -s) + + case "$os" in + Darwin) + checkpaths+=( + "$HOME/Library/Preferences/resticprofile" + "/Library/Preferences/resticprofile" + ) + ;; + CYGWIN*|MINGW*|MSYS_NT*) + exit_fail 255 "FATAL: Windows is not supported";; + *) + checkpaths+=( + "$HOME/.config/resticprofile" + "/etc/xdg/resticprofile" + ) + ;; + esac + checkpaths+=( + "/usr/local/etc/restic/" + "/usr/local/etc/resticprofile" + "/etc/restic" + "/etc/resticprofile" + "/etc/restic" + "/opt/local/etc/restic" + "/opt/local/etc/resticprofile" + ) + + for path in "${checkpaths[@]}"; do + if compgen -G "${path}/profiles.*" > /dev/null; then + resticprofile_config_dir="$path" + break + fi + done + + # If it still doesn't exist, we got issues + if ! compgen -G "${resticprofile_config_dir}/profiles.*" > /dev/null; then + exit_fail 210 "Failed to find resticprofile configuration directory" + fi + + echoreg "Found resticprofile configuration in '$resticprofile_config_dir'" +} + +resticprofile_init_find_config diff --git a/src/resticprofile/prepare/100_prepare b/src/resticprofile/prepare/100_prepare new file mode 100644 index 0000000..7c9b763 --- /dev/null +++ b/src/resticprofile/prepare/100_prepare @@ -0,0 +1,50 @@ +#!/bin/bash + +echoreg "Using SystemRescueCD Version: $systemrescue_cd_version" +echoreg "Using Restic Version: $restic_version" +echoreg "Using ResticProfile Version: $resticprofile_version" + +mkdir -p "${systemrescue_cache_dir}" || return 1 +mkdir -p "${systemrescue_data_dir}/build/build_into_srm/usr/local/bin" || return 1 +mkdir -p "${systemrescue_temp_dir}/restic-install" || return 1 + +if [[ ! -f "${systemrescue_cache_dir}/restic_${restic_version}_linux_amd64.bz2" ]]; then + curl -C - -Lso "${systemrescue_cache_dir}/restic_${restic_version}_linux_amd64.bz2" "https://github.com/restic/restic/releases/download/v${restic_version}/restic_${restic_version}_linux_amd64.bz2" || return 1 +fi +bunzip2 -k "${systemrescue_cache_dir}/restic_${restic_version}_linux_amd64.bz2" || return 1 +chmod 755 "${systemrescue_cache_dir}/restic_${restic_version}_linux_amd64" +chown root:root "${systemrescue_cache_dir}/restic_${restic_version}_linux_amd64" +mv "${systemrescue_cache_dir}/restic_${restic_version}_linux_amd64" "${systemrescue_data_dir}/build/build_into_srm/usr/local/bin/restic" || return 1 + +if [[ ! -f "${systemrescue_cache_dir}/resticprofile_${resticprofile_version}_linux_amd64.tar.gz" ]]; then + curl -C - -Lso "${systemrescue_cache_dir}/resticprofile_${resticprofile_version}_linux_amd64.tar.gz" "https://github.com/creativeprojects/resticprofile/releases/download/v${resticprofile_version}/resticprofile_${resticprofile_version}_linux_amd64.tar.gz" || return 1 +fi +tar -xzf "${systemrescue_cache_dir}/resticprofile_${resticprofile_version}_linux_amd64.tar.gz" -C "${systemrescue_temp_dir}/restic-install" || return 1 +chmod 755 "${systemrescue_temp_dir}/restic-install/resticprofile" +chown root:root "${systemrescue_temp_dir}/restic-install/resticprofile" +mv "${systemrescue_temp_dir}/restic-install/resticprofile" "${systemrescue_data_dir}/build/build_into_srm/usr/local/bin/resticprofile" || return 1 +rm -rf "${systemrescue_temp_dir}/restic-install" || return 1 + +mkdir -p "${systemrescue_data_dir}/build/build_into_srm/etc/resticprofile" +if ! mount --bind "${resticprofile_config_dir}" "${systemrescue_data_dir}/build/build_into_srm/etc/resticprofile"; then + if ! cp -a "${resticprofile_config_dir}" "${systemrescue_data_dir}/build/build_into_srm/etc/"; then + exit_fail 205 "FATAL: Failed to copy resticprofile config files over to recovery image build" + fi +fi + +if $debug; then + echo "===========================================================================" + echo "Cache Dir: $systemrescue_cache_dir" + ls -lR "$systemrescue_cache_dir" + echo "=====" + echo + + echo "Data Dir: $systemrescue_data_dir" + ls -lR "$systemrescue_data_dir" + echo "=====" + echo + + echo "Temp Dir: $systemrescue_temp_dir" + ls -lR "$systemrescue_temp_dir" + echo "===========================================================================" +fi diff --git a/src/srb b/src/srb index d72b18b..feb2e8c 100755 --- a/src/srb +++ b/src/srb @@ -127,7 +127,12 @@ main() { case "$1" in --cd-version=*) systemrescue_cd_version="${1#*=}" - echoreg -d "SystemRescueCD Version arg = $systemrescue_cd_version" + echoreg -d "Args: SystemRescueCD Version = $systemrescue_cd_version" + shift + ;; + --cd-url=*) + systemrescue_cd_url="${1#*=}" + echoreg -d "Args: SystemRescueCD Download url = $systemrescue_cd_url" shift ;; *) @@ -140,9 +145,21 @@ main() { echo "SystemRescue Backup -- Recovery CD" echoreg -d "opt: $*" - if ! load_module recovery init; then - exit_fail 200 "FATAL: Failed to load recovery init module. ($?)" - fi + + run_modules recovery + + # if ! load_module recovery init; then + # exit_fail 200 "FATAL: Failed to load recovery init module. ($?)" + # fi + # if ! load_module "$backup_engine" init; then + # exit_fail 201 "FATAL: Failed to load '$backup_engine' init module. ($?)" + # fi + + # load_config + + # if ! load_module "$backup_engine" prepare; then + # exit_fail 202 "FATAL Failed to load '$backup_engine' prepare module. ($?)" + # fi ;; *) echo "Invalid command: $1"