feat: new error handling
This commit is contained in:
		@@ -16,6 +16,9 @@
 | 
			
		||||
# You should have received a copy of the GNU General Public License
 | 
			
		||||
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
			
		||||
 | 
			
		||||
set -o pipefail  # trace ERR through pipes
 | 
			
		||||
set -o errtrace  # trace ERR through 'time command' and other functions
 | 
			
		||||
 | 
			
		||||
VERSION='0.1.0'
 | 
			
		||||
LIBRARY="${LIBRARY:-./lib}"
 | 
			
		||||
 | 
			
		||||
@@ -29,10 +32,10 @@ else
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
print_help() {
 | 
			
		||||
    printf << EOF \
 | 
			
		||||
    printf \
 | 
			
		||||
"USAGE:
 | 
			
		||||
 | 
			
		||||
    $0 [-hVvs] [-c \033[4mfile\033[0m] [-l \033[4mfile\033[0m] \
 | 
			
		||||
    %s [-hVvs] [-c \033[4mfile\033[0m] [-l \033[4mfile\033[0m] \
 | 
			
		||||
[-p \033[4mfile\033[0m] file ...
 | 
			
		||||
 | 
			
		||||
OPTIONS:
 | 
			
		||||
@@ -54,19 +57,18 @@ OPTIONS:
 | 
			
		||||
    -l, --log-file=\033[4mfile\033[0m
 | 
			
		||||
        Log file.
 | 
			
		||||
 | 
			
		||||
    -p, --pid-file=\033[4mfile\033[0m
 | 
			
		||||
        PID file. Default: /tmp/boring_backup.pid
 | 
			
		||||
 | 
			
		||||
    -s, --use-syslog
 | 
			
		||||
        Write log into syslog via logger instead of log file. This option
 | 
			
		||||
        is incompatible with --log-file option and log_date_format variable.
 | 
			
		||||
 | 
			
		||||
    -p, --pid-file=\033[4mfile\033[0m
 | 
			
		||||
        PID file. Default: /tmp/boring_backup.pid
 | 
			
		||||
 | 
			
		||||
ENVIRONMENT:
 | 
			
		||||
 | 
			
		||||
    LIBRARY Path to boring_backup library. Current path is
 | 
			
		||||
            $LIBRARY
 | 
			
		||||
"
 | 
			
		||||
EOF
 | 
			
		||||
            %s
 | 
			
		||||
" "$0" "$LIBRARY"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# ---------------------------------------------------------- #
 | 
			
		||||
@@ -132,6 +134,7 @@ while (( "$#" )); do
 | 
			
		||||
            exit 0
 | 
			
		||||
            ;;
 | 
			
		||||
        -v|--verbose)
 | 
			
		||||
            # shellcheck disable=SC2034
 | 
			
		||||
            verbose_output=1
 | 
			
		||||
            shift
 | 
			
		||||
            ;;
 | 
			
		||||
@@ -145,15 +148,16 @@ while (( "$#" )); do
 | 
			
		||||
            log_file="$val"
 | 
			
		||||
            shift "$sft"
 | 
			
		||||
            ;;
 | 
			
		||||
        -s|--use-syslog)
 | 
			
		||||
            use_syslog=1
 | 
			
		||||
            shift
 | 
			
		||||
            ;;
 | 
			
		||||
        -p|--pid-file|--pid-file=*)
 | 
			
		||||
            optval "$1" "$2"
 | 
			
		||||
            pid_file="$val"
 | 
			
		||||
            shift "$sft"
 | 
			
		||||
            ;;
 | 
			
		||||
        -s|--use-syslog)
 | 
			
		||||
            # shellcheck disable=SC2034
 | 
			
		||||
            use_syslog=1
 | 
			
		||||
            shift
 | 
			
		||||
            ;;
 | 
			
		||||
        -*)
 | 
			
		||||
            echo "Error: Unknown option: $1" >&2
 | 
			
		||||
            exit 1
 | 
			
		||||
@@ -175,11 +179,14 @@ if ! declare -F -- log > /dev/null 2>&1; then
 | 
			
		||||
    exit 1
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Set up error handler
 | 
			
		||||
trap 'error $BASH_SOURCE $FUNCNAME $LINENO $? $BASH_COMMAND' ERR
 | 
			
		||||
 | 
			
		||||
# ---------------------------------------------------------- #
 | 
			
		||||
# * Do backups                                               #
 | 
			
		||||
# ---------------------------------------------------------- #
 | 
			
		||||
 | 
			
		||||
log -V "Backup STARTED"
 | 
			
		||||
log -V "Backup started"
 | 
			
		||||
 | 
			
		||||
# Check PID file
 | 
			
		||||
if [ -e "$pid_file" ]; then
 | 
			
		||||
@@ -275,7 +282,7 @@ for script in "${__args[@]}"; do
 | 
			
		||||
done
 | 
			
		||||
 | 
			
		||||
echo -e "\nBackup [Done]"
 | 
			
		||||
log -V "Backup FINISHED"
 | 
			
		||||
log -V "Backup finished successsfully"
 | 
			
		||||
 | 
			
		||||
# Remove PID file
 | 
			
		||||
rm "$pid_file"
 | 
			
		||||
 
 | 
			
		||||
@@ -66,22 +66,33 @@ log() {
 | 
			
		||||
    done
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
handle_error() {
 | 
			
		||||
error() {
 | 
			
		||||
    # Handle errors
 | 
			
		||||
    #
 | 
			
		||||
    # Usage: handle_error ERROR_MESSAGE
 | 
			
		||||
    # Usage: error SCRIPTNAME FUNCNAME LINENO EXIT_CODE COMMAND
 | 
			
		||||
 | 
			
		||||
    # Display amd log message
 | 
			
		||||
    log -p "$*" >&2
 | 
			
		||||
    log -p "Check $log_file log for details." >&2
 | 
			
		||||
    local script lastline funcname lasterr msg
 | 
			
		||||
    script="$1"
 | 
			
		||||
    funcname="$2"
 | 
			
		||||
    lastline="$3"
 | 
			
		||||
    lasterr="$4"
 | 
			
		||||
    shift 4
 | 
			
		||||
 | 
			
		||||
    # Display and log message
 | 
			
		||||
    msg="ERROR in $script:$lastline: function '$funcname': \
 | 
			
		||||
command '$*' failed with exit code $lasterr"
 | 
			
		||||
 | 
			
		||||
    log -p "\e[31m$msg\nCheck $log_file log for details.\e[0m" >&2
 | 
			
		||||
 | 
			
		||||
    # Run user defined error handler function if set
 | 
			
		||||
    if is_function_set on_error; then
 | 
			
		||||
        echo -e "Run \e[1mon_error()\e[0m" | log -p
 | 
			
		||||
        on_error "$*"
 | 
			
		||||
        on_error "$msg"
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    log -p "Backup ERROR: Exiting with previous errors" >&2; exit 1
 | 
			
		||||
    log -p Exiting with previous errors. >&2
 | 
			
		||||
    log -V Backup finished with errors.
 | 
			
		||||
    exit 1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
is_installed() {
 | 
			
		||||
 
 | 
			
		||||
@@ -63,17 +63,10 @@ src_mysqldump() {
 | 
			
		||||
    log "Run command: $*"
 | 
			
		||||
 | 
			
		||||
    # shellcheck disable=SC2154
 | 
			
		||||
    if "$@" 2>> "$log_file" > "$sqldump"; then
 | 
			
		||||
        # Compress file
 | 
			
		||||
        sqldump="$(compress_file "$sqldump")"
 | 
			
		||||
        # Append path to 'backups' array
 | 
			
		||||
        backups+=("$sqldump")
 | 
			
		||||
        log "Dump saved as: $sqldump"
 | 
			
		||||
    else
 | 
			
		||||
        remove_if_empty "$sqldump"
 | 
			
		||||
        handle_error \
 | 
			
		||||
            "Error: Something went wrong when executing command:\n\t$*"
 | 
			
		||||
    fi
 | 
			
		||||
    "$@" > "$sqldump" |& log
 | 
			
		||||
    sqldump="$(compress_file "$sqldump")" # Compress file
 | 
			
		||||
    backups+=("$sqldump")  # Append path to 'backups' array
 | 
			
		||||
    log "Dump saved as: $sqldump"
 | 
			
		||||
 | 
			
		||||
    unset MYSQL_PWD
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -64,17 +64,10 @@ src_pg_dump() {
 | 
			
		||||
    log "Run command: $*"
 | 
			
		||||
 | 
			
		||||
    # shellcheck disable=SC2154
 | 
			
		||||
    if "$@" 2>> "$log_file" > "$sqldump"; then
 | 
			
		||||
        # Compress dump
 | 
			
		||||
        sqldump="$(compress_file "$sqldump")"
 | 
			
		||||
        # Append path to 'backups' array
 | 
			
		||||
        backups+=("$sqldump")
 | 
			
		||||
        log "Dump file: $sqldump"
 | 
			
		||||
    else
 | 
			
		||||
        remove_if_empty "$sqldump"
 | 
			
		||||
        handle_error \
 | 
			
		||||
            "Error: Something went wrong when executing command:\n\t$*"
 | 
			
		||||
    fi
 | 
			
		||||
    "$@" > "$sqldump" |& log
 | 
			
		||||
    sqldump="$(compress_file "$sqldump")" # Compress file
 | 
			
		||||
    backups+=("$sqldump")
 | 
			
		||||
    log "Dump file: $sqldump"
 | 
			
		||||
 | 
			
		||||
    unset PGPASSWORD
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -86,12 +86,7 @@ src_tar() {
 | 
			
		||||
    log "Run command: $*"
 | 
			
		||||
 | 
			
		||||
    # shellcheck disable=SC2154
 | 
			
		||||
    if "$@" 2>> "$log_file"; then
 | 
			
		||||
        # Append path to 'backups' array
 | 
			
		||||
        backups+=("$archive")
 | 
			
		||||
    else
 | 
			
		||||
        remove_if_empty "$archive"
 | 
			
		||||
        handle_error \
 | 
			
		||||
            "Error: Something went wrong when executing command:\n\t$*"
 | 
			
		||||
    fi
 | 
			
		||||
    "$@" 2>> "$log_file"
 | 
			
		||||
    # Append path to 'backups' array
 | 
			
		||||
    backups+=("$archive")
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -59,12 +59,6 @@ tgt_cp() {
 | 
			
		||||
        set -- cp --archive "$backup" "$dst_path"
 | 
			
		||||
 | 
			
		||||
        log "Run command: $*"
 | 
			
		||||
 | 
			
		||||
        if "$@" 2>> "$log_file"; then
 | 
			
		||||
            : # Success
 | 
			
		||||
        else
 | 
			
		||||
            handle_error \
 | 
			
		||||
                "Error: Something went wrong when executing command:\n\t$*"
 | 
			
		||||
        fi
 | 
			
		||||
        "$@" |& log
 | 
			
		||||
    done
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -70,10 +70,6 @@ tgt_s3cmd() {
 | 
			
		||||
 | 
			
		||||
    # Upload backups
 | 
			
		||||
    # shellcheck disable=SC2154
 | 
			
		||||
    if "$@" 2>> "$log_file"; then
 | 
			
		||||
        : # Success
 | 
			
		||||
    else
 | 
			
		||||
        handle_error \
 | 
			
		||||
            "Error: Something went wrong when executing command:\n\t$*"
 | 
			
		||||
    fi
 | 
			
		||||
    #"$@" 2>> "$log_file"
 | 
			
		||||
    "$@" |& log
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -82,6 +82,7 @@ validate_targets() {
 | 
			
		||||
        __main_target="${file_targets[0]}"
 | 
			
		||||
        # Fail if __main_target's path is not a directory
 | 
			
		||||
        parse_uri "$__main_target"
 | 
			
		||||
        # shellcheck disable=SC2154
 | 
			
		||||
        __main_target_path="$path"
 | 
			
		||||
 | 
			
		||||
        # Make dirs if `make_target_dir` is set
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user