From 777add8b775e36d8ebbe057ad84e8b691c7e86c7 Mon Sep 17 00:00:00 2001 From: Wynn Wolf Arbor Date: Thu, 4 Jun 2020 17:16:46 +0200 Subject: Simplify and improve sync_default sync_default() suffers from needlessly complicated logic in its threshold test. Join the two if clauses together and skip targets cleanly by using `continue` instead. Where possible, use OR lists instead of if clauses and also inline the whole sync_single() function, which was only called from here. --- lifeboat | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lifeboat b/lifeboat index 4ce396c..ec13352 100755 --- a/lifeboat +++ b/lifeboat @@ -4,8 +4,12 @@ set -e LIFEBOAT_ROOT=${LIFEBOAT_ROOT:-"/srv/backup/lifeboat"} -errx() { +err() { printf "lifeboat: %s\n" "$1" >&2 +} + +errx() { + err "$1" exit 1 } @@ -42,28 +46,21 @@ clean_default() { } sync_default() { - if ! test -r config/sync; then - return - fi + test -r config/sync || return while read -r target_spec; do target=$(echo "$target_spec" | cut -f1) limit=$(echo "$target_spec" | cut -sf2) - if test -n "$limit"; then - if test "$(du -s "$RESTIC_REPOSITORY" | cut -f1)" -gt "$limit"; then - printf "Warning: size threshold exceeded, skipping sync to %s\n" "$target/$LIFEBOAT_REPO_NAME" - else - sync_single "$target" - fi - else - sync_single "$target" + test -n "$target" || errx "Empty target in config/sync" + + if test -n "$limit" && test "$(du -s "$RESTIC_REPOSITORY" | cut -f1)" -gt "$limit"; then + err "Warning: size threshold exceeded, skipping sync to $target/$LIFEBOAT_REPO_NAME" + continue fi - done < config/sync -} -sync_single() { - rclone sync --transfers 20 --fast-list "$RESTIC_REPOSITORY" "$1/$LIFEBOAT_REPO_NAME" + rclone sync --transfers 20 --fast-list "$RESTIC_REPOSITORY" "$target/$LIFEBOAT_REPO_NAME" + done < config/sync } test $# -lt 2 && usage -- cgit v1.2.3-2-gb3c3