Files
shell_sequencer/seqs/ebackup.sh

335 lines
7.4 KiB
Bash
Executable File

#!/bin/bash
readonly toolName=duplicity
readonly toolCronDir="/etc/cron.d"
readonly toolPrefix="encBackup_"
toolBin=
toolSyslogTag=
sq_aptOpt=
sq_config=0
seq_config() {
if initSeqConfig -p "${seq_fileName:?}" "${seq_configTemplate:?}" ; then
sq_config=1
else
dry || return 1
fi
## Apt cmdline option to suppress user interaction
interactive || sq_aptOpt="-y"
toolSyslogTag="${seq_fileName:?}-$seq_profileName"
return 0
}
step_1_info() {
echo -n "Backup "
if contextHelp ; then
echo "selected profile"
else
echo "profile: $seq_profileName"
fi
echoinfo " [OPTIONS]"
echoinfo " --no-purge, -n : Do not purge old backups after backup"
echoinfo " --allow-source-mismatch, -a : see ${toolName:-} help"
}
step_1_options() { echo '[OPTIONS] [full|incremental]'; }
step_1_alias() { echo "backup"; }
step_1() {
shift
local arg
local retVal
local dupArgs
local purgeAfter=1
local lSourceMismatch=
for arg in "$@" ; do
case "$1" in
--no-purge|-n)
purgeAfter=0
shift ;;
--allow-source-mismatch|-a)
dupArgs+=("--allow-source-mismatch")
shift ;;
esac
done
if [ -z $EBU_TARGET ] || [ -z $EBU_SOURCE ] ; then
info "Nothing to do. Check $seq_configFile"
return 1
fi
if [ -n "${1:-}" ] && ( [ "$1" == "full" ] || [ "$1" == "incremental" ] ) ; then
dupArgs+=("$1")
elif [ -n "${1:-}" ] ; then
warning "$toolName command \"$1\" not recognized"
return 1
fi
info "Running backup profile [$seq_profileName]"
if [ "${dupArgs[0]:-}" != "full" ] && [ -n "$EBU_MAX_FULLBKP_AGE" ] ; then
dupArgs+=(--full-if-older-than "$EBU_MAX_FULLBKP_AGE")
fi
if [ -n "$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() { echo "verify"; }
step_3() {
shift
if [ -z $EBU_TARGET ] || [ -z $EBU_SOURCE ] ; then
info "Nothing to do. Check $seq_configFile"
return 1
fi
checkInstalled
setPassphrase
exe $toolBin verify "$EBU_TARGET" "$EBU_SOURCE"
unsetPassphrase
}
step_5_info() {
echo "Restore files from backup"
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_options() { echo '[OPTIONS] <LOCAL TARGET> [TARGET]'; }
step_5_alias() { echo "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
error -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"; }
step_7_options() { echo '[TARGET]'; }
step_7_alias() { echo "purge"; }
step_7() {
shift
local ebuTarget="$EBU_TARGET"
local dupCommand=
if [ -n "${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 interactive ; then warning -e "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 contextHelp; then
echo "Status of (profile)"
elif [ -n "${1:-}" ]; then
echo "Status of target: $ebuTarget"
else
echo "Status of profile \"$seq_profileName\" target: $ebuTarget"
fi
}
step_20_options() { echo '[TARGET]'; }
step_20_alias() { echo 'status'; }
step_20() {
shift
local ebuTarget="$EBU_TARGET"
[[ -n "${1:-}" ]] && ebuTarget="$1"
checkInstalled
exe $toolBin collection-status "$ebuTarget"
}
step_22_info() { echo "List backup files"; }
step_22_options() { echo '[TARGET]'; }
step_22_alias() { echo 'list'; }
step_22() {
shift
local ebuTarget="$EBU_TARGET"
if [ -n "${1:-}" ] ; then
ebuTarget="$1"
fi
checkInstalled
setPassphrase
exe $toolBin list-current-files "$ebuTarget"
unsetPassphrase
}
step_70_info() {
echo -n "Manage cron file for "
if contextHelp ; then
echo -n "selected profile"
else
echo -n "profile: $seq_profileName"
fi
echo " [OPTIONS]"
echoinfo " [OPTIONS]"
echoinfo " --remove, -r : remove cron file"
}
step_70_alias() { echo 'cron'; }
step_70_options() { echo '[OPTIONS]'; }
step_70() {
shift
local arg
local cronRemove=0
local cronScript="$toolCronDir/${toolPrefix}$seq_profileName"
local cronLog='>/dev/null'
local cronEntry="$EBU_CRONTIME $(whoami) ${seq_self:?} -qq -p $seq_profileName"
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_profileName}.log"
exe touch "$cronLog"
exe chmod 600 "$cronLog"
fi
cronEntry+=" >$cronLog"
if [ -z "$EBU_CRONTIME" ] || [ $cronRemove -ne 0 ] ; then
info "Removing cron for profile $seq_profileName"
exe rm -r "$cronScript"
else
checkFileHead "$cronScript" "$cronEntry"
if [ $? -ne 0 ] ; then
info "Update cron for profile $seq_profileName"
exep "sudo echo \"$cronEntry\" > \"$cronScript\""
syslogEntry "Cron file update complete [$EBU_CRONTIME]"
else
info "Cron for profile $seq_profileName is up to date"
fi
fi
}
step_72_info() { echo "Update all profile cron files"; }
step_72_alias() { echo "reload"; }
step_72() {
for seq in "$seq_configRoot/"* ; do
seq="$(basename -- "${seq}")"
exe "${seq_self}" -qq -p ${seq%%.*} cron
done
}
step_100_info() { echo "Install $toolName"; }
step_100_alias() { echo "install"; }
step_100() {
exe apt update
exe apt install $toolName ${sq_aptOpt}
}
setPassphrase() {
if [ -z "${PASSPHRASE:-}" ] && [ -n "${EBU_PASSPHRASE:-}" ] ; then
export PASSPHRASE="$EBU_PASSPHRASE"
fi
}
unsetPassphrase() {
unset PASSPHRASE
}
checkFileHead() {
local readChar
if [ ! -e "${1:-}" ] ; then
error -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
if ! command -v $toolName >>/dev/null ; then
step install
fi
toolBin="${EBU_PRECMD:-} $(escpath "$(command -v $toolName)")"
fi
}
# shellcheck disable=SC2034 # Appears unused
readonly sqr_minVersion=16
# shellcheck disable=SC1091 # Don't follow this source
. /usr/local/bin/sequencer.sh