Fix an error in borgwrapper where borgwrapper files might be created without suffixed repo name

This commit is contained in:
Alexander Schäferdiek 2019-08-11 22:08:25 +02:00
parent cb86176e9f
commit 511594db57

View file

@ -41,11 +41,13 @@ The following are optional or have reasonable defaults:
- BORGWRAPPER_BACKUP_NOTIFY_VIA_MAIL=false // send email (BORGWRAPPER_BACKUP_LOG has to be set to true)? false|true - BORGWRAPPER_BACKUP_NOTIFY_VIA_MAIL=false // send email (BORGWRAPPER_BACKUP_LOG has to be set to true)? false|true
- BORGWRAPPER_BACKUP_NOTIFY_MAIL_ADDRESS="" // mail.rc has to be configured - BORGWRAPPER_BACKUP_NOTIFY_MAIL_ADDRESS="" // mail.rc has to be configured
- BORGWRAPPER_BACKUP_NOTIFY_UI=false // use notify-send for UI notification? false|true - BORGWRAPPER_BACKUP_NOTIFY_UI=false // use notify-send for UI notification? false|true
- BORGWRAPPER_BACKUP_COMPRESSION="lz4" // compress repository
- BORGWRAPPER_BACKUP_ENCRYPTION="none" // encrypt repository
Borg specific values can also be overwritten and have reasonable defaults: Borg specific values can also be overwritten and have reasonable defaults:
- BORGWRAPPER_BORG_BINARY=borg; // defaults to /usr/bin/borg (determined by 'which borg'), adjust to a virtualenv if needed - BORGWRAPPER_BORG_BINARY=borg; // defaults to /usr/bin/borg (determined by 'which borg'), adjust to a virtualenv if needed
- BORGWRAPPER_BORG_INIT_PARAMS="--encryption=none"; // encryption=none|repokey-blake2|... - BORGWRAPPER_BORG_INIT_PARAMS="--encryption=BORGWRAPPER_BACKUP_ENCRYPTION"; // encryption=none|repokey-blake2|...
- BORGWRAPPER_BORG_CREATE_PARAMS="-v -s -p -C lz4"; - BORGWRAPPER_BORG_CREATE_PARAMS="-v -s -p -C BORGWRAPPER_BACKUP_COMPRESSION";
- BORGWRAPPER_BORG_CHECK_PARAMS="-v"; - BORGWRAPPER_BORG_CHECK_PARAMS="-v";
- BORGWRAPPER_BORG_PRUNE_PARAMS="-v -s -d BORGWRAPPER_BACKUP_KEEP_IN_DAYS"; - BORGWRAPPER_BORG_PRUNE_PARAMS="-v -s -d BORGWRAPPER_BACKUP_KEEP_IN_DAYS";
@ -63,123 +65,126 @@ EOF
set -e; set -e;
# vars and defaults # helper functions
BORGWRAPPER_BACKUP_PASSWORD=''
BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT=$(date "+%s")
BORGWRAPPER_BACKUP_PRUNE=true
BORGWRAPPER_BACKUP_KEEP_IN_DAYS=60
BORGWRAPPER_BACKUP_CHECK=true
BORGWRAPPER_BACKUP_CHECK_MAX_AGE_IN_SECONDS=604800
BORGWRAPPER_BACKUP_CHECK_FILE="$HOME/.borg-backup-$BORGWRAPPER_BACKUP_NAME.check"
BORGWRAPPER_BACKUP_LOG=true
BORGWRAPPER_BACKUP_LOG_PRUNE=true
BORGWRAPPER_BACKUP_LOG_FILE="$HOME/.borg-backup-$BORGWRAPPER_BACKUP_NAME.log"
BORGWRAPPER_BACKUP_NOTIFY_VIA_MAIL=false
BORGWRAPPER_BACKUP_NOTIFY_MAIL_ADDRESS=""
BORGWRAPPER_BACKUP_NOTIFY_UI=false
BORGWRAPPER_BORG_BINARY=$(which borg);
BORGWRAPPER_BORG_INIT_PARAMS="--encryption=none";
BORGWRAPPER_BORG_CREATE_PARAMS="-v -s -p -C lz4";
BORGWRAPPER_BORG_CHECK_PARAMS="-v";
BORGWRAPPER_BORG_PRUNE_PARAMS="-v -s -d $BORGWRAPPER_BACKUP_KEEP_IN_DAYS";
# check for config file
source_config() { source_config() {
local config=$1; local config=$1;
local configFallback=$2; local configFallback=$2;
if [[ ! -f "$config" ]]; then if [[ ! -f "$config" ]]; then
if [[ ! -f "$configFallback" ]]; then if [[ ! -f "$configFallback" ]]; then
echo "No config file specified and could not find default in '$configFallback'!"; echo "No config file specified and could not find default in '${configFallback}'!";
echo ""; echo "";
usage; usage;
exit 1; exit 1;
else else
config=$configFallback; config=${configFallback};
fi fi
fi fi
set -a; set -a;
source "$config"; source "${config}";
set +a; set +a;
} }
source_config "$1" "$HOME/.borgwrapper.conf" apply_defaults() {
[ -z ${BORGWRAPPER_BACKUP_PASSWORD} ] && BORGWRAPPER_BACKUP_PASSWORD='';
[ -z ${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT} ] && BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT=$(date "+%s");
[ -z ${BORGWRAPPER_BACKUP_PRUNE} ] && BORGWRAPPER_BACKUP_PRUNE=true;
[ -z ${BORGWRAPPER_BACKUP_KEEP_IN_DAYS} ] && BORGWRAPPER_BACKUP_KEEP_IN_DAYS=60;
[ -z ${BORGWRAPPER_BACKUP_CHECK} ] && BORGWRAPPER_BACKUP_CHECK=true;
[ -z ${BORGWRAPPER_BACKUP_CHECK_MAX_AGE_IN_SECONDS} ] && BORGWRAPPER_BACKUP_CHECK_MAX_AGE_IN_SECONDS=604800;
[ -z ${BORGWRAPPER_BACKUP_CHECK_FILE} ] && BORGWRAPPER_BACKUP_CHECK_FILE="$HOME/.borg-backup-${BORGWRAPPER_BACKUP_NAME}.check";
[ -z ${BORGWRAPPER_BACKUP_LOG} ] && BORGWRAPPER_BACKUP_LOG=true;
[ -z ${BORGWRAPPER_BACKUP_LOG_PRUNE} ] && BORGWRAPPER_BACKUP_LOG_PRUNE=true;
[ -z ${BORGWRAPPER_BACKUP_LOG_FILE} ] && BORGWRAPPER_BACKUP_LOG_FILE="$HOME/.borg-backup-${BORGWRAPPER_BACKUP_NAME}.log";
[ -z ${BORGWRAPPER_BACKUP_NOTIFY_VIA_MAIL} ] && BORGWRAPPER_BACKUP_NOTIFY_VIA_MAIL=false;
[ -z ${BORGWRAPPER_BACKUP_NOTIFY_MAIL_ADDRESS} ] && BORGWRAPPER_BACKUP_NOTIFY_MAIL_ADDRESS="";
[ -z ${BORGWRAPPER_BACKUP_NOTIFY_UI} ] && BORGWRAPPER_BACKUP_NOTIFY_UI=false;
[ -z ${BORGWRAPPER_BACKUP_COMPRESSION} ] && BORGWRAPPER_BACKUP_COMPRESSION=lz4;
[ -z ${BORGWRAPPER_BACKUP_ENCRYPTION} ] && BORGWRAPPER_BACKUP_ENCRYPTION=none;
[ -z ${BORGWRAPPER_BORG_BINARY} ] && BORGWRAPPER_BORG_BINARY=$(which borg);
[ -z ${BORGWRAPPER_BORG_INIT_PARAMS} ] && BORGWRAPPER_BORG_INIT_PARAMS="--encryption=${BORGWRAPPER_BACKUP_ENCRYPTION}";
[ -z ${BORGWRAPPER_BORG_CREATE_PARAMS} ] && BORGWRAPPER_BORG_CREATE_PARAMS="-v -s -p -C ${BORGWRAPPER_BACKUP_COMPRESSION}";
[ -z ${BORGWRAPPER_BORG_CHECK_PARAMS} ] && BORGWRAPPER_BORG_CHECK_PARAMS="-v";
[ -z ${BORGWRAPPER_BORG_PRUNE_PARAMS} ] && BORGWRAPPER_BORG_PRUNE_PARAMS="-v -s -d ${BORGWRAPPER_BACKUP_KEEP_IN_DAYS}";
}
# check for config file and use it
source_config "$1" "${HOME}/.borgwrapper.conf";
apply_defaults;
# start and info # start and info
echo "Using $BORGWRAPPER_BORG_BINARY as binary" echo "Using ${BORGWRAPPER_BORG_BINARY} as binary";
borgwrapper_pre_backup; borgwrapper_pre_backup;
# export passphrase if not blank # export passphrase if not blank
if [ ! -z "$BORGWRAPPER_BACKUP_PASSWORD" ]; then if [ ! -z "${BORGWRAPPER_BACKUP_PASSWORD}" ]; then
export BORG_PASSPHRASE=$BORGWRAPPER_BACKUP_PASSWORD export BORG_PASSPHRASE=${BORGWRAPPER_BACKUP_PASSWORD};
fi fi
# init hint # init hint
echo "Hint: If you haven't, you need to create the borg repository manually before first run if it doesn't exist:"; echo "Hint: If you haven't, you need to create the borg repository manually before first run if it doesn't exist:";
echo "$BORGWRAPPER_BORG_BINARY init $BORGWRAPPER_BORG_INIT_PARAMS $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME"; echo "${BORGWRAPPER_BORG_BINARY} init ${BORGWRAPPER_BORG_INIT_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}";
# notify UI # notify UI
if [ "$BORGWRAPPER_BACKUP_NOTIFY_UI" = true ]; then if [ "${BORGWRAPPER_BACKUP_NOTIFY_UI}" = true ]; then
notify-send "Starting backup" $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME::$BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT; notify-send "Starting backup" ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}::${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT};
fi fi
# create # create
$BORGWRAPPER_BORG_BINARY create $BORGWRAPPER_BORG_CREATE_PARAMS $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME::$BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT "${BORGWRAPPER_BACKUP_FILES[@]}"; ${BORGWRAPPER_BORG_BINARY} create ${BORGWRAPPER_BORG_CREATE_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}::${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT} "${BORGWRAPPER_BACKUP_FILES[@]}";
# check # check
if [ "$BORGWRAPPER_BACKUP_CHECK" = true ]; then if [ "${BORGWRAPPER_BACKUP_CHECK}" = true ]; then
# use for first time creation # use for first time creation
doCheck=false doCheck=false
if [ ! -f "$BORGWRAPPER_BACKUP_CHECK_FILE" ]; then if [ ! -f "${BORGWRAPPER_BACKUP_CHECK_FILE}" ]; then
touch $BORGWRAPPER_BACKUP_CHECK_FILE touch ${BORGWRAPPER_BACKUP_CHECK_FILE}
doCheck=true doCheck=true
fi fi
# compare date # compare date
lastMod=$(date -r $BORGWRAPPER_BACKUP_CHECK_FILE +%s) lastMod=$(date -r ${BORGWRAPPER_BACKUP_CHECK_FILE} +%s)
age=$(($BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT - $lastMod)) age=$((${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT} - ${lastMod}))
if [ "$age" -gt "$BORGWRAPPER_BACKUP_CHECK_MAX_AGE_IN_SECONDS" ]; then if [ "$age" -gt "${BORGWRAPPER_BACKUP_CHECK_MAX_AGE_IN_SECONDS}" ]; then
doCheck=true doCheck=true
fi fi
if [ "$doCheck" = true ]; then if [ "$doCheck" = true ]; then
$BORGWRAPPER_BORG_BINARY check $BORGWRAPPER_BORG_CHECK_PARAMS $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME; ${BORGWRAPPER_BORG_BINARY} check ${BORGWRAPPER_BORG_CHECK_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME};
touch $BORGWRAPPER_BACKUP_CHECK_FILE touch ${BORGWRAPPER_BACKUP_CHECK_FILE}
fi fi
fi fi
# prune # prune
if [ "$BORGWRAPPER_BACKUP_PRUNE" = true ]; then if [ "${BORGWRAPPER_BACKUP_PRUNE}" = true ]; then
$BORGWRAPPER_BORG_BINARY prune $BORGWRAPPER_BORG_PRUNE_PARAMS $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME; ${BORGWRAPPER_BORG_BINARY} prune ${BORGWRAPPER_BORG_PRUNE_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME};
fi fi
# log # log
if [ "$BORGWRAPPER_BACKUP_LOG" = true ]; then if [ "${BORGWRAPPER_BACKUP_LOG}" = true ]; then
if [ "$BORGWRAPPER_BACKUP_LOG_PRUNE" = true ] && [ -f "$BORGWRAPPER_BACKUP_LOG_FILE" ]; then if [ "${BORGWRAPPER_BACKUP_LOG_PRUNE}" = true ] && [ -f "${BORGWRAPPER_BACKUP_LOG_FILE}" ]; then
rm $BORGWRAPPER_BACKUP_LOG_FILE; rm ${BORGWRAPPER_BACKUP_LOG_FILE};
fi fi
touch $BORGWRAPPER_BACKUP_LOG_FILE; touch ${BORGWRAPPER_BACKUP_LOG_FILE};
$BORGWRAPPER_BORG_BINARY list -v $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME >> $BORGWRAPPER_BACKUP_LOG_FILE; ${BORGWRAPPER_BORG_BINARY} list -v ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME} >> ${BORGWRAPPER_BACKUP_LOG_FILE};
echo "---" >> $BORGWRAPPER_BACKUP_LOG_FILE; echo "---" >> ${BORGWRAPPER_BACKUP_LOG_FILE};
$BORGWRAPPER_BORG_BINARY info $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME::$BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT >> $BORGWRAPPER_BACKUP_LOG_FILE; ${BORGWRAPPER_BORG_BINARY} info ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}::${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT} >> ${BORGWRAPPER_BACKUP_LOG_FILE};
if [ "$BORGWRAPPER_BACKUP_NOTIFY_VIA_MAIL" = true ]; then if [ "${BORGWRAPPER_BACKUP_NOTIFY_VIA_MAIL}" = true ]; then
cat $BORGWRAPPER_BACKUP_LOG_FILE|mailx -Ssendwait -s "[backup $BORGWRAPPER_BACKUP_NAME]" $BORGWRAPPER_BACKUP_NOTIFY_MAIL_ADDRESS; cat ${BORGWRAPPER_BACKUP_LOG_FILE}|mailx -Ssendwait -s "[borgwrapper ${BORGWRAPPER_BACKUP_NAME}]" ${BORGWRAPPER_BACKUP_NOTIFY_MAIL_ADDRESS};
fi fi
fi fi
# notify UI # notify UI
if [ "$BORGWRAPPER_BACKUP_NOTIFY_UI" = true ]; then if [ "${BORGWRAPPER_BACKUP_NOTIFY_UI}" = true ]; then
notify-send "Finished backup" $BORGWRAPPER_BACKUP_REPOSITORY$BORGWRAPPER_BACKUP_NAME::$BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT; notify-send "Finished backup" ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}::${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT};
fi fi
borgwrapper_post_backup; borgwrapper_post_backup;