Files
shell_sequencer/seqs/backup.sh

109 lines
2.8 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 <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() {
# don't use step number
shift
if [ -z "$1" ] ; then
echoerr " [E] Noting found to backup $1"
exit 1
fi
BACKUP_LOG=$(basename $1)
if [ "$BACKUP_LOG" == "/" ] ; then
BACKUP_LOG="root"
BACKUP_SOURCE=$1
else
# remove trailing slashes
BACKUP_SOURCE=$(echo $1 | sed 's:/*$::')
fi
BACKUP_LOG="backup_${BACKUP_LOG}"
shift
if [ -z "$1" ] ; then
echoerr " [E] No valid target found"
exit 1
fi
BACKUP_TARGET=$(echo $1 | sed 's:/*$::')
if [ ! -d "${BACKUP_TARGET}" ] && [ ! -L "${BACKUP_TARGET}" ]
then
echoerr " [E] Backup target (${BACKUP_TARGET}) doesn't exist"
exit 1
fi
shift
for exclu in "$@"; do
BU_EXCLUDES+=("--exclude='$exclu'")
done
echo " [I] Source : $BACKUP_SOURCE"
echo " [I] Target : $BACKUP_TARGET"
echo " [I] Excludes: $@"
#fix doubling trailing slash on verbose output when backing up root
local buSource="$BACKUP_SOURCE/"
local buTarget="${BACKUP_TARGET}/$(basename ${BACKUP_SOURCE})"
if [ "$BACKUP_SOURCE" == "/" ] ; then
buSource="/"
buTarget="${BACKUP_TARGET}/"
fi
exep "mount -o rw,remount '${BACKUP_TARGET}' >>/dev/null 2>&1"
exep "mv -f ${BACKUP_TARGET}/${BACKUP_LOG}0.log /tmp/${BACKUP_LOG}1.log 2>/dev/null"
exep "rsync -avAX --delete --info=stats2 ${BU_EXCLUDES[*]} ${buSource} ${buTarget} > /tmp/${BACKUP_LOG}0.log"
exe mv -f /tmp/${BACKUP_LOG}*.log ${BACKUP_TARGET}
exe sync
exep "mount -o ro,remount '${BACKUP_TARGET}' >>/dev/null 2>&1"
}
VERSION_SEQREV=10
. /usr/local/bin/sequencer.sh