Files
shell_sequencer/seqs/backup.sh
Martin Winkler 31b85395d6 New budir option to take backup target from config
Rsync doesn't cross filesystem boundaries any more

Important partitions need to be backup separately using "budir"
2020-01-13 16:42:29 +01:00

124 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
toolName=backup
# Get script working directory
# (when called from a different directory)
WDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >>/dev/null 2>&1 && pwd )"
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"
fi
}
step_1_info() {
echo "Backup root [ADDITIONAL_EXCLUDES...]"
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() {
echo "Backup [OPTION] <DIRECTORY> <TARGET> [EXCLUDES...]"
echoinfo "Exclude path notation starts within DIRECTORY."
echoinfo "e.g. to exclude DIRECTORY/a:"
echoinfo " $0 budir DIRECTORY TARGET /a"
}
step_3_alias() { ALIAS="budir"; }
step_3() {
local configTarget=0
local buSource=
local buTarget=
local buExcludes=
local buLog=
# don't use step number
shift
if [ "$1" == "-t" ] ; then
configTarget=1
shift
fi
if [ -z "$1" ] ; then
echoerr " [E] Noting 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
echo " [I] Source : $buSource"
echo " [I] Target : $buTarget"
echo " [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
exep "mount -o rw,remount '${buTarget}' >>/dev/null 2>&1"
exep "mv -f ${buTarget}/${buLog}0.log /tmp/${buLog}1.log 2>/dev/null"
exep "rsync -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"
}
VERSION_SEQREV=10
. /usr/local/bin/sequencer.sh