Files
shell_sequencer/seqs/backup.sh

188 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
readonly toolName=backup
readonly toolBin=rsync
sq_aptOpt=
sq_config=0
seq_config() {
if initSeqConfig -t "${seq_configName:?}" "${seq_configTemplate:?}" ; then
sq_config=1
else
error -e "Check output for errors"
# End if no configuration file exists
dry || return 1
fi
interactive || sq_aptOpt="-y"
return 0
}
step_1_info() {
echo "Backup root"
echoinfo "Essential excludes are provided in the configuration template."
echoinfo "(${seq_configTemplate})"
}
step_1_options() { echo "[ADDITIONAL_EXCLUDES...]"; }
step_1_alias() { echo "buroot"; }
step_1() {
local buTarget="/backup"
if (( ! sq_config )) ; then
error -e "Cannot backup root without properly configured excludes"
error -e " (expected config: ${seq_configFile})"
else
buTarget="$BACKUP_TARGET"
fi
# don't use step number
shift
step budir / "$buTarget" "${BACKUP_EXCLUDES[@]}" "$@"
}
step_3_info() {
# Backup single directory recursively
local opt=
local dir=
local tar=
local exc=
shift
if [ "${1:-}" == "-t" ] ; then
opt=" -t "
shift
fi
if [ -n "${1:-}" ] ; then
dir="$1"
shift
if [ -z $opt ] && [ -n "$1" ] ; then
tar="$1"
shift
else
tar="$BACKUP_TARGET"
fi
if [ -n "${1:-}" ] ; then
exc="$@"
else
exc="[NO EXCLUDES]"
fi
fi
echo "Backup $opt $dir $tar $exc"
echoinfo " -t : Using configuration value as TARGET"
echoinfo " <DIRECTORY> [EXCLUDES...]"
echoinfo "EXCLUDES path notation starts within $dir"
echoinfo " e.g. to exclude $dir/a:"
echoinfo " $0 budir $dir $tar /a"
}
step_3_options() { echo "[OPTION] <DIRECTORY> [TARGET] [EXCLUDES...]"; }
step_3_alias() { echo "budir"; }
step_3() {
local configTarget=0
local noRemount=0
local buSource=
local buTarget=
local buExcludes=
local buLog=
# don't use step number
shift
for _ in "$@"; do
case "$1" in
-t)
configTarget=1
shift;;
-n)
noRemount=1
shift;;
esac
done
if [ -z "$1" ] ; then
error -e "Nothing found to backup $1"
exit 1
fi
buLog=$(basename $1)
if [ "$buLog" == "/" ] ; then
buLog="root"
buSource="$1"
else
# remove trailing slashes
buSource=$(echo "$1" | sed 's:/*$::')
fi
buLog="backup_${buLog}"
shift
if [ $configTarget -ne 0 ] && (( sq_config )) ; then
# Taking target from config
buTarget=$(echo "$BACKUP_TARGET" | sed 's:/*$::')
else
if [ -z "$1" ] ; then
error -e "No valid target found"
exit 1
fi
buTarget=$(echo "$1" | sed 's:/*$::')
shift
fi
if [ ! -d "${buTarget}" ] && [ ! -L "${buTarget}" ]
then
error -e "Backup target (${buTarget}) doesn't exist"
exit 1
fi
for exclu in "$@"; do
buExcludes+=("--exclude='$exclu'")
done
info "Source : $buSource"
info "Target : $buTarget"
info "Excludes: $@"
#fix doubling trailing slash on verbose output when backing up root
local tmpSource="$buSource/"
local tmpTarget="${buTarget}/$(basename ${buSource})"
if [ "$buSource" == "/" ] ; then
tmpSource="/"
tmpTarget="${buTarget}/"
fi
if [ $noRemount -eq 0 ]; then
# remount target to be writable
exep "mount -o rw,remount '${buTarget}' >>/dev/null 2>&1"
endReturn -o $? "Remount (${buTarget}) to be writable failed"
fi
if [ ! -w "${buTarget}" ] ; then
error -e "Backup target (${buTarget}) is not writable"
exit 1
fi
checkInstalled
exep "mv -f ${buTarget}/${buLog}0.log /tmp/${buLog}1.log 2>/dev/null"
exep "$toolBin -avxHAX --delete --info=stats2 ${buExcludes[*]} ${tmpSource} ${tmpTarget} > /tmp/${buLog}0.log"
exe mv -f /tmp/${buLog}*.log ${buTarget}
exe sync
exep "mount -o ro,remount '${buTarget}' >>/dev/null 2>&1"
}
step_100_info() { echo "Install $toolBin"; }
step_100_alias() { echo "install"; }
step_100() {
exe apt update
exe apt install $toolBin ${sq_aptOpt}
}
checkInstalled() {
command -v $toolBin >>/dev/null
if [ $? -ne 0 ] ; then
step install
fi
toolBin="$(command -v $toolBin)"
}
# shellcheck disable=SC2034 # Appears unused
readonly sqr_minVersion=16
# shellcheck disable=SC1091 # Don't follow this source
. /usr/local/bin/sequencer.sh