feat: Add variable name_suffix; Pass shellcheck; Discard useless variables from main script

This commit is contained in:
ge
2022-06-29 21:27:35 +03:00
parent 3d514678e7
commit aaa7a5c2d3
11 changed files with 177 additions and 147 deletions

View File

@ -37,7 +37,9 @@ process_source() {
mysql) handler='handler::mysqldump';;
postgres) handler='handler::pg_dump';;
sqlite) handler='handler::sqlite';;
*) echo "Error: $__user_script: Unsupported URI scheme: $scheme" >&2; exit 1;;
*) # shellcheck disable=SC2154
# '__user_script' is assigned in main script.
echo "Error: $__user_script: Unsupported URI scheme: $scheme" >&2; exit 1;;
esac
# Run handler function
@ -84,10 +86,12 @@ builtin_backup() {
#
# Usage: builtin_backup
# shellcheck disable=SC2154
for source in "${sources[@]}"; do
process_source "$source"
done
# shellcheck disable=SC2154
for target in "${targets[@]}"; do
process_target "$target"
done
@ -102,21 +106,15 @@ gen_backup_name() {
#
# Usage: gen_backup_name NAME_EXT
local prefix
local name
local date_fmt
local name_ext
[ -n "$name_prefix" ] || { prefix="${__user_script}_"; }
name="$(basename $path)" # 'path' is variable parsed from URI
name_prefix="${name_prefix:-${__user_script}_}"
# shellcheck disable=SC2154
name="$(basename "$path")" # 'path' is variable parsed from URI
name_ext="$1"
name_date_fmt="${name_date_fmt:-_%Y.%m.%d}"
name_suffix="${name_suffix:--%H%M}"
# Overwrite __name_date_fmt
if [ -n "$name_date_fmt" ]; then
date_fmt="$name_date_fmt"
else
date_fmt="$__name_date_fmt"
fi
date +"${prefix}${name}${date_fmt}${name_ext}"
date +"${name_prefix}${name}${name_date_fmt}${name_suffix}${name_ext}"
}

View File

@ -31,7 +31,7 @@ log() {
[[ ! -t 0 ]] || [[ "$#" == 0 ]] && message="$(cat <&0)"
# Set log date format
[ "$log_date_fmt" ] || log_date_fmt="$__log_date_fmt"
log_date_fmt="${log_date_fmt:-%d/%b/%Y:%H:%M:%S %z}"
while (( "$#" )); do
case "$1" in
@ -44,7 +44,8 @@ log() {
[[ "$print" == 1 ]] && echo -e "$message"
while read -r line; do
if [[ "$line" ]]; then
if [ -n "$line" ]; then
# shellcheck disable=SC2154
printf '[%s] %s\n' "$(date +"$log_date_fmt")" "$line" >> "$__log_file"
fi
done <<< "$(sed -r 's/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g' <<< "$message")"
@ -69,7 +70,8 @@ err() {
case "$args" in
-*)
args="$(echo "${args:1}" | grep -o . | xargs -I {} echo -n '-{} ')"
set -- "$@" $args;;
# shellcheck disable=SC2086
set -- "$@" $args;; # 'args' must be unquoted!
*)
set -- "$@" "$args";; # save positional arguments
esac
@ -100,9 +102,10 @@ err() {
# Exit
if [ "$errexit" ]; then
# Count errors for backup recap
__errors_count="$(cat $__errors_file)"
# shellcheck disable=SC2154
__errors_count="$(cat "$__errors_file")"
((__errors_count++)) || true
echo $__errors_count > "$__errors_file"
echo "$__errors_count" > "$__errors_file"
log -p "Backup ERROR: Exiting with previous errors" >&2; exit 1
fi
@ -113,7 +116,8 @@ try() {
#
# Usage: try COMMAND
if eval "$@" 2>> "$__log_file"; then
# https://www.shellcheck.net/wiki/SC2294
if "$@" 2>> "$__log_file"; then
return 0 # Success
else
# Count errors
@ -168,6 +172,11 @@ set_compression() {
xz) file_ext='.xz'; compr_util='xz';;
zstd|zst) file_ext='.zst'; compr_util='zstd';;
tzst) file_ext='.zst'; compr_util='zstd';;
*) file_ext=''; compr_util='';; # No compression
*) # No compression
# shellcheck disable=SC2034
file_ext=''
# shellcheck disable=SC2034
compr_util=''
;;
esac
}

