feat: Add logging and output info

This commit is contained in:
ge 2022-05-15 07:19:52 +03:00
parent c43346aa35
commit d22604b13a
2 changed files with 58 additions and 3 deletions

View File

@ -18,6 +18,7 @@
__version='0.0.0' __version='0.0.0'
__config= __config=
__verbose=
__log_file='./log.txt' __log_file='./log.txt'
__tar_options='-czf' __tar_options='-czf'
__name_date_fmt='_%Y%m%d-%H%M' __name_date_fmt='_%Y%m%d-%H%M'
@ -110,27 +111,59 @@ done
# * Do backups # # * Do backups #
# ---------------------------------------------------------- # # ---------------------------------------------------------- #
# Scripts counter.
_count=${#__args[@]} # count
_iter=1 # iterator
# Startup log.
date +'Start: %d %b %Y %T %z'
log "Backup STARTED"
log -p "Configuration file: $([ "$__config" ] || echo not specified && echo "$__config")"
log "Scripts to process (${_count}): ${__args[@]}"
# TODO source config
for script in "${__args[@]}"; do for script in "${__args[@]}"; do
source_script "$script" source_script "$script"
echo
echo -e "\e[1m==> Script: ${__args[_iter-1]##*/} [$_iter/$_count]\e[0m" | log -p
# Initialise variables # Initialise variables
__user_script="$script" __user_script="$script"
backups=() # Array of created backups, contains full pathes backups=() # Array of created backups, contains full pathes
# Run prepare() before all if set # Run prepare() before all if set
if is_function_set prepare; then if is_function_set prepare; then
log -p "Execute prepare() ..."
prepare prepare
unset prepare
fi fi
# Run user defined backup() if set or builtin_backup() # Run user defined backup() if set or builtin_backup()
if is_function_set backup; then if is_function_set backup; then
echo -e "Execute \e[1mbackup()\e[0m ..." | log -p
backup backup
unset backup
else else
echo -e "Execute \e[1mbuiltin_backup()\e[0m ..." | log -p
builtin_backup builtin_backup
fi fi
# Run post backup function if set # Run post backup function if set
if is_function_set finalise; then if is_function_set finalise; then
echo -e "Execute \e[1mfinalise()\e[0m ..." | log -p
finalise finalise
unset finalise
fi fi
# Increase counter
((_iter++)) || true
# Unset user defined variables
unset tar_options
unset name_date_fmt
done done
log "Backup FINISHED"
echo -e "\nBackup [Done]"

View File

@ -28,6 +28,8 @@ process_source() {
uri="$1" uri="$1"
scheme="${uri%%:*}" scheme="${uri%%:*}"
echo -e "Processing source $uri ..."
case "$scheme" in case "$scheme" in
file) handler='backup_files';; file) handler='backup_files';;
mysql) handler='backup_mysql';; mysql) handler='backup_mysql';;
@ -37,6 +39,7 @@ process_source() {
esac esac
# Run handler function # Run handler function
[ "$__verbose" ] && echo "Run handler ${handler}()"
"$handler" "$uri" "$handler" "$uri"
} }
@ -52,6 +55,8 @@ process_target() {
uri="$1" uri="$1"
scheme="${uri%%:*}" scheme="${uri%%:*}"
echo -e "Processing target $uri ..."
case "$scheme" in case "$scheme" in
file) handler='transfer_files';; file) handler='transfer_files';;
ftp) handler='transfer_ftp';; ftp) handler='transfer_ftp';;
@ -66,6 +71,7 @@ process_target() {
esac esac
# Run handler function # Run handler function
[ "$__verbose" ] && echo "Run handler ${handler}()"
"$handler" "$uri" "$handler" "$uri"
} }
@ -95,6 +101,7 @@ backup_files() {
local uri local uri
local src_path local src_path
local dst_path local dst_path
local tar_opts
local archive local archive
local file_ext local file_ext
@ -114,7 +121,9 @@ backup_files() {
# Overwrire __tar_options # Overwrire __tar_options
if [ -n "$tar_options" ]; then if [ -n "$tar_options" ]; then
__tar_options="$tar_options" tar_opts="$tar_options"
else
tar_opts="$__tar_options"
fi fi
# TODO выбор сжатия, можно в переменной __tar_options заменять букву z на # TODO выбор сжатия, можно в переменной __tar_options заменять букву z на
@ -123,7 +132,16 @@ backup_files() {
archive="${dst_path}/$(gen_backup_name "$file_ext")" archive="${dst_path}/$(gen_backup_name "$file_ext")"
tar "$__tar_options" "$archive" "$src_path" |& log -p [ "$__verbose" ] && {
echo "Source path: $src_path"
echo "Destination path: $dst_path"
echo "Command: tar $tar_opts $archive $src_path"
}
log "Archiving $src_path to $archive ..."
# Run tar
try tar "$tar_opts" "$archive" "$src_path"
# Append path to 'backups' array # Append path to 'backups' array
backups+=("$archive") backups+=("$archive")
@ -168,9 +186,13 @@ transfer_files() {
exit 1 exit 1
fi fi
[ "$__verbose" ] && echo "Destination path: $dst_path"
# Copy files preserving metadata # Copy files preserving metadata
for backup in "${backups[@]}"; do for backup in "${backups[@]}"; do
cp --archive "$backup" "$dst_path" log "Copying file $backup to $dst_path ..."
[ "$__verbose" ] && echo "Command: cp --archive $backup $dst_path"
try cp --archive "$backup" "$dst_path"
done done
fi fi
} }