#!/usr/bin/env bash
# tar.sh - backup files via tar.
# Copyright (c) 2022 ge
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
handler::tar() {
# Backup local files with tar(1). Handle 'file' URI scheme.
#
# Usage: handler::tar URI
log "Run handler handler::tar() ..."
local uri
local src_path
local dst_path
local opts
local archive
local compr
local exclude
local file_ext
uri="$1"
dst_path="$__main_target_path"
parse_uri "$uri"
if [ -f "$path" ] || [ -d "$path" ]; then
src_path="$path"
else
echo "Error: Path '$path' from URI '$uri' does not exists" >&2
exit 1
fi
if [[ "$src_path" == "$dst_path" ]]; then
echo "Error: Source and destination paths is the same: $src_path; $dst_path" >&2
exit 1
fi
# Exit if tar is not installed
is_installed tar
# Overwrire __tar_options
if [ -n "$tar_options" ]; then
opts="$tar_options"
else
opts="$__tar_options"
fi
# Overwrite __tar_exclude
if [ "$tar_exclude" ]; then
for item in "${tar_exclude[@]}"; do
exclude+=" --exclude $item"
done
else
exclude=
fi
# Overwrite __compression
if [ "$compression" ]; then
compr="$compression"
else
compr="$__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 "$compr" 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")"
[ "$__verbose" ] && {
echo "Source path: $src_path"
echo "Destination path: $dst_path"
echo "Command: tar $exclude $opts $archive $src_path"
}
log "Archiving $src_path to $archive ..."
log "Command: tar $exclude $opts $archive $src_path"
# Run tar
try tar "$exclude" "$opts" "$archive" "$src_path"
# Append path to 'backups' array
backups+=("$archive")
}