Current File : //usr/local/jetapps/usr/share/rear/backup/DUPLICITY/default/500_make_duplicity_backup.sh
# This file is part of Relax-and-Recover, licensed under the GNU General
# Public License. Refer to the included COPYING for full text of license.

if [ "$BACKUP_PROG" = "duply" ] ; then

    has_binary duply || Error "No 'duply' binary (BACKUP_PROG=duply)"

    # error out when DUPLY_PROFILE is empty or does not exist
    # cf. the same test in prep/DUPLICITY/default/200_find_duply_profile.sh
    test -s "$DUPLY_PROFILE" || Error "DUPLY_PROFILE '$DUPLY_PROFILE' empty or does not exist (BACKUP_PROG=duply)"

    # do the backup
    LogPrint "Starting full backup with duply/duplicity"
    # command stdout and stderr output appears in the ReaR logfile only in debug modes,
    # cf. https://github.com/rear/rear/wiki/Coding-Style#what-to-do-with-stdin-stdout-and-stderr
    DebugPrint "Calling 'duply $DUPLY_PROFILE backup'"
    Debug "'duply $DUPLY_PROFILE backup' output:"
    duply "$DUPLY_PROFILE" backup || Error "'duply $DUPLY_PROFILE backup' returned non-zero exit code, check $RUNTIME_LOGFILE"
fi


