2022-05-15 02:16:51 +03:00
|
|
|
#!/usr/bin/env bash
|
2022-05-15 02:13:05 +03:00
|
|
|
|
|
|
|
# bafscript -- backup automation micro-framework.
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
__version='0.0.0'
|
|
|
|
__config=
|
2022-05-15 07:19:52 +03:00
|
|
|
__verbose=
|
2022-05-15 02:13:05 +03:00
|
|
|
__log_file='./log.txt'
|
2022-05-15 08:12:47 +03:00
|
|
|
__tar_options='-acf'
|
|
|
|
__tar_compression='gzip'
|
2022-05-15 02:13:05 +03:00
|
|
|
__name_date_fmt='_%Y%m%d-%H%M'
|
|
|
|
|
|
|
|
if [ -n "$BAFLIB" ]; then
|
|
|
|
__library="$BAFLIB"
|
|
|
|
else
|
|
|
|
__library='./lib'
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Source library
|
|
|
|
for file in "$__library"/*; do
|
|
|
|
. "$file"
|
|
|
|
done
|
|
|
|
|
|
|
|
print_help() {
|
|
|
|
cat <<- EOF
|
|
|
|
Backup files and databases.
|
|
|
|
|
|
|
|
Usage: $0 [-cvlhV] ARGUMENTS..
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-c, --config config file.
|
|
|
|
-v, --verbose verbose output.
|
|
|
|
-l, --log-file log file.
|
|
|
|
-h, --help print this help messagea and exit.
|
|
|
|
-V, --version print version and exit.
|
|
|
|
|
|
|
|
Environment:
|
|
|
|
BAFLIB path to baf library [current: $__library]
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
# ---------------------------------------------------------- #
|
|
|
|
# * CLI Arguments Parser #
|
|
|
|
# ---------------------------------------------------------- #
|
|
|
|
|
|
|
|
optval() {
|
|
|
|
# GNU-style CLI options parser.
|
|
|
|
#
|
|
|
|
# Parse --opt VAL and --opt=VAL options.
|
|
|
|
# Requires 2 arguments: $1, $2.
|
|
|
|
# Return variables:
|
|
|
|
# opt option name.
|
|
|
|
# val option value.
|
|
|
|
# sft value for shift.
|
|
|
|
|
|
|
|
if [[ "$1" =~ .+=.+ ]]; then
|
|
|
|
opt="${1%%=*}"; val="${1#*=}"; sft=1
|
|
|
|
elif [[ ! "$1" =~ .+=$ ]] && [ "$2" ] && [ "${2:0:1}" != "-" ]; then
|
|
|
|
opt="$1"; val="$2"; sft=2
|
|
|
|
else
|
|
|
|
opt="$1"
|
|
|
|
if [[ "$1" =~ .+=$ ]]; then opt="${1:0: -1}"; fi
|
|
|
|
echo "Error: Missing argument for $opt" >&2; exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# Print help if no arguments passed
|
|
|
|
[[ "$#" == 0 ]] && { print_help; exit 1; }
|
|
|
|
|
|
|
|
# Split combined short options, e.g. '-abc' to '-a' '-b' '-c'
|
|
|
|
for args in "$@"; do
|
|
|
|
shift
|
|
|
|
case "$args" in
|
|
|
|
--*)
|
|
|
|
set -- "$@" "$args";; # save long options
|
|
|
|
-*)
|
|
|
|
args="$(echo "${args:1}" | grep -o . | xargs -I {} echo -n '-{} ')"
|
|
|
|
set -- "$@" $args;;
|
|
|
|
*)
|
|
|
|
set -- "$@" "$args";; # save positional arguments
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# Final arguments parser
|
|
|
|
while (( "$#" )); do
|
|
|
|
case "$1" in
|
|
|
|
-c|--config|--config=*) optval "$1" "$2"; __config="$val"; shift "$sft";;
|
|
|
|
-v|--verbose) __verbose=1; shift;;
|
|
|
|
-l|--log-file|--log-file=*) optval "$1" "$2"; __log_file="$val"; shift "$sft";;
|
|
|
|
-h|--help) print_help; exit 1;;
|
|
|
|
-V|--version) echo "$__version"; exit 0;;
|
|
|
|
-*) echo "Error: Unknown option: $1" >&2; exit 1;;
|
|
|
|
*) __args+=("$1"); shift;; # Save positional args
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# ---------------------------------------------------------- #
|
|
|
|
# * Do backups #
|
|
|
|
# ---------------------------------------------------------- #
|
|
|
|
|
2022-05-15 07:19:52 +03:00
|
|
|
# Scripts counter.
|
|
|
|
_count=${#__args[@]} # count
|
|
|
|
_iter=1 # iterator
|
|
|
|
|
|
|
|
# Startup log.
|
|
|
|
date +'Start: %d %b %Y %T %z'
|
|
|
|
log "Backup STARTED"
|
|
|
|
log -p "Configuration file: $([ "$__config" ] || echo not specified && echo "$__config")"
|
|
|
|
log "Scripts to process (${_count}): ${__args[@]}"
|
|
|
|
|
|
|
|
|
2022-05-15 02:13:05 +03:00
|
|
|
for script in "${__args[@]}"; do
|
|
|
|
source_script "$script"
|
2022-05-15 08:12:47 +03:00
|
|
|
#. "$__config" # TODO Make it safe!
|
2022-05-15 02:13:05 +03:00
|
|
|
|
2022-05-15 07:19:52 +03:00
|
|
|
echo
|
|
|
|
echo -e "\e[1m==> Script: ${__args[_iter-1]##*/} [$_iter/$_count]\e[0m" | log -p
|
|
|
|
|
2022-05-15 02:13:05 +03:00
|
|
|
# Initialise variables
|
|
|
|
__user_script="$script"
|
|
|
|
backups=() # Array of created backups, contains full pathes
|
|
|
|
|
|
|
|
# Run prepare() before all if set
|
|
|
|
if is_function_set prepare; then
|
2022-05-15 07:19:52 +03:00
|
|
|
log -p "Execute prepare() ..."
|
2022-05-15 02:13:05 +03:00
|
|
|
prepare
|
2022-05-15 07:19:52 +03:00
|
|
|
unset prepare
|
2022-05-15 02:13:05 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Run user defined backup() if set or builtin_backup()
|
|
|
|
if is_function_set backup; then
|
2022-05-15 07:19:52 +03:00
|
|
|
echo -e "Execute \e[1mbackup()\e[0m ..." | log -p
|
2022-05-15 02:13:05 +03:00
|
|
|
backup
|
2022-05-15 07:19:52 +03:00
|
|
|
unset backup
|
2022-05-15 02:13:05 +03:00
|
|
|
else
|
2022-05-15 07:19:52 +03:00
|
|
|
echo -e "Execute \e[1mbuiltin_backup()\e[0m ..." | log -p
|
2022-05-15 02:13:05 +03:00
|
|
|
builtin_backup
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Run post backup function if set
|
|
|
|
if is_function_set finalise; then
|
2022-05-15 07:19:52 +03:00
|
|
|
echo -e "Execute \e[1mfinalise()\e[0m ..." | log -p
|
2022-05-15 02:13:05 +03:00
|
|
|
finalise
|
2022-05-15 07:19:52 +03:00
|
|
|
unset finalise
|
2022-05-15 02:13:05 +03:00
|
|
|
fi
|
2022-05-15 07:19:52 +03:00
|
|
|
|
|
|
|
# Increase counter
|
|
|
|
((_iter++)) || true
|
|
|
|
|
|
|
|
# Unset user defined variables
|
|
|
|
unset tar_options
|
2022-05-15 08:12:47 +03:00
|
|
|
unset tar_compression
|
2022-05-15 07:19:52 +03:00
|
|
|
unset name_date_fmt
|
2022-05-15 02:13:05 +03:00
|
|
|
done
|
2022-05-15 07:19:52 +03:00
|
|
|
|
|
|
|
log "Backup FINISHED"
|
|
|
|
echo -e "\nBackup [Done]"
|