#!/usr/bin/env bash
# bafscript -- backup automation micro-framework.
# Copyright (c) 2022 ge
#
# 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 .
__version='0.0.0'
__config=
__verbose=
__log_file='./log.txt'
__tar_options='-czf'
__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 #
# ---------------------------------------------------------- #
# 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[@]}"
# TODO source config
for script in "${__args[@]}"; do
source_script "$script"
echo
echo -e "\e[1m==> Script: ${__args[_iter-1]##*/} [$_iter/$_count]\e[0m" | log -p
# 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
log -p "Execute prepare() ..."
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 tar_options
unset name_date_fmt
done
log "Backup FINISHED"
echo -e "\nBackup [Done]"