diff --git a/seqs/backup.cfg.example b/seqs/backup.cfg.example new file mode 100644 index 0000000..92de45e --- /dev/null +++ b/seqs/backup.cfg.example @@ -0,0 +1,24 @@ +#!/bin/bash + +# Backup sequence definitions template. +# Remove ".example" for customization. + +# A running debian needs at least the following runtime directories created to start up successfully: +## dev +## proc +## sys +## run + +# Exclude notation "directory/*" creates the directory but NOT its content +BACKUP_EXCLUDES=(\ + "backup*"\ + "/home/network/*"\ + "/dev/*"\ + "/proc/*"\ + "/sys/*"\ + "/tmp/*"\ + "/run/*"\ + "/mnt/*"\ + "/media/*"\ + "/lost+found"\ + ) diff --git a/seqs/backup.sh b/seqs/backup.sh new file mode 100755 index 0000000..f3af497 --- /dev/null +++ b/seqs/backup.sh @@ -0,0 +1,101 @@ +#!/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="$WDIR/${toolName}.cfg" +CONFIG_FILE_DEFAULT="${CONFIG_FILE}.example" + +step_config() { + if [ -f "$CONFIG_FILE" ] ; then + CONFIG=1 + . "$CONFIG_FILE" + elif [ -f "$CONFIG_FILE_DEFAULT" ] ; then + echoerr " [W] Using default configuration $CONFIG_FILE_DEFAULT" + CONFIG=1 + . "$CONFIG_FILE_DEFAULT" + else + echoerr " [W] No Configuration found." + fi +} + +step_1_info() { + echo "Backup root [ADDITIONAL_EXCLUDES...]" + echoinfo "Essential excludes are provided in the configuration template." + echoinfo "($CONFIG_FILE_DEFAULT)" +} +step_1_alias() { ALIAS="buroot"; } +step_1() { + if [ $CONFIG -eq 0 ] ; then + echoerr " [E] Cannot backup root without properly configured excludes" + echoerr " (expected template: $CONFIG_FILE_DEFAULT)" + fi + + # don't use step number + shift + + step budir / /backup "${BACKUP_EXCLUDES[@]}" "$@" +} + +step_3_info() { + echo "Backup [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: $@" + + 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[*]} ${BACKUP_SOURCE}/ ${BACKUP_TARGET}/$(basename ${BACKUP_SOURCE}) > /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=9 +. /usr/local/bin/sequencer.sh +