#!/bin/bash toolName=duplicity toolBin= toolPpa="ppa:duplicity-team/duplicity-release-git" toolCronDir="/etc/cron.d" toolPrefix="encBackup_" # 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 exit 1 fi } 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 dupArgs local dupCommand 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 dupCommand="$1" elif [ ! -z "$1" ] ; then echo " [W] $toolName command \"$1\" not recognized" return -1 fi echo " [I] Running backup profile [$SEQ_PROFILE_NAME]" if [ "$dupCommand" != "full" ] && [ ! -z "$EBU_MAX_FULLBKP_AGE" ] ; then dupArgs+="--full-if-older-than=$EBU_MAX_FULLBKP_AGE " fi checkInstalled setPassphrase exe $toolBin $dupCommand $dupArgs "$EBU_SOURCE" "$EBU_TARGET" unsetPassphrase if [ $purgeAfter -ne 0 ] ; then step purge fi } step_3_info() { echo "Restore [TARGET]"; } step_3_alias() { ALIAS="restore"; } step_3() { shift 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 "$EBU_TARGET" "$ebuLocalTarget" unsetPassphrase } step_5_info() { echo "Purge old backups [TARGET]"; } step_5_alias() { ALIAS="purge"; } step_5() { 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() { echo "Status of [TARGET]"; } 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\"" 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_74_info() { echo "Open configuration file"; } step_74_alias() { ALIAS='config'; } step_74() { exe vi "$SEQ_CONFIG_FILE" } 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 } 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