From 1e124a6aa8823bd9f191374a45ae5b80bef999d9 Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 15 May 2022 08:12:47 +0300 Subject: [PATCH] feat: Add tar compression selection --- src/bafscript | 6 ++++-- src/lib/backup.sh | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/bafscript b/src/bafscript index 69e875c..c4a2434 100755 --- a/src/bafscript +++ b/src/bafscript @@ -20,7 +20,8 @@ __version='0.0.0' __config= __verbose= __log_file='./log.txt' -__tar_options='-czf' +__tar_options='-acf' +__tar_compression='gzip' __name_date_fmt='_%Y%m%d-%H%M' if [ -n "$BAFLIB" ]; then @@ -121,10 +122,10 @@ 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" + #. "$__config" # TODO Make it safe! echo echo -e "\e[1m==> Script: ${__args[_iter-1]##*/} [$_iter/$_count]\e[0m" | log -p @@ -162,6 +163,7 @@ for script in "${__args[@]}"; do # Unset user defined variables unset tar_options + unset tar_compression unset name_date_fmt done diff --git a/src/lib/backup.sh b/src/lib/backup.sh index a001c7d..97e477a 100644 --- a/src/lib/backup.sh +++ b/src/lib/backup.sh @@ -103,6 +103,7 @@ backup_files() { local dst_path local tar_opts local archive + local compression local file_ext uri="$1" @@ -117,7 +118,8 @@ backup_files() { exit 1 fi - is_installed tar # Exit if tar is not installed + # Exit if tar is not installed + is_installed tar # Overwrire __tar_options if [ -n "$tar_options" ]; then @@ -126,9 +128,35 @@ backup_files() { tar_opts="$__tar_options" fi - # TODO выбор сжатия, можно в переменной __tar_options заменять букву z на - # другую для соответствующего сжатия - file_ext=.tar.gz + # Overwrite __tar_compression + if [ "$tar_compression" ]; then + compression="$tar_compression" + else + compression="$__tar_compression" + fi + + # Select filename extension by compression type. + # Make sure for the `--auto-compress` is enabled in __tar_options + # Refference: https://www.gnu.org/software/tar/manual/html_node/gzip.html + case "$compression" in + gzip|gz) file_ext='.tar.gz';; # gzip + tgz) file_ext='.tgz';; # gzip + taz) file_ext='.taz';; # gzip + compress|Z) file_ext='.tar.Z';; # compress + taZ) file_ext='.taZ';; # compress + bzip2|bz2) file_ext='.tar.bz2';; # bzip2 + tz2) file_ext='.tz2';; # bzip2 + tbz2) file_ext='.tbz2';; # bzip2 + tbz) file_ext='.tbz';; # bzip2 + lzip|lz) file_ext='.tar.lz';; # lzip + lzma) file_ext='.tar.lzma';; # lzma + tlz) file_ext='.tlz';; # lzma + lzop|lzo) file_ext='.tar.lzo';; # lzop + xz) file_ext='.tar.xz';; # xz + zstd|zst) file_ext='.tar.zst';; # zstd + tzst) file_ext='.tzst';; # zstd + *) file_ext='.tar.gz';; # Force gzip + esac archive="${dst_path}/$(gen_backup_name "$file_ext")"