backup - fix toolBin and correct shellcheck warnings

This commit is contained in:
2022-12-19 09:21:07 +01:00
parent a57fda8d3f
commit aad6ded99b

View File

@@ -1,8 +1,9 @@
#!/bin/bash #!/bin/bash
readonly toolName=backup # shellcheck disable=SC2001 # See if you can use ${variable//search/replace} instead.
readonly toolBin=rsync # It is more easy with sed to remove multiple trailing /
sq_toolBin=rsync
sq_aptOpt= sq_aptOpt=
sq_config=0 sq_config=0
@@ -30,7 +31,7 @@ step_1() {
local buTarget="/backup" local buTarget="/backup"
if (( ! sq_config )) ; then if (( ! sq_config )) ; then
error -e "Cannot backup root without properly configured excludes" error -e "Cannot backup root without properly configured excludes"
error -e " (expected config: ${seq_configFile})" error -e " (expected config: ${seq_configFile:?})"
else else
buTarget="$BACKUP_TARGET" buTarget="$BACKUP_TARGET"
fi fi
@@ -44,8 +45,8 @@ step_1() {
step_3_info() { step_3_info() {
# Backup single directory recursively # Backup single directory recursively
local opt= local opt=
local dir= local dir='<SOURCE>'
local tar= local tar='[TARGET]'
local exc= local exc=
shift shift
if [ "${1:-}" == "-t" ] ; then if [ "${1:-}" == "-t" ] ; then
@@ -55,14 +56,14 @@ step_3_info() {
if [ -n "${1:-}" ] ; then if [ -n "${1:-}" ] ; then
dir="$1" dir="$1"
shift shift
if [ -z $opt ] && [ -n "$1" ] ; then if [ -z "$opt" ] && [ -n "$1" ] ; then
tar="$1" tar="$1"
shift shift
else else
tar="$BACKUP_TARGET" tar="$BACKUP_TARGET"
fi fi
if [ -n "${1:-}" ] ; then if [ -n "${1:-}" ] ; then
exc="$@" exc="$*"
else else
exc="[NO EXCLUDES]" exc="[NO EXCLUDES]"
fi fi
@@ -102,26 +103,26 @@ step_3() {
exit 1 exit 1
fi fi
buLog=$(basename $1) buLog="$(basename -- "$1")"
if [ "$buLog" == "/" ] ; then if [ "$buLog" == "/" ] ; then
buLog="root" buLog="root"
buSource="$1" buSource="$1"
else else
# remove trailing slashes # remove trailing slashes
buSource=$(echo "$1" | sed 's:/*$::') buSource="$(echo "$1" | sed 's:/*$::')"
fi fi
buLog="backup_${buLog}" buLog="backup_${buLog}"
shift shift
if [ $configTarget -ne 0 ] && (( sq_config )) ; then if (( configTarget )) && (( sq_config )) ; then
# Taking target from config # Taking target from config
buTarget=$(echo "$BACKUP_TARGET" | sed 's:/*$::') buTarget="$(echo "$BACKUP_TARGET" | sed 's:/*$::')"
else else
if [ -z "$1" ] ; then if [ -z "$1" ] ; then
error -e "No valid target found" error -e "No valid target found"
exit 1 exit 1
fi fi
buTarget=$(echo "$1" | sed 's:/*$::') buTarget="$(echo "$1" | sed 's:/*$::')"
shift shift
fi fi
if [ ! -d "${buTarget}" ] && [ ! -L "${buTarget}" ] if [ ! -d "${buTarget}" ] && [ ! -L "${buTarget}" ]
@@ -136,11 +137,13 @@ step_3() {
info "Source : $buSource" info "Source : $buSource"
info "Target : $buTarget" info "Target : $buTarget"
info "Excludes: $@" info "Excludes: $*"
#fix doubling trailing slash on verbose output when backing up root #fix doubling trailing slash on verbose output when backing up root
local tmpSource="$buSource/" local tmpSource="$buSource/"
local tmpTarget="${buTarget}/$(basename ${buSource})" local tmpTarget=
tmpTarget="${buTarget}/$(basename -- "${buSource}")"
if [ "$buSource" == "/" ] ; then if [ "$buSource" == "/" ] ; then
tmpSource="/" tmpSource="/"
tmpTarget="${buTarget}/" tmpTarget="${buTarget}/"
@@ -159,26 +162,25 @@ step_3() {
checkInstalled checkInstalled
exep "mv -f ${buTarget}/${buLog}0.log /tmp/${buLog}1.log 2>/dev/null" 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" exep "${sq_toolBin} -avxHAX --delete --info=stats2 ${buExcludes[*]} ${tmpSource} ${tmpTarget} > /tmp/${buLog}0.log"
exe mv -f /tmp/${buLog}*.log ${buTarget} exe mv -f /tmp/"${buLog}"*.log "${buTarget}"
exe sync exe sync
exep "mount -o ro,remount '${buTarget}' >>/dev/null 2>&1" exep "mount -o ro,remount '${buTarget}' >>/dev/null 2>&1"
} }
step_100_info() { echo "Install $toolBin"; } step_100_info() { echo "Install ${sq_toolBin}"; }
step_100_alias() { echo "install"; } step_100_alias() { echo "install"; }
step_100() { step_100() {
exe apt update exe apt update
exe apt install $toolBin ${sq_aptOpt} exe apt install ${sq_toolBin} ${sq_aptOpt}
} }
checkInstalled() { checkInstalled() {
command -v $toolBin >>/dev/null if ! command -v ${sq_toolBin} >>/dev/null ; then
if [ $? -ne 0 ] ; then
step install step install
fi fi
toolBin="$(command -v $toolBin)" sq_toolBin="$(command -v ${sq_toolBin})"
} }
# shellcheck disable=SC2034 # Appears unused # shellcheck disable=SC2034 # Appears unused