feat: Improve code, add mariadb scheme, fix source issue

This commit is contained in:
ge 2022-08-21 04:49:51 +03:00
parent bafb8b729a
commit e5b244f358

View File

@ -21,22 +21,21 @@ validate_sources() {
# #
# Usage: validate_sources ARRAY # Usage: validate_sources ARRAY
# Allowed URI schemes: file, mysql postgres, sqlite # Allowed URI schemes: file, mysql, mariadb, postgres, sqlite
# No required schemes. # No required schemes.
local array=("$@")
local scheme= local scheme=
for uri in "${array[@]}"; do for uri in "$@"; do
scheme="${uri%%:*}" scheme="${uri%%:*}"
# Process Path-only URI # Process Path-only URI
if [[ "$scheme" == "$uri" ]]; then scheme='file'; fi if [[ "$scheme" == "$uri" ]]; then scheme='file'; fi
case "$scheme" in case "$scheme" in
file|mysql|postgres|sqlite) : ;; # do nothing, this is OK file|mysql|mariadb|postgres|sqlite) : ;; # do nothing, this is OK
*) # shellcheck disable=SC2154 *) # shellcheck disable=SC2154
echo "Error: $__user_script:" \ echo "Error: $backup_script:" \
"Unsupported URI scheme: $scheme" >&2; exit 1;; "Unsupported URI scheme: $scheme" >&2; exit 1;;
esac esac
done done
@ -50,11 +49,10 @@ validate_targets() {
# Allowed URI schemes: file, ftp, sftp, rsync, s3, swift, sj, dav, davs # Allowed URI schemes: file, ftp, sftp, rsync, s3, swift, sj, dav, davs
# Required schemes (one or more times): file # Required schemes (one or more times): file
local array=("$@")
local scheme= local scheme=
local file_targets=() local file_targets=()
for uri in "${array[@]}"; do for uri in "$@"; do
scheme="${uri%%:*}" scheme="${uri%%:*}"
# Process Path-only URI # Process Path-only URI
@ -67,7 +65,7 @@ validate_targets() {
fi fi
;; ;;
*) *)
echo "Error: $__user_script:" \ echo "Error: $backup_script:" \
"Unsupported URI scheme: $scheme" >&2 "Unsupported URI scheme: $scheme" >&2
exit 1 exit 1
;; ;;
@ -75,7 +73,7 @@ validate_targets() {
done done
if [ "${#file_targets[@]}" -eq 0 ]; then if [ "${#file_targets[@]}" -eq 0 ]; then
echo -e "Error: $__user_script: 'file' scheme is not set in targets."\ echo -e "Error: $backup_script: 'file' scheme is not set in targets."\
"You must provide one or more targets with 'file' scheme." >&2 "You must provide one or more targets with 'file' scheme." >&2
exit 1 exit 1
else else
@ -88,7 +86,7 @@ validate_targets() {
if [ -d "$path" ]; then if [ -d "$path" ]; then
__main_target_path="$path" __main_target_path="$path"
else else
echo "Error: $__user_script:" \ echo "Error: $backup_script:" \
"Path '$path' from URI '$__main_target'" \ "Path '$path' from URI '$__main_target'" \
"does not exists or not a directory" >&2 "does not exists or not a directory" >&2
exit 1 exit 1
@ -109,23 +107,25 @@ source_script() {
# Dry run script, check syntax. See manpage set(1) # Dry run script, check syntax. See manpage set(1)
if ! bash -n "$script"; then if ! bash -n "$script"; then
echo "Error: $__user_script: Please check your syntax" >&2; exit 1 echo "Error: $backup_script: Please check your syntax" >&2; exit 1
fi fi
# Source script # Source script
# shellcheck disable=SC1090 # shellcheck disable=SC1090
. "$script" if ! . "$(realpath "$script")"; then
echo "Error: Cannot source script: $script" >&2; exit 1;
fi
# Check required variables # Check required variables
if [ -n "$sources" ]; then if [ -n "$sources" ]; then
validate_sources "${sources[@]}" validate_sources "${sources[@]}"
else else
echo "Error: $__user_script: sources array is not set" >&2; exit 1 echo "Error: $backup_script: sources array is not set" >&2; exit 1
fi fi
if [ -n "$targets" ]; then if [ -n "$targets" ]; then
validate_targets "${targets[@]}" validate_targets "${targets[@]}"
else else
echo "Error: $__user_script: targets array is not set" >&2; exit 1 echo "Error: $backup_script: targets array is not set" >&2; exit 1
fi fi
} }