diff --git a/src/lib/handlers/sources/tar.sh b/src/lib/handlers/sources/tar.sh index 6414fd0..46d100e 100644 --- a/src/lib/handlers/sources/tar.sh +++ b/src/lib/handlers/sources/tar.sh @@ -16,12 +16,12 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -handler::tar() { +src_tar() { # Backup local files with tar(1). Handle 'file' URI scheme. # - # Usage: handler::tar URI + # Usage: src::tar URI - log "Run handler handler::tar()" + log "Run handler ${FUNCNAME[0]}()" local uri local src_path @@ -36,6 +36,11 @@ handler::tar() { parse_uri "$uri" + # shellcheck disable=SC2154 + if [[ "$scheme" != 'file' ]]; then + log -p "Error: Wrong URI scheme: $scheme" >&2; exit 1 + fi + # shellcheck disable=SC2154 if [ -f "$path" ] || [ -d "$path" ]; then src_path="$path" @@ -95,18 +100,22 @@ handler::tar() { archive="${dst_path}/$(gen_backup_name "$file_ext")" - [ "$__verbose" ] && { - echo "Source path: $src_path" - echo "Destination path: $dst_path" - echo "Run command: tar $exclude $tar_options $archive $src_path" - } - log "Archiving $src_path to $archive ..." - log "Run command: tar $exclude $tar_options $archive $src_path" # Run tar - try tar "$exclude" "$tar_options" "$archive" "$src_path" + # NOTE! `exclude` and `tar_options` variables must be unquoted! + # shellcheck disable=SC2086 + set -- tar $exclude $tar_options "$archive" "$src_path" - # Append path to 'backups' array - backups+=("$archive") + log "Run command: $*" + + # shellcheck disable=SC2154 + if "$@" 2>> "$__log_file"; then + # Append path to 'backups' array + backups+=("$archive") + else + remove_if_empty "$archive" + handle_error \ + "Error: Something went wrong when executing command:\n\t$*" + fi } diff --git a/src/lib/handlers/targets/cp.sh b/src/lib/handlers/targets/cp.sh index 6e65902..26914d2 100644 --- a/src/lib/handlers/targets/cp.sh +++ b/src/lib/handlers/targets/cp.sh @@ -16,13 +16,13 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -handler::cp() { +tgt_cp() { # Transfer files to another location from __main_target_path using cp(1) # Handle 'file' URI scheme. # - # Usage: handler::cp URI + # Usage: tgt_cp URI - log "Run handler handler::cp()" + log "Run handler ${FUNCNAME[0]}()" local uri local dst_path @@ -32,30 +32,40 @@ handler::cp() { # shellcheck disable=SC2154 if [[ "$uri" == "$__main_target" ]]; then : # Do nothing. Source and destination is the same - log "Nothing to do: Source and destination is the same: $__main_target" - else - # Copy backups to another destination - parse_uri "$uri" - - # shellcheck disable=SC2154 - if [ -f "$path" ] || [ -d "$path" ]; then - dst_path="$path" - else - echo "Error: Path '$path' from URI '$uri' does not exists" >&2 - exit 1 - fi - - [ "$__verbose" ] && echo "Destination path: $dst_path" - - # Copy files preserving metadata - # shellcheck disable=SC2154 - for backup in "${backups[@]}"; do - log "Copying file $backup to $dst_path ..." - log "Run command: cp --archive $backup $dst_path" - if [ "$__verbose" ]; then - echo "Run command: cp --archive $backup $dst_path" - fi - try cp --archive "$backup" "$dst_path" - done + log "Nothing to do:"\ + "Source and destination is the same: $__main_target" + return fi + + # Copy backups to another destination + parse_uri "$uri" + + # shellcheck disable=SC2154 + if [[ "$scheme" != 'file' ]]; then + log -p "Error: Wrong URI scheme: $scheme" >&2; exit 1 + fi + + # shellcheck disable=SC2154 + if [ -f "$path" ] || [ -d "$path" ]; then + dst_path="$path" + else + echo "Error: Path '$path' from URI '$uri' does not exists" >&2 + exit 1 + fi + + # Copy files preserving metadata + # shellcheck disable=SC2154 + for backup in "${backups[@]}"; do + log "Copying file $backup to $dst_path ..." + set -- cp --archive "$backup" "$dst_path" + + log "Run command: $*" + + if "$@" 2>> "$log"; then + : # Success + else + handle_error \ + "Error: Something went wrong when executing command:\n\t$*" + fi + done }