174 lines
4.9 KiB
Bash
Executable File
174 lines
4.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# boring-backup -- backup automation micro-framework and library.
|
|
# 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=
|
|
__verbose=
|
|
__log_file='./log.txt'
|
|
__log_date_fmt='%d/%b/%Y:%H:%M:%S %z'
|
|
__tar_options='-acf'
|
|
__tar_exclude=
|
|
__name_date_fmt='_%Y%m%d-%H%M'
|
|
__compression='gzip'
|
|
__library="${BBLIB:-./lib}"
|
|
|
|
# Source library
|
|
for file in "$__library"/*; do
|
|
. "$file"
|
|
done
|
|
|
|
print_help() {
|
|
cat <<- EOF
|
|
Backup files and databases.
|
|
|
|
Usage: $0 [-cvlhV] FILES..
|
|
|
|
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:
|
|
BBLIB path to bb 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 #
|
|
# ---------------------------------------------------------- #
|
|
|
|
# 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[@]}"
|
|
|
|
|
|
for script in "${__args[@]}"; do
|
|
# Initialise variables
|
|
__user_script="$script"
|
|
backups=() # Array of created backups, contains full pathes
|
|
errors=() # Array of error messages written by err() function
|
|
|
|
# Source scripts
|
|
source_script "$script"
|
|
|
|
# Config can ovewrite script functions and variables
|
|
[ -n "$__config" ] && . "$__config"
|
|
|
|
echo
|
|
echo -e "\e[1m--> Script: ${__args[__iter-1]##*/} [$__iter/$__count]\e[0m" | log -p
|
|
|
|
# Run prepare() before all if set
|
|
if is_function_set prepare; then
|
|
echo -e "Execute \e[1mprepare()\e[0m ..." | log -p
|
|
prepare
|
|
unset prepare
|
|
fi
|
|
|
|
# Run user defined backup() if set or builtin_backup()
|
|
if is_function_set backup; then
|
|
echo -e "Execute \e[1mbackup()\e[0m ..." | log -p
|
|
backup
|
|
unset backup
|
|
else
|
|
echo -e "Execute \e[1mbuiltin_backup()\e[0m ..." | log -p
|
|
builtin_backup
|
|
fi
|
|
|
|
# Run post backup function if set
|
|
if is_function_set finalise; then
|
|
echo -e "Execute \e[1mfinalise()\e[0m ..." | log -p
|
|
finalise
|
|
unset finalise
|
|
fi
|
|
|
|
# Increase counter
|
|
((__iter++)) || true
|
|
|
|
# Unset user defined variables
|
|
unset compression
|
|
unset tar_options
|
|
unset tar_exclude
|
|
unset name_date_fmt
|
|
done
|
|
|
|
log "Backup FINISHED"
|
|
echo -e "\nBackup [Done]"
|