feat: Minor changes

This commit is contained in:
ge 2022-08-21 04:45:56 +03:00
parent 4832a3a751
commit 0e2f95337d

View File

@ -21,10 +21,6 @@ __config=
__verbose= __verbose=
__log_file='./log.txt' __log_file='./log.txt'
__pid_file='/tmp/boring-backup.pid' __pid_file='/tmp/boring-backup.pid'
__errors_count=0
__errors_file='/tmp/boring-backup.errors'
echo "$__errors_count" > "$__errors_file"
LIBRARY="${LIBRARY:-./lib}" LIBRARY="${LIBRARY:-./lib}"
@ -34,7 +30,6 @@ if [ -f "$LIBRARY/lib.sh" ]; then
. "$LIBRARY/lib.sh" . "$LIBRARY/lib.sh"
else else
echo "Error: Cannot source library $LIBRARY/lib.sh: No such file" >&2 echo "Error: Cannot source library $LIBRARY/lib.sh: No such file" >&2
exit 1
fi fi
print_help() { print_help() {
@ -120,21 +115,27 @@ while (( "$#" )); do
esac esac
done done
# Exit if library is not loaded
if ! declare -F -- log > /dev/null 2>&1; then
exit 1
fi
# ---------------------------------------------------------- # # ---------------------------------------------------------- #
# * Do backups # # * Do backups #
# ---------------------------------------------------------- # # ---------------------------------------------------------- #
log "Backup STARTED" log -V "Backup STARTED"
# Check PID file # Check PID file
if [ -e "$__pid_file" ]; then if [ -e "$__pid_file" ]; then
# shellcheck disable=SC2009 # shellcheck disable=SC2009
# shellcheck disable=SC2143 # shellcheck disable=SC2143
if [ -z "$(ps ax -o pid | grep "$(cat "$__pid_file")")" ]; then 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" rm "$__pid_file"
else else
err -e "Process $(cat "$__pid_file") still running."; echo "Process $(cat "$__pid_file") still running." >&2
exit 1
fi fi
fi fi
@ -149,29 +150,30 @@ __i=1 # iterator
date +'Start: %d %b %Y %T %z' date +'Start: %d %b %Y %T %z'
log -p "Library path: $LIBRARY" log -p "Library path: $LIBRARY"
log -p "Log file: $__log_file" log -p "Log file: $__log_file"
log -p "Configuration file: $(if [ "$__config" ]; then \ log -p "Configuration file:" \
echo not specified; else echo "$__config"; fi)" "$([ "$__config" ] || echo not specified && echo "$__config")"
log -p "Total scripts: $__count" log -p "Total scripts: $__count"
log "Scripts to process (${__count}): ${__args[*]}" log "Scripts to process (${__count}): ${__args[*]}"
for script in "${__args[@]}"; do for script in "${__args[@]}"; do
# Initialise variables echo # Just print new line
__user_script="$script"
# Initialise variables
# shellcheck disable=SC2034
backup_script="$script"
# shellcheck disable=SC2034 # shellcheck disable=SC2034
backups=() # Array of created backups, contains full pathes 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" source_script "$script"
# Config can ovewrite script functions and variables # Config can ovewrite script functions and variables
# shellcheck source=/dev/null # shellcheck source=/dev/null
[ -n "$__config" ] && . "$__config" [ -n "$__config" ] && . "$__config"
echo -e "\n\e[1m--> Script: ${__args[__i-1]##*/} [$__i/$__count]\e[0m" | # --- Run user script -- #
log -p
# Run prepare() before all if set # Run prepare() before all if set
if is_function_set prepare; then if is_function_set prepare; then
@ -204,29 +206,24 @@ for script in "${__args[@]}"; do
unset on_error unset on_error
# Unset user defined variables # Unset user defined variables
unset backups
unset compression unset compression
#unset name_prefix unset name_prefix
unset name_date_fmt unset name_date_format
#unset s3_access_key unset s3_access_key
#unset s3_secret_key unset s3_secret_key
#unset s3_access_token unset s3_access_token
#unset s3_endpoint unset s3_endpoint
#unset s3_region unset s3_region
#unset s3cmd_config unset s3cmd_config
unset tar_options unset tar_options
unset tar_exclude unset tar_exclude
unset mysqldump_options
unset pg_dump_options
done done
if [[ "$(cat $__errors_file)" != '0' ]]; then echo -e "\nBackup [Done]"
echo log -V "Backup FINISHED"
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"
# Remove PID file # Remove PID file
rm "$__pid_file" rm "$__pid_file"