borgmatic/backup.sh - fix printf bug

Interpolation can and does insert % placeholders into the printf
template text - although not valid ones as they're intended for
python.

So be more careful!  Put all inserted text into the parameters to
printf, or use echo.

Also, keep some of the alterations used whilst diagnosing this.
This commit is contained in:
Nick Stokoe
2023-04-15 22:36:57 +01:00
parent bd3ad70af4
commit 86653e5f79

View File

@@ -5,46 +5,47 @@
# - run: create the backup, no checks # - run: create the backup, no checks
# - check: prune, compact and check the backup # - check: prune, compact and check the backup
# Anything else is an error. # Anything else is an error.
set -o pipefail set -o pipefail
# Set up environment # Set up environment
/bin/sh /scripts/msmtprc.sh /bin/sh /scripts/msmtprc.sh
/bin/sh /scripts/env.sh /bin/sh /scripts/env.sh
RUN_COMMAND="borgmatic --stats -v 2 create" RUN_COMMAND="borgmatic --stats -v 2 create"
CHECK_COMMAND="borgmatic --stats -v 1 prune compact check" CHECK_COMMAND="borgmatic --stats -v 1 prune compact check"
LOGFILE="/tmp/backup_run_$(date +%s).log" LOGFILE="/tmp/backup_run_$(date +%s).log"
SUCCESS_PREFIX="=?utf-8?Q? =E2=9C=85 SUCCESS?=" SUCCESS_PREFIX="=?utf-8?Q? =E2=9C=85 SUCCESS?="
FAILED_PREFIX="=?utf-8?Q? =E2=9D=8C FAILED?=" FAILED_PREFIX="=?utf-8?Q? =E2=9D=8C FAILED?="
PARAM="$1" PARAM="$1"
# Helper function to prepend a timestamp and the first parameter to every line of STDIN # Helper function to prepend a timestamp and the first parameter to every line of STDIN
indent() { indent() {
while read -rs line; do while IFS='' read -rs line; do
printf "%s%s%s\n" "$(date -Iminutes)" "${1:- }" "$line" echo "$(date -Iminutes)${1:- }$line"
done done
} }
# This function prepends timestamps to stderr and stdout of the # This function prepends timestamps to stderr and stdout of the
# command supplied as parameters to this. # command supplied as parameters to this.
log() { log() {
# Adapted from https://stackoverflow.com/a/31151808 # Adapted from https://stackoverflow.com/a/31151808
{ {
"$@" 2>&1 1>&3 3>&- | indent " ! " "$@" 2>&1 1>&3 3>&- | indent " ! "
} 3>&1 1>&2 | indent " | " | tee -a "$LOGFILE" } 3>&1 1>&2 | indent " | " | tee -a "$LOGFILE"
} }
report() { report() {
if [ "$RESULT" = "0" ]; then if [ "$RESULT" = "0" ]; then
log echo "SUCCESS" log echo "SUCCESS!"
PREFIX="$SUCCESS_PREFIX" PREFIX="$SUCCESS_PREFIX"
else else
log echo "FAILED: $RESULT" log echo "FAILED: $RESULT"
PREFIX="$FAILED_PREFIX" PREFIX="$FAILED_PREFIX"
fi fi
printf "Subject: $PREFIX: '$PARAM'\n\n$(cat $LOGFILE)\n" | printf "Subject: %s: '%s'\n\n%s\n" "$PREFIX" "$PARAM" "$(cat $LOGFILE)" |
sendmail -t "$MAIL_TO" sendmail -t "$MAIL_TO"
log echo "Report sent."
} }
testmail() { testmail() {
@@ -60,8 +61,9 @@ failed() {
cleanup() { cleanup() {
borgmatic break-lock borgmatic break-lock
echo "Done, removing $LOGFILE" echo "Removing $LOGFILE"
rm -f "$LOGFILE" rm -f "$LOGFILE"
echo "Exiting."
} }
# Handle various kinds of exit # Handle various kinds of exit
@@ -86,9 +88,14 @@ case "$PARAM" in
RESULT=$? RESULT=$?
report report
;; ;;
dummy-run)
log echo STARTED: dummy-run
borgmatic nonesuch
RESULT=$?
report
;;
*) *)
log echo "UNKNOWN COMMAND: '$PARAM'" log echo "UNKNOWN COMMAND: '$PARAM'"
report report
;; ;;
esac esac