From 0e2f95337de523040b2a93c90ceb02f290dc80f6 Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 21 Aug 2022 04:45:56 +0300 Subject: [PATCH] feat: Minor changes --- src/boring-backup | 67 ++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/src/boring-backup b/src/boring-backup index 1936117..91365fd 100755 --- a/src/boring-backup +++ b/src/boring-backup @@ -21,10 +21,6 @@ __config= __verbose= __log_file='./log.txt' __pid_file='/tmp/boring-backup.pid' -__errors_count=0 -__errors_file='/tmp/boring-backup.errors' - -echo "$__errors_count" > "$__errors_file" LIBRARY="${LIBRARY:-./lib}" @@ -34,7 +30,6 @@ if [ -f "$LIBRARY/lib.sh" ]; then . "$LIBRARY/lib.sh" else echo "Error: Cannot source library $LIBRARY/lib.sh: No such file" >&2 - exit 1 fi print_help() { @@ -120,21 +115,27 @@ while (( "$#" )); do esac done +# Exit if library is not loaded +if ! declare -F -- log > /dev/null 2>&1; then + exit 1 +fi + # ---------------------------------------------------------- # # * Do backups # # ---------------------------------------------------------- # -log "Backup STARTED" +log -V "Backup STARTED" # Check PID file if [ -e "$__pid_file" ]; then # shellcheck disable=SC2009 # shellcheck disable=SC2143 if [ -z "$(ps ax -o pid | grep "$(cat "$__pid_file")")" ]; then - err "Process $(cat "$__pid_file") died." + log -p "Process $(cat "$__pid_file") died." >&2 rm "$__pid_file" else - err -e "Process $(cat "$__pid_file") still running."; + echo "Process $(cat "$__pid_file") still running." >&2 + exit 1 fi fi @@ -149,29 +150,30 @@ __i=1 # iterator date +'Start: %d %b %Y %T %z' log -p "Library path: $LIBRARY" log -p "Log file: $__log_file" -log -p "Configuration file: $(if [ "$__config" ]; then \ - echo not specified; else echo "$__config"; fi)" +log -p "Configuration file:" \ + "$([ "$__config" ] || echo not specified && echo "$__config")" log -p "Total scripts: $__count" log "Scripts to process (${__count}): ${__args[*]}" for script in "${__args[@]}"; do - # Initialise variables - __user_script="$script" + echo # Just print new line + # Initialise variables + # shellcheck disable=SC2034 + backup_script="$script" # shellcheck disable=SC2034 backups=() # Array of created backups, contains full pathes - # shellcheck disable=SC2034 - errors=() # Array of error messages written by err() function - # Source scripts + echo -e "\e[1m--> Script: ${script##*/} [$__i/$__count]\e[0m" | log -p + + # Source script source_script "$script" # Config can ovewrite script functions and variables # shellcheck source=/dev/null [ -n "$__config" ] && . "$__config" - echo -e "\n\e[1m--> Script: ${__args[__i-1]##*/} [$__i/$__count]\e[0m" | - log -p + # --- Run user script -- # # Run prepare() before all if set if is_function_set prepare; then @@ -204,29 +206,24 @@ for script in "${__args[@]}"; do unset on_error # Unset user defined variables + unset backups unset compression - #unset name_prefix - unset name_date_fmt - #unset s3_access_key - #unset s3_secret_key - #unset s3_access_token - #unset s3_endpoint - #unset s3_region - #unset s3cmd_config + unset name_prefix + unset name_date_format + unset s3_access_key + unset s3_secret_key + unset s3_access_token + unset s3_endpoint + unset s3_region + unset s3cmd_config unset tar_options unset tar_exclude + unset mysqldump_options + unset pg_dump_options done -if [[ "$(cat $__errors_file)" != '0' ]]; then - echo - log -p "Backup [Failed]: Process finished with $(cat $__errors_file) " \ - "errors. See $__log_file for info." - rm "$__errors_file" -else - echo -e "\nBackup [Done]" -fi - -log "Backup FINISHED" +echo -e "\nBackup [Done]" +log -V "Backup FINISHED" # Remove PID file rm "$__pid_file"