feat: Move handler functions to separate scripts

This commit is contained in:
ge 2022-05-19 08:38:23 +03:00
parent 3b81668b2b
commit 9ed6528911
3 changed files with 180 additions and 214 deletions

View File

@ -33,10 +33,10 @@ process_source() {
echo -e "Processing source $uri ..." echo -e "Processing source $uri ..."
case "$scheme" in case "$scheme" in
file) handler='backup_files';; file) handler='handler::tar';;
mysql) handler='backup_mysql';; mysql) handler='handler::mysqldump';;
postgres) handler='backup_postgres';; postgres) handler='handler::pg_dump';;
sqlite) handler='backup_sqlite';; sqlite) handler='handler::sqlite';;
*) echo "Error: $__user_script: Unsupported URI scheme: $scheme" >&2; exit 1;; *) echo "Error: $__user_script: Unsupported URI scheme: $scheme" >&2; exit 1;;
esac esac
@ -62,15 +62,15 @@ process_target() {
echo -e "Processing target $uri ..." echo -e "Processing target $uri ..."
case "$scheme" in case "$scheme" in
file) handler='transfer_files';; file) handler='handler::cp';;
ftp) handler='transfer_ftp';; ftp) handler='handler::ftp';;
sftp) handler='transfer_sftp';; sftp) handler='handler::sftp';;
rsync) handler='transfer_rsync';; rsync) handler='handler::rsync';;
s3) handler='transfer_s3';; s3) handler='handler::s3';;
sj) handler='transfer_sj';; sj) handler='handler::sj';;
swift) handler='transfer_swift';; swift) handler='handler::swift';;
dav) handler='transfer_dav';; dav) handler='handler::dav';;
davs) handler='transfer_davs';; davs) handler='handler::davs';;
*) echo "Error: Unsupported URI scheme: $scheme" >&2; exit 1;; *) echo "Error: Unsupported URI scheme: $scheme" >&2; exit 1;;
esac esac
@ -93,211 +93,10 @@ builtin_backup() {
done done
} }
# ---------------------------------------------------------- #
# * Backup functions #
# ---------------------------------------------------------- #
backup_files() {
# Backup local files with tar(1). Handle 'file' URI scheme.
#
# Usage: backup_files URI
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")
}
backup_mysql() {
echo Not implemented >&2; exit 1
}
backup_postgres() {
echo Not implemented >&2; exit 1
}
backup_sqlite() {
echo Not implemented >&2; exit 1
}
# ---------------------------------------------------------- #
# * Functions for targets processing #
# ---------------------------------------------------------- #
transfer_files() {
# Transfer files to another location from __main_target_path using cp(1)
#
# Usage: transfer_files URI
local uri
local dst_path
uri="$1"
if [[ "$uri" == "$__main_target" ]]; then
: # Do nothing. Source and destination is the same
else
# Copy backups to another destination
parse_uri "$uri"
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
for backup in "${backups[@]}"; do
log "Copying file $backup to $dst_path ..."
[ "$__verbose" ] && echo "Command: cp --archive $backup $dst_path"
try cp --archive "$backup" "$dst_path"
done
fi
}
transfer_ftp() {
echo Not implemented >&2; exit 1
}
transfer_sftp() {
echo Not implemented >&2; exit 1
}
transfer_rsync() {
echo Not implemented >&2; exit 1
}
transfer_s3() {
echo Not implemented >&2; exit 1
}
transfer_sj() {
echo Not implemented >&2; exit 1
}
transfer_swift() {
echo Not implemented >&2; exit 1
}
transfer_dav() {
echo Not implemented >&2; exit 1
}
transfer_davs() {
echo Not implemented >&2; exit 1
}
# ---------------------------------------------------------- # # ---------------------------------------------------------- #
# * Helper functions # # * Helper functions #
# ---------------------------------------------------------- # # ---------------------------------------------------------- #
is_installed() {
# Check if the program is installed.
# See good answer: https://stackoverflow.com/a/677212
#
# Usage: is_installed COMMAND
local cmd
cmd="$1"
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: Command $cmd not found." \
"Please install $cmd or check your PATH if it's actually installed." >&2
exit 1
fi
}
gen_backup_name() { gen_backup_name() {
# Generate backup file name. Return (echo) string. # Generate backup file name. Return (echo) string.
# #

52
src/lib/handlers/cp.sh Normal file
View File

@ -0,0 +1,52 @@
#!/usr/bin/env bash
# cp.sh - copy files to new location.
# Copyright (c) 2022 ge <https://nixhacks.net/>
#
# 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 <https://www.gnu.org/licenses/>.
handler::cp() {
# Transfer files to another location from __main_target_path using cp(1)
# Handle 'file' URI scheme.
#
# Usage: handler::cp URI
local uri
local dst_path
uri="$1"
if [[ "$uri" == "$__main_target" ]]; then
: # Do nothing. Source and destination is the same
else
# Copy backups to another destination
parse_uri "$uri"
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
for backup in "${backups[@]}"; do
log "Copying file $backup to $dst_path ..."
[ "$__verbose" ] && echo "Command: cp --archive $backup $dst_path"
try cp --archive "$backup" "$dst_path"
done
fi
}

115
src/lib/handlers/tar.sh Normal file
View File

@ -0,0 +1,115 @@
#!/usr/bin/env bash
# tar.sh - backup files via tar.
# Copyright (c) 2022 ge <https://nixhacks.net/>
#
# 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 <https://www.gnu.org/licenses/>.
handler::tar() {
# Backup local files with tar(1). Handle 'file' URI scheme.
#
# Usage: handler::tar URI
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")
}