From d4c7e6c4a11efbb0a5915108fa44e8dc97aad297 Mon Sep 17 00:00:00 2001 From: ge Date: Sun, 21 Aug 2022 18:54:52 +0300 Subject: [PATCH] feat: Update compression for tar --- src/lib/common.sh | 64 +++++++++++++++++---------------- src/lib/handlers/sources/tar.sh | 30 +++------------- 2 files changed, 37 insertions(+), 57 deletions(-) diff --git a/src/lib/common.sh b/src/lib/common.sh index 014acfb..f1dcb73 100644 --- a/src/lib/common.sh +++ b/src/lib/common.sh @@ -118,38 +118,40 @@ remove_if_empty() { } set_compression() { - # Set `compr_cmd` and `compr_ext` by name. + # Set `cmpr_cmd`, `cmpr_ext` and `tar_ext` by `$1` value. # Select compression utility and set filename extension. # Refference: https://www.gnu.org/software/tar/manual/html_node/gzip.html case "$1" in - gzip|gz) compr_ext='.gz'; compr_cmd='gzip';; - tgz) compr_ext='.gz'; compr_cmd='gzip';; - taz) compr_ext='.gz'; compr_cmd='gzip';; - compress|Z) compr_ext='.Z'; compr_cmd='compress';; - taZ) compr_ext='.Z'; compr_cmd='compress';; - bzip2|bz2) compr_ext='.bz2'; compr_cmd='bzip2';; - tz2) compr_ext='.bz2'; compr_cmd='bzip2';; - tbz2) compr_ext='.bz2'; compr_cmd='bzip2';; - tbz) compr_ext='.bz2'; compr_cmd='bzip2';; - lzip|lz) compr_ext='.lz'; compr_cmd='lzip';; - lzma) compr_ext='.lzma'; compr_cmd='lzma';; - tlz) compr_ext='.lzma'; compr_cmd='lzma';; - lzop|lzo) compr_ext='.lzop'; compr_cmd='lzop';; - xz) compr_ext='.xz'; compr_cmd='xz';; - zstd|zst) compr_ext='.zst'; compr_cmd='zstd --rm';; - tzst) compr_ext='.zst'; compr_cmd='zstd --rm';; - *) # No compression - # shellcheck disable=SC2034 - compr_ext= - # shellcheck disable=SC2034 - compr_cmd= - ;; + gzip|gz) cmpr_ext='.gz'; tar_ext='.tar.gz'; cmpr_cmd='gzip';; + tgz) cmpr_ext='.gz'; tar_ext='.tgz'; cmpr_cmd='gzip';; + taz) cmpr_ext='.gz'; tar_ext='.taz'; cmpr_cmd='gzip';; + compress|Z) cmpr_ext='.Z'; tar_ext='.tar.Z'; cmpr_cmd='compress';; + taZ) cmpr_ext='.Z'; tar_ext='.taZ'; cmpr_cmd='compress';; + bzip2|bz2) cmpr_ext='.bz2'; tar_ext='.tar.bz2'; cmpr_cmd='bzip2';; + tz2) cmpr_ext='.bz2'; tar_ext='.tz2'; cmpr_cmd='bzip2';; + tbz2) cmpr_ext='.bz2'; tar_ext='.tbz2'; cmpr_cmd='bzip2';; + tbz) cmpr_ext='.bz2'; tar_ext='.tbz'; cmpr_cmd='bzip2';; + lzip|lz) cmpr_ext='.lz'; tar_ext='.tar.lz'; cmpr_cmd='lzip';; + lzma) cmpr_ext='.lzma'; tar_ext='.tar.lzma'; cmpr_cmd='lzma';; + tlz) cmpr_ext='.lzma'; tar_ext='.tlz'; cmpr_cmd='lzma';; + lzop|lzo) cmpr_ext='.lzop'; tar_ext='.tar.lzo'; cmpr_cmd='lzop';; + xz) cmpr_ext='.xz'; tar_ext='.tar.xz'; cmpr_cmd='xz';; + zstd|zst) cmpr_ext='.zst'; tar_ext='.tar.zst'; cmpr_cmd='zstd --rm';; + tzst) cmpr_ext='.zst'; tar_ext='.tzst'; cmpr_cmd='zstd --rm';; + *) # No compression + # shellcheck disable=SC2034 + cmpr_ext= + # shellcheck disable=SC2034 + tar_ext='.tar' + # shellcheck disable=SC2034 + cmpr_cmd= + ;; esac - [ -z "$compr_cmd" ] && return # Exit from function if no compression set + [ -z "$cmpr_cmd" ] && return # Exit from function if no compression set - is_installed "${compr_cmd%% *}" + is_installed "${cmpr_cmd%% *}" } compress_file() { @@ -163,15 +165,15 @@ compress_file() { if [ -n "$compression" ]; then set_compression "$compression" else - compr_cmd= - compr_ext= + cmpr_cmd= + cmpr_ext= fi # Compress file - if [ -n "$compr_cmd" ]; then - log -V "Compressing file $1 by ${compr_cmd%% *} ..." - $compr_cmd "$1" 2>> "$__log_file" - compressed="${1}${compr_ext}" + if [ -n "$cmpr_cmd" ]; then + log -V "Compressing file $1 by ${cmpr_cmd%% *} ..." + $cmpr_cmd "$1" 2>> "$__log_file" + compressed="${1}${cmpr_ext}" fi if [ -f "$compressed" ]; then diff --git a/src/lib/handlers/sources/tar.sh b/src/lib/handlers/sources/tar.sh index 46d100e..8c126e1 100644 --- a/src/lib/handlers/sources/tar.sh +++ b/src/lib/handlers/sources/tar.sh @@ -28,7 +28,6 @@ src_tar() { local dst_path local archive local exclude - local file_ext uri="$1" # shellcheck disable=SC2154 @@ -71,34 +70,13 @@ src_tar() { tar_options="${tar_options:--acf}" # Select filename extension by compression type. - if [ "$compression" ]; then - # 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';; # No compression - esac + if [ -n "$compression" ]; then + set_compression "$compression" else - file_ext='.tar' + tar_ext='.tar' fi - archive="${dst_path}/$(gen_backup_name "$file_ext")" + archive="${dst_path}/$(gen_backup_name "$tar_ext")" log "Archiving $src_path to $archive ..."