feat: Add logging and output info
This commit is contained in:
parent
c43346aa35
commit
d22604b13a
@ -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]"
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user