feat: Add errors counter and compression function. No compression by default.
This commit is contained in:
		@@ -25,7 +25,11 @@ __pid_file='/tmp/boring-backup.pid'
 | 
			
		||||
__tar_options='-acf'
 | 
			
		||||
__tar_exclude=
 | 
			
		||||
__name_date_fmt='_%Y%m%d-%H%M'
 | 
			
		||||
__compression='gzip'
 | 
			
		||||
__compression=
 | 
			
		||||
__errors_count=0
 | 
			
		||||
__errors_file='/tmp/boring-backup.errors'
 | 
			
		||||
 | 
			
		||||
echo $__errors_count > $__errors_file
 | 
			
		||||
 | 
			
		||||
LIBRARY="${LIBRARY:-./lib}"
 | 
			
		||||
 | 
			
		||||
@@ -201,8 +205,16 @@ for script in "${__args[@]}"; do
 | 
			
		||||
    unset tar_exclude
 | 
			
		||||
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]"
 | 
			
		||||
 | 
			
		||||
# Remove PID file
 | 
			
		||||
rm $__pid_file
 | 
			
		||||
 
 | 
			
		||||
@@ -99,7 +99,12 @@ err() {
 | 
			
		||||
 | 
			
		||||
    # Exit
 | 
			
		||||
    if [ "$errexit" ]; then
 | 
			
		||||
        log "Backup ERROR: Exiting with previous errors"; exit 1
 | 
			
		||||
        # Count errors for backup recap
 | 
			
		||||
        __errors_count="$(cat $__errors_file)"
 | 
			
		||||
        ((__errors_count++)) || true
 | 
			
		||||
        echo $__errors_count > "$__errors_file"
 | 
			
		||||
 | 
			
		||||
        log -p "Backup ERROR: Exiting with previous errors" >&2; exit 1
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -111,6 +116,7 @@ try() {
 | 
			
		||||
    if eval "$@" 2>> "$__log_file"; then
 | 
			
		||||
        return 0 # Success
 | 
			
		||||
    else
 | 
			
		||||
        # Count errors
 | 
			
		||||
        err -a "Error: Something went wrong when executing command:"
 | 
			
		||||
        err -a "    $*"
 | 
			
		||||
        err -aeo "Check $__log_file log for details."
 | 
			
		||||
@@ -139,3 +145,29 @@ is_function_set() {
 | 
			
		||||
    declare -F -- "$1" > /dev/null
 | 
			
		||||
    return "$?"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
set_compression() {
 | 
			
		||||
    # Set compr_util and file_ext by name.
 | 
			
		||||
 | 
			
		||||
    # Select compression utility and set filename extension.
 | 
			
		||||
    # Refference: https://www.gnu.org/software/tar/manual/html_node/gzip.html
 | 
			
		||||
    case "$1" in
 | 
			
		||||
        gzip|gz)    file_ext='.gz';      compr_util='gzip';;
 | 
			
		||||
        tgz)        file_ext='.gz';      compr_util='gzip';;
 | 
			
		||||
        taz)        file_ext='.gz';      compr_util='gzip';;
 | 
			
		||||
        compress|Z) file_ext='.Z';       compr_util='compress';;
 | 
			
		||||
        taZ)        file_ext='.Z';       compr_util='compress';;
 | 
			
		||||
        bzip2|bz2)  file_ext='.bz2';     compr_util='bzip2';;
 | 
			
		||||
        tz2)        file_ext='.bz2';     compr_util='bzip2';;
 | 
			
		||||
        tbz2)       file_ext='.bz2';     compr_util='bzip2';;
 | 
			
		||||
        tbz)        file_ext='.bz2';     compr_util='bzip2';;
 | 
			
		||||
        lzip|lz)    file_ext='.lz';      compr_util='lzip';;
 | 
			
		||||
        lzma)       file_ext='.lzma';    compr_util='lzma';;
 | 
			
		||||
        tlz)        file_ext='.lzma';    compr_util='lzma';;
 | 
			
		||||
        lzop|lzo)   file_ext='.lzop';    compr_util='lzop';;
 | 
			
		||||
        xz)         file_ext='.xz';      compr_util='xz';;
 | 
			
		||||
        zstd|zst)   file_ext='.zst';     compr_util='zstd';;
 | 
			
		||||
        tzst)       file_ext='.zst';     compr_util='zstd';;
 | 
			
		||||
        *)          file_ext='';        compr_util='';;  # No compression
 | 
			
		||||
    esac
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user