Files
shell_sequencer/seqs/ebackup.sh

331 lines
7.2 KiB
Bash
Executable File

#!/bin/bash
toolName=duplicity
toolBin=
toolPpa="ppa:duplicity-team/duplicity-release-git"
toolCronDir="/etc/cron.d"
toolPrefix="encBackup_"
toolSyslogTag=
# Get script working directory
# (when called from a different directory)
WDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >>/dev/null 2>&1 && pwd )"
CONFIG=0
SCRIPT_NAME=$(basename -- $0)
SCRIPT_NAME=${SCRIPT_NAME%%.*}
CONFIG_FILE_TEMPLATE="$WDIR/${SCRIPT_NAME}.cfg.example"
step_config() {
initSeqConfig -p "$SCRIPT_NAME" "$CONFIG_FILE_TEMPLATE"
if [ $? -eq 0 ] ; then
CONFIG=1
else
[ $DRY -eq 0 ] && return 1
fi
toolSyslogTag="${SCRIPT_NAME}-$SEQ_PROFILE_NAME"
}
step_1_info() {
echo -n "Backup "
if [ $CONTEXT_HELP -ne 0 ] ; then
echo -n "selected profile"
else
echo -n "profile: $SEQ_PROFILE_NAME"
fi
echo " [OPTIONS] [full|incremental]"
echoinfo " [OPTIONS]"
echoinfo " --no-purge, -n : Do not purge old backups after backup"
}
step_1_alias() { ALIAS="backup"; }
step_1() {
shift
local arg
local retVal
local dupArgs
local purgeAfter=1
for arg in "$@" ; do
case "$1" in
--no-purge|-n)
purgeAfter=0
shift
;;
esac
done
if [ -z $EBU_TARGET ] || [ -z $EBU_SOURCE ] ; then
echo " [I] Nothing to do. Check $SEQ_CONFIG_FILE"
return 1
fi
if [ ! -z "$1" ] && ( [ "$1" == "full" ] || [ "$1" == "incremental" ] ) ; then
dupArgs+=("$1")
elif [ ! -z "$1" ] ; then
echo " [W] $toolName command \"$1\" not recognized"
return 1
fi
echo " [I] Running backup profile [$SEQ_PROFILE_NAME]"
if [ "${dupArgs[0]}" != "full" ] && [ ! -z "$EBU_MAX_FULLBKP_AGE" ] ; then
dupArgs+=(--full-if-older-than "$EBU_MAX_FULLBKP_AGE")
fi
if [ ! -z "$EBU_VOLSIZE" ] ; then
dupArgs+=(--volsize "$EBU_VOLSIZE")
fi
checkInstalled
setPassphrase
exe $toolBin "${dupArgs[@]}" "$EBU_SOURCE" "$EBU_TARGET"
retVal=$?
unsetPassphrase
syslogEntry "Backup complete [$retVal]"
if [ $purgeAfter -ne 0 ] ; then
step purge
fi
}
step_3_info() { echo "Verify selected backup"; }
step_3_alias() { ALIAS="verify"; }
step_3() {
shift
if [ -z $EBU_TARGET ] || [ -z $EBU_SOURCE ] ; then
echo " [I] Nothing to do. Check $SEQ_CONFIG_FILE"
return 1
fi
checkInstalled
setPassphrase
exe $toolBin verify "$EBU_TARGET" "$EBU_SOURCE"
unsetPassphrase
}
step_5_info() {
echo "Restore [OPTIONS] <LOCAL TARGET> [TARGET]"
echoinfo " [OPTIONS]"
echoinfo " --file-to-restore, -f <RELPATH> : Relative path within backup"
echoinfo " (file or folder)"
echoinfo " --time, -t <TIME> : Age of file to be restored"
}
step_5_alias() { ALIAS="restore"; }
step_5() {
shift
local arg
local restoreOpt
for arg in "$@" ; do
case "$1" in
--file-to-restore|-f)
shift
restoreOpt+=(--file-to-restore "$1")
shift
;;
--time|-t)
shift
restoreOpt=(-t "$1")
shift
;;
esac
done
if [ -z "$1" ] ; then
echoerr " [E] No target provided"
return 1
fi
local ebuLocalTarget="$1"
local ebuTarget="$EBU_TARGET"
if [ ! -z "$2" ] ; then
ebuTarget="$2"
fi
checkInstalled
setPassphrase
exe $toolBin restore "${restoreOpt[@]}" "$EBU_TARGET" "$ebuLocalTarget"
unsetPassphrase
}
step_7_info() { echo "Purge old backups [TARGET]"; }
step_7_alias() { ALIAS="purge"; }
step_7() {
shift
local ebuTarget="$EBU_TARGET"
local dupCommand=
if [ ! -z "$1" ] ; then
ebuTarget="$1"
fi
if [ ! -z "$EBU_MAX_AGE" ] ; then
dupCommand+="remove-older-than $EBU_MAX_AGE "
elif [ ! -z "$EBU_MAX_FULL_BACKUPS" ] ; then
dupCommand+="remove-all-but-n-full $EBU_MAX_FULL_BACKUPS "
elif [ ! -z "$EBU_MAX_FULLS_WITH_INCRS" ] ; then
dupCommand+="remove-all-inc-of-but-n-full $EBU_MAX_FULLS_WITH_INCRS "
else
if [ $QUIET -eq 0 ] ; then echoerr " [W] No purge option configured" ; fi
return 1
fi
checkInstalled
setPassphrase
exe $toolBin $dupCommand --force "$ebuTarget"
unsetPassphrase
}
step_20_info() {
shift
local ebuTarget=$EBU_TARGET
[ ! -z "$1" ] && ebuTarget="$1"
if [ $CONTEXT_HELP -ne 0 ]; then
echo "Status of (profile) [TARGET]"
elif [ ! -z "$1" ]; then
echo "Status of target: $ebuTarget"
else
echo "Status of profile \"$SEQ_PROFILE_NAME\" target: $ebuTarget"
fi
}
step_20_alias() { ALIAS='status'; }
step_20() {
shift
local ebuTarget="$EBU_TARGET"
if [ ! -z "$1" ] ; then
ebuTarget="$1"
fi
checkInstalled
exe $toolBin collection-status "$ebuTarget"
}
step_22_info() { echo "List backup files [TARGET]"; }
step_22_alias() { ALIAS='list'; }
step_22() {
shift
local ebuTarget="$EBU_TARGET"
if [ ! -z "$1" ] ; then
ebuTarget="$1"
fi
checkInstalled
exe $toolBin list-current-files "$ebuTarget"
}
step_70_info() {
echo -n "Manage cron file for "
if [ $CONTEXT_HELP -ne 0 ] ; then
echo -n "selected profile"
else
echo -n "profile: $SEQ_PROFILE_NAME"
fi
echo " [OPTIONS]"
echoinfo " [OPTIONS]"
echoinfo " --remove, -r : remove cron file"
}
step_70_alias() { ALIAS='cron'; }
step_70() {
shift
local arg
local cronRemove=0
local cronScript="$toolCronDir/${toolPrefix}$SEQ_PROFILE_NAME"
local cronLog='>/dev/null'
local cronEntry="$EBU_CRONTIME $(whoami) $WDIR/$(basename -- $0) -qq -p $SEQ_PROFILE_NAME"
for arg in "$@" ; do
case "$1" in
--remove|-r)
cronRemove=1
shift
;;
esac
done
if [ ! -z "$EBU_LOG_DIR" ] ; then
cronLog="$EBU_LOG_DIR/${toolPrefix}${SEQ_PROFILE_NAME}.log"
exe touch "$cronLog"
exe chmod 600 "$cronLog"
fi
cronEntry+=" >$cronLog"
if [ -z "$EBU_CRONTIME" ] || [ $cronRemove -ne 0 ] ; then
echo " [I] Removing cron for profile $SEQ_PROFILE_NAME"
exe rm -r "$cronScript"
else
checkFileHead "$cronScript" "$cronEntry"
if [ $? -ne 0 ] ; then
echo " [I] Update cron for profile $SEQ_PROFILE_NAME"
exep "sudo echo \"$cronEntry\" > \"$cronScript\""
syslogEntry "Cron file update complete [$EBU_CRONTIME]"
else
echo " [I] Cron for profile $SEQ_PROFILE_NAME is up to date"
fi
fi
}
step_72_info() { echo "Update all profile cron files"; }
step_72_alias() { ALIAS="reload"; }
step_72() {
for seq in "$SEQ_CONFIG_HOME/"* ; do
seq=$(basename ${seq})
$WDIR/$(basename -- $0) $SEQUENCER_ARGS -qq -p ${seq%%.*} cron
done
}
step_100_info() { echo "Install $toolName $toolPpa"; }
step_100_alias() { ALIAS="install"; }
step_100() {
local aptOpt=
if [ $QUIET -ne 0 ] ; then
aptOpt="-y"
fi
exe add-apt-repository $toolPpa $aptOpt
exe apt install $toolName $aptOpt
}
setPassphrase() {
if [ -z $PASSPHRASE ] && [ ! -z $EBU_PASSPHRASE ] ; then
export PASSPHRASE="$EBU_PASSPHRASE"
fi
}
unsetPassphrase() {
unset PASSPHRASE
}
checkFileHead() {
local readChar
if [ ! -e "$1" ] ; then
echoerr " [E] File $1 not found"
return 1
fi
read -r -n ${#2} readChar < "$1"
if [ "$readChar" == "$2" ] ; then
return 0
fi
return 1
}
syslogEntry() {
if [ "$EBU_SYSLOG" == "true" ] ; then
exe logger -t $toolSyslogTag "$@"
fi
}
checkInstalled() {
if [ -z "$toolBin" ] ; then
command -v $toolName >>/dev/null
if [ $? -ne 0 ] ; then
step install
fi
toolBin="$EBU_PRECMD $(command -v $toolName)"
fi
}
VERSION_SEQREV=12
. /usr/local/bin/sequencer.sh