From d22604b13a9a220b736bf1fc5325c12cbeee1516 Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 15 May 2022 07:19:52 +0300 Subject: [PATCH] feat: Add logging and output info --- src/bafscript | 33 +++++++++++++++++++++++++++++++++ src/lib/backup.sh | 28 +++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/bafscript b/src/bafscript index 4ead433..69e875c 100755 --- a/src/bafscript +++ b/src/bafscript @@ -18,6 +18,7 @@ __version='0.0.0' __config= +__verbose= __log_file='./log.txt' __tar_options='-czf' __name_date_fmt='_%Y%m%d-%H%M' @@ -110,27 +111,59 @@ done # * 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 source_script "$script" + echo + echo -e "\e[1m==> Script: ${__args[_iter-1]##*/} [$_iter/$_count]\e[0m" | log -p + # Initialise variables __user_script="$script" backups=() # Array of created backups, contains full pathes # Run prepare() before all if set if is_function_set prepare; then + log -p "Execute prepare() ..." prepare + unset prepare fi # Run user defined backup() if set or builtin_backup() if is_function_set backup; then + echo -e "Execute \e[1mbackup()\e[0m ..." | log -p backup + unset backup else + echo -e "Execute \e[1mbuiltin_backup()\e[0m ..." | log -p builtin_backup fi # Run post backup function if set if is_function_set finalise; then + echo -e "Execute \e[1mfinalise()\e[0m ..." | log -p finalise + unset finalise fi + + # Increase counter + ((_iter++)) || true + + # Unset user defined variables + unset tar_options + unset name_date_fmt done + +log "Backup FINISHED" +echo -e "\nBackup [Done]" diff --git a/src/lib/backup.sh b/src/lib/backup.sh index a146d2a..a001c7d 100644 --- a/src/lib/backup.sh +++ b/src/lib/backup.sh @@ -28,6 +28,8 @@ process_source() { uri="$1" scheme="${uri%%:*}" + echo -e "Processing source $uri ..." + case "$scheme" in file) handler='backup_files';; mysql) handler='backup_mysql';; @@ -37,6 +39,7 @@ process_source() { esac # Run handler function + [ "$__verbose" ] && echo "Run handler ${handler}()" "$handler" "$uri" } @@ -52,6 +55,8 @@ process_target() { uri="$1" scheme="${uri%%:*}" + echo -e "Processing target $uri ..." + case "$scheme" in file) handler='transfer_files';; ftp) handler='transfer_ftp';; @@ -66,6 +71,7 @@ process_target() { esac # Run handler function + [ "$__verbose" ] && echo "Run handler ${handler}()" "$handler" "$uri" } @@ -95,6 +101,7 @@ backup_files() { local uri local src_path local dst_path + local tar_opts local archive local file_ext @@ -114,7 +121,9 @@ backup_files() { # Overwrire __tar_options if [ -n "$tar_options" ]; then - __tar_options="$tar_options" + tar_opts="$tar_options" + else + tar_opts="$__tar_options" fi # TODO выбор сжатия, можно в переменной __tar_options заменять букву z на @@ -123,7 +132,16 @@ backup_files() { 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 backups+=("$archive") @@ -168,9 +186,13 @@ transfer_files() { exit 1 fi + [ "$__verbose" ] && echo "Destination path: $dst_path" + # Copy files preserving metadata 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 fi }