Various improvements
This commit is contained in:
		@@ -1,6 +1,6 @@
 | 
			
		||||
#!/usr/bin/env bash
 | 
			
		||||
 | 
			
		||||
# boring-backup -- backup files and databases.
 | 
			
		||||
# boring_backup -- backup files and databases.
 | 
			
		||||
# Copyright (c) 2022 ge <https://nixhacks.net/>
 | 
			
		||||
#
 | 
			
		||||
# This program is free software: you can redistribute it and/or modify
 | 
			
		||||
@@ -16,12 +16,7 @@
 | 
			
		||||
# You should have received a copy of the GNU General Public License
 | 
			
		||||
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
			
		||||
 | 
			
		||||
__version='0.1.0'
 | 
			
		||||
__config=
 | 
			
		||||
__verbose=
 | 
			
		||||
__log_file='./log.txt'
 | 
			
		||||
__pid_file='/tmp/boring-backup.pid'
 | 
			
		||||
 | 
			
		||||
VERSION='0.1.0'
 | 
			
		||||
LIBRARY="${LIBRARY:-./lib}"
 | 
			
		||||
 | 
			
		||||
# Source library
 | 
			
		||||
@@ -30,24 +25,47 @@ 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() {
 | 
			
		||||
    cat <<- EOF
 | 
			
		||||
Backup files and databases.
 | 
			
		||||
    printf << EOF \
 | 
			
		||||
"USAGE:
 | 
			
		||||
 | 
			
		||||
Usage: $0 [-cvlphV] FILES..
 | 
			
		||||
    $0 [-hVvs] [-c \033[4mfile\033[0m] [-l \033[4mfile\033[0m] \
 | 
			
		||||
[-p \033[4mfile\033[0m] file ...
 | 
			
		||||
 | 
			
		||||
Options:
 | 
			
		||||
    -c, --config    config file.
 | 
			
		||||
    -v, --verbose   verbose output.
 | 
			
		||||
    -l, --log-file  log file [default: $__log_file]
 | 
			
		||||
    -p, --pid-file  PID file [default: $__pid_file]
 | 
			
		||||
    -h, --help      print this help messagea and exit.
 | 
			
		||||
    -V, --version   print version and exit.
 | 
			
		||||
OPTIONS:
 | 
			
		||||
 | 
			
		||||
Environment:
 | 
			
		||||
    LIBRARY         path to bb library [current: $LIBRARY]
 | 
			
		||||
    -h, --help
 | 
			
		||||
        Print this help message and exit.
 | 
			
		||||
 | 
			
		||||
    -V, --version
 | 
			
		||||
        Print version and exit.
 | 
			
		||||
 | 
			
		||||
    -v, --verbose
 | 
			
		||||
        Enable verbose output.
 | 
			
		||||
 | 
			
		||||
    -c, --config=\033[4mfile\033[0m
 | 
			
		||||
        Path to configuration file. Default: ~/.config/boring_backup
 | 
			
		||||
        Configuration file actually is Bash-script similar to backup
 | 
			
		||||
        script. See boring_backup(1) CONFIGURATION.
 | 
			
		||||
 | 
			
		||||
    -l, --log-file=\033[4mfile\033[0m
 | 
			
		||||
        Log file.
 | 
			
		||||
 | 
			
		||||
    -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -88,33 +106,70 @@ for args in "$@"; do
 | 
			
		||||
    shift
 | 
			
		||||
    case "$args" in
 | 
			
		||||
        --*)
 | 
			
		||||
            set -- "$@" "$args";; # save long options
 | 
			
		||||
            set -- "$@" "$args" # save long options
 | 
			
		||||
            ;;
 | 
			
		||||
        -*)
 | 
			
		||||
            args="$(echo "${args:1}" | grep -o . | xargs -I {} echo -n '-{} ')"
 | 
			
		||||
            args="$(echo "${args:1}" |
 | 
			
		||||
                grep -o . | xargs -I {} echo -n '-{} ')"
 | 
			
		||||
            # shellcheck disable=SC2086
 | 
			
		||||
            set -- "$@" $args;; # 'args' must be unquoted!
 | 
			
		||||
            set -- "$@" $args  # 'args' must be unquoted!
 | 
			
		||||
            ;;
 | 
			
		||||
        *)
 | 
			
		||||
            set -- "$@" "$args";;  # save positional arguments
 | 
			
		||||
            set -- "$@" "$args"  # save positional arguments
 | 
			
		||||
            ;;
 | 
			
		||||
    esac
 | 
			
		||||
done
 | 
			
		||||
 | 
			
		||||
# Final arguments parser
 | 
			
		||||
while (( "$#" )); do
 | 
			
		||||
    case "$1" in
 | 
			
		||||
        -h|--help)
 | 
			
		||||
            print_help
 | 
			
		||||
            exit 0
 | 
			
		||||
            ;;
 | 
			
		||||
        -V|--version)
 | 
			
		||||
            echo "$VERSION"
 | 
			
		||||
            exit 0
 | 
			
		||||
            ;;
 | 
			
		||||
        -v|--verbose)
 | 
			
		||||
            verbose_output=1
 | 
			
		||||
            shift
 | 
			
		||||
            ;;
 | 
			
		||||
        -c|--config|--config=*)
 | 
			
		||||
            optval "$1" "$2"; __config="$val"; shift "$sft";;
 | 
			
		||||
        -v|--verbose) __verbose=1; shift;;
 | 
			
		||||
            optval "$1" "$2"
 | 
			
		||||
            bb_config="$val"
 | 
			
		||||
            shift "$sft"
 | 
			
		||||
            ;;
 | 
			
		||||
        -l|--log-file|--log-file=*)
 | 
			
		||||
            optval "$1" "$2"; __log_file="$val"; shift "$sft";;
 | 
			
		||||
            optval "$1" "$2"
 | 
			
		||||
            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";;
 | 
			
		||||
        -h|--help) print_help; exit 0;;
 | 
			
		||||
        -V|--version) echo "$__version"; exit 0;;
 | 
			
		||||
        -*) echo "Error: Unknown option: $1" >&2; exit 1;;
 | 
			
		||||
        *)  __args+=("$1"); shift;; # Save positional args
 | 
			
		||||
            optval "$1" "$2"
 | 
			
		||||
            pid_file="$val"
 | 
			
		||||
            shift "$sft"
 | 
			
		||||
            ;;
 | 
			
		||||
        -*)
 | 
			
		||||
            echo "Error: Unknown option: $1" >&2
 | 
			
		||||
            exit 1
 | 
			
		||||
            ;;
 | 
			
		||||
        *)
 | 
			
		||||
            __args+=("$1") # Save positional args
 | 
			
		||||
            shift
 | 
			
		||||
            ;;
 | 
			
		||||
    esac
 | 
			
		||||
