feat: Improve code
This commit is contained in:
parent
4b39560d67
commit
1b2ce90019
@ -16,12 +16,12 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
handler::tar() {
|
src_tar() {
|
||||||
# Backup local files with tar(1). Handle 'file' URI scheme.
|
# 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 uri
|
||||||
local src_path
|
local src_path
|
||||||
@ -36,6 +36,11 @@ handler::tar() {
|
|||||||
|
|
||||||
parse_uri "$uri"
|
parse_uri "$uri"
|
||||||
|
|
||||||
|
# shellcheck disable=SC2154
|
||||||
|
if [[ "$scheme" != 'file' ]]; then
|
||||||
|
log -p "Error: Wrong URI scheme: $scheme" >&2; exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# shellcheck disable=SC2154
|
# shellcheck disable=SC2154
|
||||||
if [ -f "$path" ] || [ -d "$path" ]; then
|
if [ -f "$path" ] || [ -d "$path" ]; then
|
||||||
src_path="$path"
|
src_path="$path"
|
||||||
@ -95,18 +100,22 @@ handler::tar() {
|
|||||||
|
|
||||||
archive="${dst_path}/$(gen_backup_name "$file_ext")"
|
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 "Archiving $src_path to $archive ..."
|
||||||
log "Run command: tar $exclude $tar_options $archive $src_path"
|
|
||||||
|
|
||||||
# Run tar
|
# 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
|
log "Run command: $*"
|
||||||
backups+=("$archive")
|
|
||||||
|
# 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
|
||||||
}
|
}
|
||||||
|
@ -16,13 +16,13 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
handler::cp() {
|
tgt_cp() {
|
||||||
# Transfer files to another location from __main_target_path using cp(1)
|
# Transfer files to another location from __main_target_path using cp(1)
|
||||||
# Handle 'file' URI scheme.
|
# 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 uri
|
||||||
local dst_path
|
local dst_path
|
||||||
@ -32,30 +32,40 @@ handler::cp() {
|
|||||||
# shellcheck disable=SC2154
|
# shellcheck disable=SC2154
|
||||||
if [[ "$uri" == "$__main_target" ]]; then
|
if [[ "$uri" == "$__main_target" ]]; then
|
||||||
: # Do nothing. Source and destination is the same
|
: # Do nothing. Source and destination is the same
|
||||||
log "Nothing to do: Source and destination is the same: $__main_target"
|
log "Nothing to do:"\
|
||||||
else
|
"Source and destination is the same: $__main_target"
|
||||||
# Copy backups to another destination
|
return
|
||||||
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
|
|
||||||
fi
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user