feat: Update compression for tar

This commit is contained in:
ge 2022-08-21 18:54:52 +03:00
parent a5e2a00feb
commit d4c7e6c4a1
2 changed files with 37 additions and 57 deletions

View File

@ -118,38 +118,40 @@ remove_if_empty() {
} }
set_compression() { 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. # Select compression utility and set filename extension.
# Refference: https://www.gnu.org/software/tar/manual/html_node/gzip.html # Refference: https://www.gnu.org/software/tar/manual/html_node/gzip.html
case "$1" in case "$1" in
gzip|gz) compr_ext='.gz'; compr_cmd='gzip';; gzip|gz) cmpr_ext='.gz'; tar_ext='.tar.gz'; cmpr_cmd='gzip';;
tgz) compr_ext='.gz'; compr_cmd='gzip';; tgz) cmpr_ext='.gz'; tar_ext='.tgz'; cmpr_cmd='gzip';;
taz) compr_ext='.gz'; compr_cmd='gzip';; taz) cmpr_ext='.gz'; tar_ext='.taz'; cmpr_cmd='gzip';;
compress|Z) compr_ext='.Z'; compr_cmd='compress';; compress|Z) cmpr_ext='.Z'; tar_ext='.tar.Z'; cmpr_cmd='compress';;
taZ) compr_ext='.Z'; compr_cmd='compress';; taZ) cmpr_ext='.Z'; tar_ext='.taZ'; cmpr_cmd='compress';;
bzip2|bz2) compr_ext='.bz2'; compr_cmd='bzip2';; bzip2|bz2) cmpr_ext='.bz2'; tar_ext='.tar.bz2'; cmpr_cmd='bzip2';;
tz2) compr_ext='.bz2'; compr_cmd='bzip2';; tz2) cmpr_ext='.bz2'; tar_ext='.tz2'; cmpr_cmd='bzip2';;
tbz2) compr_ext='.bz2'; compr_cmd='bzip2';; tbz2) cmpr_ext='.bz2'; tar_ext='.tbz2'; cmpr_cmd='bzip2';;
tbz) compr_ext='.bz2'; compr_cmd='bzip2';; tbz) cmpr_ext='.bz2'; tar_ext='.tbz'; cmpr_cmd='bzip2';;
lzip|lz) compr_ext='.lz'; compr_cmd='lzip';; lzip|lz) cmpr_ext='.lz'; tar_ext='.tar.lz'; cmpr_cmd='lzip';;
lzma) compr_ext='.lzma'; compr_cmd='lzma';; lzma) cmpr_ext='.lzma'; tar_ext='.tar.lzma'; cmpr_cmd='lzma';;
tlz) compr_ext='.lzma'; compr_cmd='lzma';; tlz) cmpr_ext='.lzma'; tar_ext='.tlz'; cmpr_cmd='lzma';;
lzop|lzo) compr_ext='.lzop'; compr_cmd='lzop';; lzop|lzo) cmpr_ext='.lzop'; tar_ext='.tar.lzo'; cmpr_cmd='lzop';;
xz) compr_ext='.xz'; compr_cmd='xz';; xz) cmpr_ext='.xz'; tar_ext='.tar.xz'; cmpr_cmd='xz';;
zstd|zst) compr_ext='.zst'; compr_cmd='zstd --rm';; zstd|zst) cmpr_ext='.zst'; tar_ext='.tar.zst'; cmpr_cmd='zstd --rm';;
tzst) compr_ext='.zst'; compr_cmd='zstd --rm';; tzst) cmpr_ext='.zst'; tar_ext='.tzst'; cmpr_cmd='zstd --rm';;
*) # No compression *) # No compression
# shellcheck disable=SC2034 # shellcheck disable=SC2034
compr_ext= cmpr_ext=
# shellcheck disable=SC2034 # shellcheck disable=SC2034
compr_cmd= tar_ext='.tar'
# shellcheck disable=SC2034
cmpr_cmd=
;; ;;
esac 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() { compress_file() {
@ -163,15 +165,15 @@ compress_file() {
if [ -n "$compression" ]; then if [ -n "$compression" ]; then
set_compression "$compression" set_compression "$compression"
else else
compr_cmd= cmpr_cmd=
compr_ext= cmpr_ext=
fi fi
# Compress file # Compress file
if [ -n "$compr_cmd" ]; then if [ -n "$cmpr_cmd" ]; then
log -V "Compressing file $1 by ${compr_cmd%% *} ..." log -V "Compressing file $1 by ${cmpr_cmd%% *} ..."
$compr_cmd "$1" 2>> "$__log_file" $cmpr_cmd "$1" 2>> "$__log_file"
compressed="${1}${compr_ext}" compressed="${1}${cmpr_ext}"
fi fi
if [ -f "$compressed" ]; then if [ -f "$compressed" ]; then

View File

@ -28,7 +28,6 @@ src_tar() {
local dst_path local dst_path
local archive local archive
local exclude local exclude
local file_ext
uri="$1" uri="$1"
# shellcheck disable=SC2154 # shellcheck disable=SC2154
@ -71,34 +70,13 @@ src_tar() {
tar_options="${tar_options:--acf}" tar_options="${tar_options:--acf}"
# Select filename extension by compression type. # Select filename extension by compression type.
if [ "$compression" ]; then if [ -n "$compression" ]; then
# Make sure for the `--auto-compress` is enabled in __tar_options set_compression "$compression"
# 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
else else
file_ext='.tar' tar_ext='.tar'
fi fi
archive="${dst_path}/$(gen_backup_name "$file_ext")" archive="${dst_path}/$(gen_backup_name "$tar_ext")"
log "Archiving $src_path to $archive ..." log "Archiving $src_path to $archive ..."