done
 | 
			
		||||
 | 
			
		||||
# -- Set defaults -- #
 | 
			
		||||
log_file="${log_file:-./log.txt}"
 | 
			
		||||
bb_config="${bb_config:-$HOME/.config/boring_backup}"
 | 
			
		||||
pid_file="${pid_file:-/tmp/boring_backup.pid}"
 | 
			
		||||
 | 
			
		||||
# Exit if library is not loaded
 | 
			
		||||
if ! declare -F -- log > /dev/null 2>&1; then
 | 
			
		||||
    exit 1
 | 
			
		||||
@@ -127,20 +182,20 @@ fi
 | 
			
		||||
log -V "Backup STARTED"
 | 
			
		||||
 | 
			
		||||
# Check PID file
 | 
			
		||||
if [ -e "$__pid_file" ]; then
 | 
			
		||||
if [ -e "$pid_file" ]; then
 | 
			
		||||
    # shellcheck disable=SC2009
 | 
			
		||||
    # shellcheck disable=SC2143
 | 
			
		||||
    if [ -z "$(ps ax -o pid | grep "$(cat "$__pid_file")")" ]; then
 | 
			
		||||
        log -p "Process $(cat "$__pid_file") died." >&2
 | 
			
		||||
        rm "$__pid_file"
 | 
			
		||||
    if [ -z "$(ps ax -o pid | grep "$(cat "$pid_file")")" ]; then
 | 
			
		||||
        log -p "Process $(cat "$pid_file") died." >&2
 | 
			
		||||
        rm "$pid_file"
 | 
			
		||||
    else
 | 
			
		||||
        echo "Process $(cat "$__pid_file") still running." >&2
 | 
			
		||||
        echo "Process $(cat "$pid_file") still running." >&2
 | 
			
		||||
        exit 1
 | 
			
		||||
    fi
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Touch PID file
 | 
			
		||||
echo "$$" > "$__pid_file"
 | 
			
		||||
echo "$$" > "$pid_file"
 | 
			
		||||
 | 
			
		||||
# Scripts counter.
 | 
			
		||||
__count="${#__args[@]}" # count
 | 
			
		||||
@@ -149,9 +204,12 @@ __i=1                   # iterator
 | 
			
		||||
# Startup log.
 | 
			
		||||
date +'Start: %d %b %Y %T %z'
 | 
			
		||||
log -p "Library path: $LIBRARY"
 | 
			
		||||
log -p "Log file: $__log_file"
 | 
			
		||||
log -p "Configuration file:" \
 | 
			
		||||
    "$([ "$__config" ] || echo not specified && echo "$__config")"
 | 
			
		||||
log -p "Log file: $log_file"
 | 
			
		||||
