115 lines
3.1 KiB
Bash
115 lines
3.1 KiB
Bash
#! /usr/bin/env bash
|
|
|
|
# script.sh - utilitary functions.
|
|
# 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/>.
|
|
|
|
validate_sources() {
|
|
# Check sources array.
|
|
#
|
|
# Usage: validate_sources ARRAY
|
|
|
|
# Allowed URI schemes: file, mysql postgres, sqlite
|
|
# No required schemes.
|
|
|
|
local array=("$@")
|
|
local scheme=
|
|
|
|
for uri in "${array[@]}"; do
|
|
scheme="${uri%%:*}"
|
|
case "$scheme" in
|
|
file|mysql|postgres|sqlite) : ;; # do nothing, this is OK
|
|
*) echo "Error: Unsupported URI scheme: $scheme" >&2; exit 1;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
validate_targets() {
|
|
# Check targets array and set local_target variable.
|
|
#
|
|
# Usage: validate_targets ARRAY
|
|
|
|
# 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
|
|
scheme="${uri%%:*}"
|
|
case "$scheme" in
|
|
file|ftp|sftp|rsync|s3|swift|sj|dav|davs)
|
|
if [[ "$scheme" == file ]]; then
|
|
file_targets+=("$uri")
|
|
fi
|
|
;;
|
|
*) echo "Error: Unsupported URI scheme: $scheme" >&2; exit 1;;
|
|
esac
|
|
done
|
|
|
|
if [ "${#file_targets[@]}" -eq 0 ]; then
|
|
echo "Error: 'file' scheme is not set in targets." \
|
|
"You must provide one or more targets with 'file' scheme." >&2
|
|
exit 1
|
|
else
|
|
# Set local_target. This variable contains path to save local backups.
|
|
# Files to additional targets will be coped from this directory.
|
|
local_target="${file_targets[0]}"
|
|
fi
|
|
}
|
|
|
|
source_script() {
|
|
# Safely as possible source backup script.
|
|
#
|
|
# Usage: source_script SCRIPT
|
|
|
|
local script="$1"
|
|
|
|
if ! test -f "$script"; then
|
|
echo "Error: No such file: $script" >&2; exit 1
|
|
fi
|
|
|
|
# Dry run script, check syntax. See set(1p)
|
|
if ! bash -n "$script"; then
|
|
echo Error: Please check your syntax >&2; exit 1
|
|
fi
|
|
|
|
# Source script
|
|
. "$@"
|
|
|
|
# Check required variables
|
|
if [[ "$sources" ]]; then
|
|
validate_sources "${sources[@]}"
|
|
else
|
|
echo Error: sources array is not set >&2; exit 1
|
|
fi
|
|
|
|
if [[ "$targets" ]]; then
|
|
validate_targets "${targets[@]}"
|
|
else
|
|
echo Error: targets array is not set >&2; exit 1
|
|
fi
|
|
}
|
|
|
|
is_function_set() {
|
|
# Test function is set or not. Return exit code.
|
|
# Useful with 'if' statement.
|
|
#
|
|
# Usage: is_function_set FUNCTION
|
|
|
|
declare -F -- "$1" > /dev/null
|
|
}
|