Files
shell_sequencer/seqs/backup.sh

194 lines
4.3 KiB
Bash
Executable File

#!/bin/bash
toolName=backup
toolBin=rsync
# Get script working directory
# (when called from a different directory)
WDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >>/dev/null 2>&1 && pwd )"
APTOPT=
CONFIG=0
CONFIG_FILE_NAME="${toolName}.cfg"
CONFIG_FILE_TEMPLATE="$WDIR/${CONFIG_FILE_NAME}.example"
step_config() {
initSeqConfig -t "$CONFIG_FILE_NAME" "$CONFIG_FILE_TEMPLATE"
if [ $? -eq 0 ] ; then
CONFIG=1
else
echoerr " [E] Check output for errors"
# End if no configuration file exists
[ $DRY -eq 0 ] && return -1
fi
[ $QUIET -ne 0 ] && APTOPT="-y"
return 0
}
step_1_info() {
echoinfoArgs "[ADDITIONAL_EXCLUDES...]"
echo "Backup root"
echoinfo "Essential excludes are provided in the configuration template."
echoinfo "($CONFIG_FILE_TEMPLATE)"
}
step_1_alias() { ALIAS="buroot"; }
step_1() {
local buTarget="/backup"
if [ $CONFIG -eq 0 ] ; then
echoerr " [E] Cannot backup root without properly configured excludes"
echoerr " (expected config: $SEQ_CONFIG_HOME/$CONFIG_FILE_NAME)"
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="[OPTION]"
local dir="<DIRECTORY>"
local tar="<TARGET>"
local exc="[EXCLUDES...]"
shift
if [ "$1" == "-t" ] ; then
opt=" -t "
tar=
shift
fi
if [ ! -z "$1" ] ; then
dir="$1"
shift
if [ ! -z $tar ] && [ ! -z "$1" ] ; then
tar="$1"
shift
else
tar="$BACKUP_TARGET"
fi
if [ ! -z "$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_alias() { ALIAS="budir"; }
step_3() {
local arg
local configTarget=0
local noRemount=0
local buSource=
local buTarget=
local buExcludes=
local buLog=
# don't use step number
shift
for arg in "$@"; do
case "$1" in
-t)
configTarget=1
shift;;
-n)
noRemount=1
shift;;
esac
done
if [ -z "$1" ] ; then
echoerr " [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 ] && [ $CONFIG -ne 0 ] ; then
# Taking target from config
buTarget=$(echo "$BACKUP_TARGET" | sed 's:/*$::')
else
if [ -z "$1" ] ; then
echoerr " [E] No valid target found"
exit 1
fi
buTarget=$(echo "$1" | sed 's:/*$::')
shift
fi
if [ ! -d "${buTarget}" ] && [ ! -L "${buTarget}" ]
then
echoerr " [E] Backup target (${buTarget}) doesn't exist"
exit 1
fi
for exclu in "$@"; do
buExcludes+=("--exclude='$exclu'")
done
echoseq " [I] Source : $buSource"
echoseq " [I] Target : $buTarget"
echoseq " [I] 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
echoerr " [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() { ALIAS="install"; }
step_100() {
exe apt update
exe apt install $toolBin $APTOPT
}
checkInstalled() {
command -v $toolBin >>/dev/null
if [ $? -ne 0 ] ; then
step install
fi
toolBin="$(command -v $toolBin)"
}
VERSION_SEQREV=14
. /usr/local/bin/sequencer.sh