Fix an error in if statements in borgwrapper
This commit is contained in:
parent
4d993ba545
commit
61c0122742
1 changed files with 137 additions and 111 deletions
|
@ -15,10 +15,10 @@ usage() {
|
|||
USAGE=$(cat <<EOF
|
||||
Usage: borgwrapper [CONFIG_FILE (absolute path)]
|
||||
|
||||
A script which wraps commonly used functions for borg.
|
||||
A script which wraps the most common use case for borg: creating a backup and sending a notification.
|
||||
|
||||
If no CONFIG_FILE is given, HOME/.borgwrapper.conf is used. This fallback option
|
||||
has to exist or the script will exit.
|
||||
If no CONFIG_FILE is given, \$HOME/.borgwrapper.conf is used. This fallback option
|
||||
has to exist if no other CONFIG_FILE exists. Otherwise the script exits.
|
||||
|
||||
Configuration can be done in any file and any pre-defined variable can be overwritten.
|
||||
|
||||
|
@ -30,26 +30,28 @@ The following are at least required for the script to work:
|
|||
The following are optional or have reasonable defaults:
|
||||
- BORGWRAPPER_BACKUP_PASSWORD='' // the password
|
||||
- BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT=$(date "+%s")
|
||||
- BORGWRAPPER_BACKUP_PRUNE=true // prune the repository (cleans old backups; uses BORGWRAPPER_BACKUP_KEEP_IN_DAYS)? false|true
|
||||
- BORGWRAPPER_BACKUP_PRUNE=true // prune the repository (cleans old backups; uses \$BORGWRAPPER_BACKUP_KEEP_IN_DAYS)? false|true
|
||||
- BORGWRAPPER_BACKUP_KEEP_IN_DAYS=60 // in days, integer only (used for prune)
|
||||
- BORGWRAPPER_BACKUP_CHECK=true // checks entire repository, this is slow! false|true
|
||||
- BORGWRAPPER_BACKUP_CHECK_MAX_AGE_IN_SECONDS=604800 // determine when "borg check" has to run by comparing current time with latest run check (determined by last mod time of $BORGWRAPPER_BACKUP_CHECK_FILE). default is 7 days, 0 will always run the check
|
||||
- BORGWRAPPER_BACKUP_CHECK_FILE="~/.borg-backup-BORGWRAPPER_BACKUP_NAME.check" // used for time comparison
|
||||
- BORGWRAPPER_BACKUP_CHECK_MAX_AGE_IN_SECONDS=604800 // determine when "borg check" has to run by comparing current time with latest run check (determined by last mod time of \$BORGWRAPPER_BACKUP_CHECK_FILE). default is 7 days, 0 will always run the check
|
||||
- BORGWRAPPER_BACKUP_CHECK_FILE="~/.borgwrapper-\$BORGWRAPPER_BACKUP_NAME.check" // used for time comparison
|
||||
- BORGWRAPPER_BACKUP_LOG=true // log? false|true
|
||||
- BORGWRAPPER_BACKUP_LOG_PRUNE=true // overwrite old log for each execution? false|true
|
||||
- BORGWRAPPER_BACKUP_LOG_FILE="~/.borg-backup-BORGWRAPPER_BACKUP_NAME.log" // log file to save output to to
|
||||
- BORGWRAPPER_BACKUP_NOTIFY_VIA_MAIL=false // send email (BORGWRAPPER_BACKUP_LOG has to be set to true)? false|true
|
||||
- BORGWRAPPER_BACKUP_LOG_FILE="~/.borgwrapper-\$BORGWRAPPER_BACKUP_NAME.log" // log file to save output to to
|
||||
- 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_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:
|
||||
- BORGWRAPPER_BORG_BINARY=borg; // defaults to /usr/bin/borg (determined by 'which borg'), adjust to a virtualenv if needed
|
||||
- BORGWRAPPER_BORG_INIT_PARAMS="--encryption=BORGWRAPPER_BACKUP_ENCRYPTION"; // encryption=none|repokey-blake2|...
|
||||
- BORGWRAPPER_BORG_CREATE_PARAMS="-v -s -p -C BORGWRAPPER_BACKUP_COMPRESSION";
|
||||
- BORGWRAPPER_BORG_CHECK_PARAMS="-v";
|
||||
- BORGWRAPPER_BORG_PRUNE_PARAMS="-v -s -d BORGWRAPPER_BACKUP_KEEP_IN_DAYS";
|
||||
- BORG_BINARY=borg; // defaults to /usr/bin/borg (determined by 'which borg'), adjust to a virtualenv if needed
|
||||
- BORGWRAPPER_BORG_INIT_PARAMS="init --encryption=\$BORGWRAPPER_BACKUP_ENCRYPTION"; // encryption=none|repokey-blake2|...
|
||||
- BORGWRAPPER_BORG_CREATE_PARAMS="create -v -s -p -C \$BORGWRAPPER_BACKUP_COMPRESSION";
|
||||
- BORGWRAPPER_BORG_CHECK_PARAMS="check -v";
|
||||
- BORGWRAPPER_BORG_PRUNE_PARAMS="prune -v -s -d \$BORGWRAPPER_BACKUP_KEEP_IN_DAYS";
|
||||
- BORGWRAPPER_BORG_LIST_PARAMS="list -v";
|
||||
- BORGWRAPPER_BORG_INFO_PARAMS="info";
|
||||
|
||||
More information can be found in the docs: https://borgbackup.readthedocs.io
|
||||
|
||||
|
@ -86,45 +88,49 @@ source_config() {
|
|||
set +a;
|
||||
}
|
||||
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}";
|
||||
[[ -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/.borgwrapper-${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/.borgwrapper-${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=none;
|
||||
[[ -z ${BORGWRAPPER_BACKUP_ENCRYPTION} ]] && BORGWRAPPER_BACKUP_ENCRYPTION=none;
|
||||
[[ -z ${BORG_BINARY} ]] && BORG_BINARY=$(which borg);
|
||||
[[ -z ${BORGWRAPPER_BORG_INIT_PARAMS} ]] && BORGWRAPPER_BORG_INIT_PARAMS="init --encryption=${BORGWRAPPER_BACKUP_ENCRYPTION}";
|
||||
[[ -z ${BORGWRAPPER_BORG_CREATE_PARAMS} ]] && BORGWRAPPER_BORG_CREATE_PARAMS="create -v -s -p -C ${BORGWRAPPER_BACKUP_COMPRESSION}";
|
||||
[[ -z ${BORGWRAPPER_BORG_CHECK_PARAMS} ]] && BORGWRAPPER_BORG_CHECK_PARAMS="check -v";
|
||||
[[ -z ${BORGWRAPPER_BORG_PRUNE_PARAMS} ]] && BORGWRAPPER_BORG_PRUNE_PARAMS="prune -v -s -d ${BORGWRAPPER_BACKUP_KEEP_IN_DAYS}";
|
||||
[[ -z ${BORGWRAPPER_BORG_LIST_PARAMS} ]] && BORGWRAPPER_BORG_LIST_PARAMS="list -v";
|
||||
[[ -z ${BORGWRAPPER_BORG_INFO_PARAMS} ]] && BORGWRAPPER_BORG_INFO_PARAMS="info";
|
||||
}
|
||||
check_required() {
|
||||
if [[ -z ${BORGWRAPPER_BACKUP_NAME} ]]; then
|
||||
echo "BORGWRAPPER_BACKUP_NAME is required"
|
||||
exit 1;
|
||||
fi
|
||||
type ${BORG_BINARY} >/dev/null 2>&1 || { echo >&2 "Require '${BORG_BINARY}' (binary) but it's not installed. Aborting."; exit 1; }
|
||||
}
|
||||
|
||||
# check for config file and use it
|
||||
source_config "$1" "${HOME}/.borgwrapper.conf";
|
||||
apply_defaults;
|
||||
|
||||
# start and info
|
||||
echo "Using ${BORGWRAPPER_BORG_BINARY} as binary";
|
||||
|
||||
# define wrapper functions
|
||||
backup() {
|
||||
echo "Using ${BORG_BINARY} as binary";
|
||||
borgwrapper_pre_backup;
|
||||
|
||||
# export passphrase if not blank
|
||||
if [ ! -z "${BORGWRAPPER_BACKUP_PASSWORD}" ]; then
|
||||
export BORG_PASSPHRASE=${BORGWRAPPER_BACKUP_PASSWORD};
|
||||
fi
|
||||
|
||||
# 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 "${BORGWRAPPER_BORG_BINARY} init ${BORGWRAPPER_BORG_INIT_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}";
|
||||
echo "${BORG_BINARY} ${BORGWRAPPER_BORG_INIT_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}";
|
||||
|
||||
# notify UI
|
||||
if [ "${BORGWRAPPER_BACKUP_NOTIFY_UI}" = true ]; then
|
||||
|
@ -132,7 +138,7 @@ if [ "${BORGWRAPPER_BACKUP_NOTIFY_UI}" = true ]; then
|
|||
fi
|
||||
|
||||
# create
|
||||
${BORGWRAPPER_BORG_BINARY} create ${BORGWRAPPER_BORG_CREATE_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}::${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT} "${BORGWRAPPER_BACKUP_FILES[@]}";
|
||||
${BORG_BINARY} ${BORGWRAPPER_BORG_CREATE_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}::${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT} "${BORGWRAPPER_BACKUP_FILES[@]}";
|
||||
|
||||
# check
|
||||
if [ "${BORGWRAPPER_BACKUP_CHECK}" = true ]; then
|
||||
|
@ -155,14 +161,14 @@ if [ "${BORGWRAPPER_BACKUP_CHECK}" = true ]; then
|
|||
|
||||
|
||||
if [ "$doCheck" = true ]; then
|
||||
${BORGWRAPPER_BORG_BINARY} check ${BORGWRAPPER_BORG_CHECK_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME};
|
||||
${BORG_BINARY} ${BORGWRAPPER_BORG_CHECK_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME};
|
||||
touch ${BORGWRAPPER_BACKUP_CHECK_FILE}
|
||||
fi
|
||||
fi
|
||||
|
||||
# prune
|
||||
if [ "${BORGWRAPPER_BACKUP_PRUNE}" = true ]; then
|
||||
${BORGWRAPPER_BORG_BINARY} prune ${BORGWRAPPER_BORG_PRUNE_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME};
|
||||
${BORG_BINARY} ${BORGWRAPPER_BORG_PRUNE_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME};
|
||||
fi
|
||||
|
||||
# log
|
||||
|
@ -173,9 +179,9 @@ if [ "${BORGWRAPPER_BACKUP_LOG}" = true ]; then
|
|||
fi
|
||||
|
||||
touch ${BORGWRAPPER_BACKUP_LOG_FILE};
|
||||
${BORGWRAPPER_BORG_BINARY} list -v ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME} >> ${BORGWRAPPER_BACKUP_LOG_FILE};
|
||||
${BORG_BINARY} ${BORGWRAPPER_BORG_LIST_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME} >> ${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};
|
||||
${BORG_BINARY} ${BORGWRAPPER_BORG_INFO_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}::${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT} >> ${BORGWRAPPER_BACKUP_LOG_FILE};
|
||||
|
||||
if [ "${BORGWRAPPER_BACKUP_NOTIFY_VIA_MAIL}" = true ]; then
|
||||
cat ${BORGWRAPPER_BACKUP_LOG_FILE}|mailx -Ssendwait -s "[borgwrapper ${BORGWRAPPER_BACKUP_NAME}]" ${BORGWRAPPER_BACKUP_NOTIFY_MAIL_ADDRESS};
|
||||
|
@ -188,3 +194,23 @@ if [ "${BORGWRAPPER_BACKUP_NOTIFY_UI}" = true ]; then
|
|||
fi
|
||||
|
||||
borgwrapper_post_backup;
|
||||
}
|
||||
|
||||
# check requirements
|
||||
CONFIG_FILE="$1";
|
||||
FALLBACK_CONFIG_FILE="${HOME}/.borgwrapper.conf";
|
||||
source_config "${CONFIG_FILE}" "${FALLBACK_CONFIG_FILE}";
|
||||
apply_defaults;
|
||||
check_required;
|
||||
|
||||
COMMAND="backup"
|
||||
case "$COMMAND" in
|
||||
backup)
|
||||
backup;
|
||||
;;
|
||||
*)
|
||||
echo "Unsupported command or no command given" >&2
|
||||
usage;
|
||||
exit 1
|
||||
;;
|
||||
esac
|
Loading…
Reference in a new issue