log -p "Configuration file:" "$(\
 | 
			
		||||
    if [ -f "$bb_config" ];
 | 
			
		||||
        then echo "$bb_config";
 | 
			
		||||
        else echo not specified;
 | 
			
		||||
    fi)"
 | 
			
		||||
log -p "Total scripts: $__count"
 | 
			
		||||
log "Scripts to process (${__count}): ${__args[*]}"
 | 
			
		||||
 | 
			
		||||
@@ -171,7 +229,7 @@ for script in "${__args[@]}"; do
 | 
			
		||||
 | 
			
		||||
    # Config can ovewrite script functions and variables
 | 
			
		||||
    # shellcheck source=/dev/null
 | 
			
		||||
    [ -n "$__config" ] && . "$__config"
 | 
			
		||||
    [ -f "$bb_config" ] && . "$bb_config"
 | 
			
		||||
 | 
			
		||||
    # --- Run user script -- #
 | 
			
		||||
 | 
			
		||||
@@ -208,17 +266,10 @@ for script in "${__args[@]}"; do
 | 
			
		||||
 | 
			
		||||
    # Unset user defined variables
 | 
			
		||||
    unset compression
 | 
			
		||||
    unset name_prefix
 | 
			
		||||
    unset name_date_format
 | 
			
		||||
    unset s3cmd_options
 | 
			
		||||
    unset s3cmd_config
 | 
			
		||||
    unset s3_access_key
 | 
			
		||||
    unset s3_secret_key
 | 
			
		||||
    unset s3_region
 | 
			
		||||
    unset s3_host
 | 
			
		||||
    unset s3_host_bucket
 | 
			
		||||
    unset tar_options
 | 
			
		||||
    unset tar_exclude
 | 
			
		||||
    unset name_prefix name_date_format name_prefix name_suffix
 | 
			
		||||
    unset s3cmd_config s3cmd_options
 | 
			
		||||
    unset s3_access_key s3_secret_key s3_region s3_host s3_host_bucket
 | 
			
		||||
    unset tar_options tar_exclude
 | 
			
		||||
    unset mysqldump_options
 | 
			
		||||
    unset pg_dump_options
 | 
			
		||||
done
 | 
			
		||||
@@ -227,4 +278,6 @@ echo -e "\nBackup [Done]"
 | 
			
		||||
log -V "Backup FINISHED"
 | 
			
		||||
 | 
			
		||||
# Remove PID file
 | 
			
		||||
rm "$__pid_file"
 | 
			
		||||
rm "$pid_file"
 | 
			
		||||
 | 
			
		||||
exit "${__exit:-0}"
 | 
			
		||||
@@ -25,7 +25,6 @@ tgt_s3cmd() {
 | 
			
		||||
    log "Run handler ${FUNCNAME[0]}()"
 | 
			
		||||
 | 
			
		||||
    local uri
 | 
			
		||||
    local src_path
 | 
			
		||||
 | 
			
		||||
    uri="$1"
 | 
			
		||||
 | 
			
		||||
@@ -44,7 +43,7 @@ tgt_s3cmd() {
 | 
			
		||||
    # Set s3cmd comand. See s3cmd(1)
 | 
			
		||||
    if [ -n "$s3cmd_config" ]; then
 | 
			
		||||
        # Use configuration file
 | 
			
		||||
        # shellcheck disable=SC2154
 | 
			
		||||
        # shellcheck disable=SC2154,SC2086
 | 
			
		||||
        set -- s3cmd $s3cmd_options --config "$s3cmd_config" \
 | 
			
		||||
            put "${backups[@]}" "$uri"
 | 
			
		||||
    elif [ -n "$s3_access_key" ]    && \
 | 
			
		||||
@@ -54,7 +53,7 @@ tgt_s3cmd() {
 | 
			
		||||
    then
 | 
			
		||||
        s3_region="${s3_region:-na}" # fallback to 'no available' region
 | 
			
		||||
        # Use parameters provided from backup script
 | 
			
		||||
        # shellcheck disable=SC2154
 | 
			
		||||
        # shellcheck disable=SC2154,SC2086
 | 
			
		||||
        set -- s3cmd $s3cmd_options \
 | 
			
		||||
            --access_key="$s3_access_key" \
 | 
			
		||||
            --secret_key="$s3_secret_key" \
 | 
			
		||||
@@ -74,6 +73,7 @@ tgt_s3cmd() {
 | 
			
		||||
        # ^^^ hide secret_key from output ^^^
 | 
			
		||||
 | 
			
		||||
    # Upload backups
 | 
			
		||||
    # shellcheck disable=SC2154
 | 
			
		||||
    if "$@" 2>> "$__log_file"; then
 | 
			
		||||
        : # Success
 | 
			
		||||
    else
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user