if [ "$BACKUP_PROG" = "duplicity" ] ; then

    # make backup using the DUPLICITY method with duplicity
    # by falk hoeppner

    if is_true "$BACKUP_DUPLICITY_ASK_PASSPHRASE"; then
        LogPrint "Duplicity passphrase must be provided interactively on restore (BACKUP_DUPLICITY_ASK_PASSPHRASE is set)"
    fi

    LogPrint "Creating $BACKUP_PROG archives on '$BACKUP_DUPLICITY_URL'"

    # todo: check parameters
    BKP_URL="$BACKUP_DUPLICITY_URL"

    DUP_OPTIONS="$BACKUP_DUPLICITY_OPTIONS"

    if [ -n "$BACKUP_DUPLICITY_FULL_IF_OLDER_THAN" ]; then
        FULL_BACKUP="--full-if-older-than $BACKUP_DUPLICITY_FULL_IF_OLDER_THAN"
    fi

    if [ -n "$BACKUP_DUPLICITY_GPG_ENC_KEY" ]; then
        GPG_KEY="--encrypt-key $BACKUP_DUPLICITY_GPG_ENC_KEY"
    fi
    PASSPHRASE="$BACKUP_DUPLICITY_GPG_ENC_PASSPHRASE"

    if ! is_true "$BACKUP_DUPLICITY_EXCLUDE_EVALUATE_BY_SHELL"; then
        set -f # Temporarily Stop Evaluation of Patterns By the Shell
    fi
    # Disable SC2068: "Double quote array expansions to avoid re-splitting elements."
    # because whether or not "Evaluation of Patterns By the Shell" (globbing) happens
    # is controlled by BACKUP_DUPLICITY_EXCLUDE_EVALUATE_BY_SHELL
    # shellcheck disable=SC2068
    for EXDIR in ${BACKUP_DUPLICITY_EXCLUDE[@]} ; do
        EXCLUDES+=" --exclude $EXDIR"
    done
    if ! is_true "$BACKUP_DUPLICITY_EXCLUDE_EVALUATE_BY_SHELL"; then
        set +f # Re-enable Evaluation of Patterns By the Shell
    fi

    LogUserOutput "EXCLUDES = $EXCLUDES"

    # Setting the pass phrase to encrypt the backup files
    export PASSPHRASE

    # check and create directory at backup-server
    # if the target-directory don't exist, duplicity will fail
    # Note: this is only working if we use duplicity with ssh/rsync and the
    # given user is allowed to create directories/files this way !!
    # maybe better done in an if or case statement
    #
    if [[ $BKP_URL == ssh://* ]] || [[ $BKP_URL == rsync://* ]] || [[ $BKP_URL == fish://* ]] ; then
        LogPrint "Checking backup-path at server ..."
        ssh ${DUPLICITY_USER}@${DUPLICITY_HOST} "test -d ${DUPLICITY_PATH}/${HOSTNAME} || mkdir -p ${DUPLICITY_PATH}/${HOSTNAME}"
    fi

    # First remove old Backups
    # --force Is needed to actually remove them
    if [ -n "$BACKUP_DUPLICITY_MAX_SETS" ] ; then
        LogPrint "Removing the old Backups from the Server with CMD:
    $DUPLICITY_PROG remove-all-but-n-full --name $BACKUP_DUPLICITY_NAME --force $BACKUP_DUPLICITY_MAX_SETS -v5 $BKP_URL/$HOSTNAME"
        $DUPLICITY_PROG remove-all-but-n-full --name $BACKUP_DUPLICITY_NAME --force $BACKUP_DUPLICITY_MAX_SETS -v5 $BKP_URL/$HOSTNAME >> ${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log
    fi

    if [ -n "$BACKUP_DUPLICITY_MAX_SETS_KEEP_FULL" ] ; then
        LogPrint "Removing the old Backups from the Server with CMD:
    $DUPLICITY_PROG remove-all-inc-of-but-n-full --name $BACKUP_DUPLICITY_NAME --force $BACKUP_DUPLICITY_MAX_SETS_KEEP_FULL -v5 $BKP_URL/$HOSTNAME"
        $DUPLICITY_PROG remove-all-inc-of-but-n-full --name $BACKUP_DUPLICITY_NAME --force $BACKUP_DUPLICITY_MAX_SETS_KEEP_FULL -v5 $BKP_URL/$HOSTNAME >> ${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log
    fi

    if [ -n "$BACKUP_DUPLICITY_MAX_TIME" ] ; then
        LogPrint "Removing the old Backups from the Server with CMD:
    $DUPLICITY_PROG remove-older-than --name $BACKUP_DUPLICITY_NAME --force $BACKUP_DUPLICITY_MAX_TIME -v5 $BKP_URL/$HOSTNAME"
        $DUPLICITY_PROG remove-older-than --name $BACKUP_DUPLICITY_NAME --force $BACKUP_DUPLICITY_MAX_TIME -v5 $BKP_URL/$HOSTNAME >> ${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log
    fi

    if ! is_true "$BACKUP_DUPLICITY_EXCLUDE_EVALUATE_BY_SHELL"; then
        set -f # Temporarily Stop Evaluation of Patterns By the Shell
    fi

    # do the backup
    if [[ "$BACKUP_DUPLICITY_GPG_OPTIONS" ]] ; then
        LogPrint "Running CMD: $DUPLICITY_PROG -v5 --name $BACKUP_DUPLICITY_NAME $FULL_BACKUP $DUP_OPTIONS $GPG_KEY --gpg-options ${BACKUP_DUPLICITY_GPG_OPTIONS} $EXCLUDES / $BKP_URL/$HOSTNAME >> ${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log "
        $DUPLICITY_PROG -v5 --name $BACKUP_DUPLICITY_NAME $FULL_BACKUP $DUP_OPTIONS $GPG_KEY --gpg-options "${BACKUP_DUPLICITY_GPG_OPTIONS}" $EXCLUDES \
           / $BKP_URL/$HOSTNAME >> ${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log 2>&1
    else
        LogPrint "Running CMD: $DUPLICITY_PROG -v5 --name $BACKUP_DUPLICITY_NAME $FULL_BACKUP $DUP_OPTIONS $GPG_KEY $EXCLUDES / $BKP_URL/$HOSTNAME >> ${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log "
        $DUPLICITY_PROG -v5 --name $BACKUP_DUPLICITY_NAME $FULL_BACKUP $DUP_OPTIONS $GPG_KEY $EXCLUDES \
           / $BKP_URL/$HOSTNAME >> ${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log 2>&1
    fi

    if ! is_true "$BACKUP_DUPLICITY_EXCLUDE_EVALUATE_BY_SHELL"; then
        set +f # Re-enable Evaluation of Patterns By the Shell
    fi

    RC_DUP=$?

    sleep 1
    LOGAUSZUG=$(tail -n 18 ${TMP_DIR}/${BACKUP_PROG_ARCHIVE}.log)
    LogPrint "${LOGAUSZUG}"

    # everyone should see this warning, even if not verbose
    if [ $RC_DUP -gt 0 ] ; then
        VERBOSE=1
        LogPrint "WARNING !
    There was an error during archive creation.
    Please check the archive and see '$RUNTIME_LOGFILE' for more information.

    Since errors are often related to files that cannot be saved by
    $BACKUP_PROG, we will continue the $WORKFLOW process. However, you MUST
    verify the backup yourself before trusting it !
    "
        LogPrint "$LOGAUSZUG"
    fi
fi