diff --git a/usr/local/bin/borgwrapper b/usr/local/bin/borgwrapper index 2a83293..7aa7718 100755 --- a/usr/local/bin/borgwrapper +++ b/usr/local/bin/borgwrapper @@ -15,10 +15,10 @@ usage() { USAGE=$(cat </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"; +# define wrapper functions +backup() { + echo "Using ${BORG_BINARY} as binary"; + borgwrapper_pre_backup; + + 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 "${BORG_BINARY} ${BORGWRAPPER_BORG_INIT_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}"; + + # notify UI + if [ "${BORGWRAPPER_BACKUP_NOTIFY_UI}" = true ]; then + notify-send "Starting backup" ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}::${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT}; + fi + + # create + ${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 + + # use for first time creation + doCheck=false + + if [ ! -f "${BORGWRAPPER_BACKUP_CHECK_FILE}" ]; then + touch ${BORGWRAPPER_BACKUP_CHECK_FILE} + doCheck=true + fi + + # compare date + lastMod=$(date -r ${BORGWRAPPER_BACKUP_CHECK_FILE} +%s) + age=$((${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT} - ${lastMod})) + + if [ "$age" -gt "${BORGWRAPPER_BACKUP_CHECK_MAX_AGE_IN_SECONDS}" ]; then + doCheck=true + fi + + + if [ "$doCheck" = true ]; then + ${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 + ${BORG_BINARY} ${BORGWRAPPER_BORG_PRUNE_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}; + fi + + # log + if [ "${BORGWRAPPER_BACKUP_LOG}" = true ]; then + + if [ "${BORGWRAPPER_BACKUP_LOG_PRUNE}" = true ] && [ -f "${BORGWRAPPER_BACKUP_LOG_FILE}" ]; then + rm ${BORGWRAPPER_BACKUP_LOG_FILE}; + fi + + touch ${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}; + ${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}; + fi + fi + + # notify UI + if [ "${BORGWRAPPER_BACKUP_NOTIFY_UI}" = true ]; then + notify-send "Finished backup" ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}::${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT}; + 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; -# start and info -echo "Using ${BORGWRAPPER_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}"; - -# notify UI -if [ "${BORGWRAPPER_BACKUP_NOTIFY_UI}" = true ]; then - notify-send "Starting backup" ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}::${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT}; -fi - -# create -${BORGWRAPPER_BORG_BINARY} create ${BORGWRAPPER_BORG_CREATE_PARAMS} ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}::${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT} "${BORGWRAPPER_BACKUP_FILES[@]}"; - -# check -if [ "${BORGWRAPPER_BACKUP_CHECK}" = true ]; then - - # use for first time creation - doCheck=false - - if [ ! -f "${BORGWRAPPER_BACKUP_CHECK_FILE}" ]; then - touch ${BORGWRAPPER_BACKUP_CHECK_FILE} - doCheck=true - fi - - # compare date - lastMod=$(date -r ${BORGWRAPPER_BACKUP_CHECK_FILE} +%s) - age=$((${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT} - ${lastMod})) - - if [ "$age" -gt "${BORGWRAPPER_BACKUP_CHECK_MAX_AGE_IN_SECONDS}" ]; then - doCheck=true - fi - - - if [ "$doCheck" = true ]; then - ${BORGWRAPPER_BORG_BINARY} check ${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}; -fi - -# log -if [ "${BORGWRAPPER_BACKUP_LOG}" = true ]; then - - if [ "${BORGWRAPPER_BACKUP_LOG_PRUNE}" = true ] && [ -f "${BORGWRAPPER_BACKUP_LOG_FILE}" ]; then - rm ${BORGWRAPPER_BACKUP_LOG_FILE}; - fi - - touch ${BORGWRAPPER_BACKUP_LOG_FILE}; - ${BORGWRAPPER_BORG_BINARY} list -v ${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}; - - 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}; - fi -fi - -# notify UI -if [ "${BORGWRAPPER_BACKUP_NOTIFY_UI}" = true ]; then - notify-send "Finished backup" ${BORGWRAPPER_BACKUP_REPOSITORY}${BORGWRAPPER_BACKUP_NAME}::${BORGWRAPPER_BACKUP_TIMESTAMP_FORMAT}; -fi - -borgwrapper_post_backup; \ No newline at end of file +COMMAND="backup" +case "$COMMAND" in + backup) + backup; + ;; + *) + echo "Unsupported command or no command given" >&2 + usage; + exit 1 + ;; +esac \ No newline at end of file