feat: Improve code, add mariadb scheme, fix source issue
This commit is contained in:
		@@ -21,22 +21,21 @@ validate_sources() {
 | 
			
		||||
    #
 | 
			
		||||
    # Usage: validate_sources ARRAY
 | 
			
		||||
 | 
			
		||||
    # Allowed URI schemes: file, mysql postgres, sqlite
 | 
			
		||||
    # Allowed URI schemes: file, mysql, mariadb, postgres, sqlite
 | 
			
		||||
    # No required schemes.
 | 
			
		||||
 | 
			
		||||
    local array=("$@")
 | 
			
		||||
    local scheme=
 | 
			
		||||
 | 
			
		||||
    for uri in "${array[@]}"; do
 | 
			
		||||
    for uri in "$@"; do
 | 
			
		||||
        scheme="${uri%%:*}"
 | 
			
		||||
 | 
			
		||||
        # Process Path-only URI
 | 
			
		||||
        if [[ "$scheme" == "$uri" ]]; then scheme='file'; fi
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
                echo "Error: $__user_script:" \
 | 
			
		||||
                echo "Error: $backup_script:" \
 | 
			
		||||
                    "Unsupported URI scheme: $scheme" >&2; exit 1;;
 | 
			
		||||
        esac
 | 
			
		||||
    done
 | 
			
		||||
@@ -50,11 +49,10 @@ validate_targets() {
 | 
			
		||||
    # Allowed URI schemes: file, ftp, sftp, rsync, s3, swift, sj, dav, davs
 | 
			
		||||
    # Required schemes (one or more times): file
 | 
			
		||||
 | 
			
		||||
    local array=("$@")
 | 
			
		||||
    local scheme=
 | 
			
		||||
    local file_targets=()
 | 
			
		||||
 | 
			
		||||
    for uri in "${array[@]}"; do
 | 
			
		||||
    for uri in "$@"; do
 | 
			
		||||
        scheme="${uri%%:*}"
 | 
			
		||||
 | 
			
		||||
        # Process Path-only URI
 | 
			
		||||
@@ -67,7 +65,7 @@ validate_targets() {
 | 
			
		||||
                fi
 | 
			
		||||
            ;;
 | 
			
		||||
            *)
 | 
			
		||||
                echo "Error: $__user_script:" \
 | 
			
		||||
                echo "Error: $backup_script:" \
 | 
			
		||||
                    "Unsupported URI scheme: $scheme" >&2
 | 
			
		||||
                exit 1
 | 
			
		||||
            ;;
 | 
			
		||||
@@ -75,7 +73,7 @@ validate_targets() {
 | 
			
		||||
    done
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
        exit 1
 | 
			
		||||
    else
 | 
			
		||||
@@ -88,7 +86,7 @@ validate_targets() {
 | 
			
		||||
        if [ -d "$path" ]; then
 | 
			
		||||
            __main_target_path="$path"
 | 
			
		||||
        else
 | 
			
		||||
            echo "Error: $__user_script:" \
 | 
			
		||||
            echo "Error: $backup_script:" \
 | 
			
		||||
                "Path '$path' from URI '$__main_target'" \
 | 
			
		||||
                "does not exists or not a directory" >&2
 | 
			
		||||
            exit 1
 | 
			
		||||
@@ -109,23 +107,25 @@ source_script() {
 | 
			
		||||
 | 
			
		||||
    # Dry run script, check syntax. See manpage set(1)
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
    # Source script
 | 
			
		||||
    # shellcheck disable=SC1090
 | 
			
		||||
    . "$script"
 | 
			
		||||
    if ! . "$(realpath "$script")"; then
 | 
			
		||||
        echo "Error: Cannot source script: $script" >&2; exit 1;
 | 
			
		||||
    fi
 | 
			
		||||
 | 
			
		||||
    # Check required variables
 | 
			
		||||
    if [ -n "$sources" ]; then
 | 
			
		||||
        validate_sources "${sources[@]}"
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
    if [ -n "$targets" ]; then
 | 
			
		||||
        validate_targets "${targets[@]}"
 | 
			
		||||
    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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user