View File

@ -32,12 +32,14 @@ handler::mysqldump() {
local file_ext
uri="$1"
# shellcheck disable=SC2154
dst_path="$__main_target_path"
parse_uri "$uri"
[ -z "$port" ] && port=3306 # Set default MySQL port.
# shellcheck disable=SC2154
[ -n "$__verbose" ] && echo "Dumping database '${path##*/}'"\
"owned by ${username}@${hostname} ..." | log -p
@ -61,7 +63,10 @@ handler::mysqldump() {
compr_cmd="cat"
fi
mysqldump_opts="${mysqldump_opts:-}"
# NOTE! mysqldump_opts and compr_cmd variables must be unquoted!
# shellcheck disable=SC2086,SC2154
try mysqldump $mysqldump_opts \
--host="$hostname" \
--port="$port" \
@ -76,33 +81,32 @@ handler::mysqldump() {
fi
}
# POSTGRESQL vvv
bk_dump_postgresql() {
# Do PostgreSQL dump.
bk_log "Dumping database '${path##*/}' owned by ${user}@${host} (PostgreSQL) ..."
dump_name="$entry_local"/"$(bk_get_name "$db_name" .psql.gz)"
[ "$port" ] || port=5432 # Set default PostgreSQL port.
export PGPASSWORD="$password"
pg_dump \
--host="$host" \
--port="$port" \
--dbname="${path##*/}" \
--username="$user" \
--no-password | gzip -c > "$dump_name" |& bk_log
unset PGPASSWORD
if [ -s "$dump_name" ]; then
bk_log "Dumping database '${path##*/}' owned by ${user}@${host} (PostgreSQL) [Done]"
bk_log "Dump saved as: $dump_name"
bk_upload_file "$dump_name"
else
rm "$dump_name"
bk_err "Something went wrong. Dump size is 0 bytes. Removing $dump_name"
fi
}
# POSTGRESQL
#bk_dump_postgresql() {
# # Do PostgreSQL dump.
#
# bk_log "Dumping database '${path##*/}' owned by ${user}@${host} (PostgreSQL) ..."
#
# dump_name="$entry_local"/"$(bk_get_name "$db_name" .psql.gz)"
# [ "$port" ] || port=5432 # Set default PostgreSQL port.
# export PGPASSWORD="$password"
#
# pg_dump \
# --host="$host" \
# --port="$port" \
# --dbname="${path##*/}" \
# --username="$user" \
# --no-password | gzip -c > "$dump_name" |& bk_log
#
# unset PGPASSWORD
#
# if [ -s "$dump_name" ]; then
# bk_log "Dumping database '${path##*/}' owned by ${user}@${host} (PostgreSQL) [Done]"
# bk_log "Dump saved as: $dump_name"
# bk_upload_file "$dump_name"
#
# else
# rm "$dump_name"
# bk_err "Something went wrong. Dump size is 0 bytes. Removing $dump_name"
# fi
#}

View File

@ -26,16 +26,17 @@ handler::tar() {
local uri
local src_path
local dst_path
local opts
local archive
local exclude
local file_ext
uri="$1"
# shellcheck disable=SC2154
dst_path="$__main_target_path"
parse_uri "$uri"
# shellcheck disable=SC2154
if [ -f "$path" ] || [ -d "$path" ]; then
src_path="$path"
else
@ -51,15 +52,8 @@ handler::tar() {
# 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
# Set tar_exclude
if [ -n "$tar_exclude" ]; then
for item in "${tar_exclude[@]}"; do
exclude+=" --exclude $item"
done
@ -67,6 +61,9 @@ handler::tar() {
exclude=
fi
# Set options
tar_options="${tar_options:--acf}"
# Select filename extension by compression type.
if [ "$compression" ]; then
# Make sure for the `--auto-compress` is enabled in __tar_options
@ -99,14 +96,14 @@ handler::tar() {
[ "$__verbose" ] && {
echo "Source path: $src_path"
echo "Destination path: $dst_path"
echo "Run command: tar $exclude $opts $archive $src_path"
echo "Run command: tar $exclude $tar_options $archive $src_path"
}
log "Archiving $src_path to $archive ..."
log "Run command: tar $exclude $opts $archive $src_path"
log "Run command: tar $exclude $tar_options $archive $src_path"
# Run tar
try tar "$exclude" "$opts" "$archive" "$src_path"
try tar "$exclude" "$tar_options" "$archive" "$src_path"
# Append path to 'backups' array
backups+=("$archive")

View File

@ -29,6 +29,7 @@ handler::cp() {
uri="$1"
# shellcheck disable=SC2154
if [[ "$uri" == "$__main_target" ]]; then
: # Do nothing. Source and destination is the same
log "Nothing to do: Source and destination is the same: $__main_target"
@ -36,6 +37,7 @@ handler::cp() {
# Copy backups to another destination
parse_uri "$uri"
# shellcheck disable=SC2154
if [ -f "$path" ] || [ -d "$path" ]; then
dst_path="$path"
else
@ -46,6 +48,7 @@ handler::cp() {
[ "$__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"

View File

@ -1,4 +1,5 @@
#!/usr/bin/env bash
# shellcheck disable=SC1091
# lib.sh - source scripts.
# Copyright (c) 2022 ge <https://nixhacks.net/>

View File

@ -35,7 +35,8 @@ validate_sources() {
case "$scheme" in
file|mysql|postgres|sqlite) : ;; # do nothing, this is OK
*) echo "Error: $__user_script: Unsupported URI scheme: $scheme" >&2; exit 1;;
*) # shellcheck disable=SC2154
echo "Error: $__user_script: Unsupported URI scheme: $scheme" >&2; exit 1;;
esac
done
}
@ -78,6 +79,7 @@ validate_targets() {
__main_target="${file_targets[0]}"
# Fail if __main_target's path is not a directory
parse_uri "$__main_target"
# shellcheck disable=SC2154
if [ -d "$path" ]; then
__main_target_path="$path"
else
@ -99,24 +101,25 @@ source_script() {
echo "Error: No such file: $script" >&2; exit 1
fi
# Dry run script, check syntax. See set(1p)
# 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: $__user_script: Please check your syntax" >&2; exit 1
fi
# Source script
. "$@"
# shellcheck disable=SC1090
. "$script"
# Check required variables
if [[ "$sources" ]]; then
if [ -n "$sources" ]; then
validate_sources "${sources[@]}"
else
echo Error: $__user_script: sources array is not set >&2; exit 1
echo "Error: $__user_script: sources array is not set" >&2; exit 1
fi
if [[ "$targets" ]]; then
if [ -n "$targets" ]; then
validate_targets "${targets[@]}"
else
echo Error: $__user_script: targets array is not set >&2; exit 1
echo "Error: $__user_script: targets array is not set" >&2; exit 1
fi
}

View File

@ -23,7 +23,7 @@ parse_uri() {
# URL-encoded passwords supported.
#
# Usage: parse_uri URI
# Return variables:
# Set variables:
# scheme
# username
# password
@ -113,6 +113,7 @@ parse_uri() {
port=
fi
else
# shellcheck disable=SC2034
hostname="$host"
port=
fi
@ -122,6 +123,7 @@ parse_uri() {
username="${userinfo%:*}"
password="${userinfo#*:}"
else
# shellcheck disable=SC2034
username="$userinfo"
password=
fi