From e29c53208a1dc3a737f75e8bb771c688ef8f49a4 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Fri, 31 Mar 2023 21:12:29 -0700 Subject: consistent weekly schedule with configurable start day --- backomp | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/backomp b/backomp index 460584c..068dade 100755 --- a/backomp +++ b/backomp @@ -12,13 +12,18 @@ BEGIN { for (i = 1; i <= len; ++i) { b = arr[i] - if (!match(retain, "([*0-9]+)" b, md)) + if (!match(opt_retain, "([*0-9]+)" b, md)) continue ret[b] = md[1] buckets[++n_buckets] = b - repl[b] = !!index(replace, b) + newest_in[b] = !!index(opt_newest_in, b) } + + split("sun mon tue wed thu fri sat", arr) + for (i in arr) + arr2[arr[i]] = i - 1 + wshift = (11 - arr2[opt_week_start])*24*60*60 } { @@ -27,13 +32,16 @@ BEGIN { bt["h"] = flr(t, 60*60) bt["d"] = flr(t, 24*60*60) + bt["w"] = flr(t + wshift, 7*24*60*60) - wshift bt["m"] = mktime(strftime("%Y %m", t, 1) " 01 00 00 00", 1) - bt["w"] = bt["m"] + flr(t - bt["m"], 7*24*60*60) for (i = 1; i <= n_buckets; ++i) { b = buckets[i] - if (repl[b] && bt[b] == last[b, b]) { + if (b == "m" && !newest_in["w"] && bt["m"] > bt["w"]) + next + + if (newest_in[b] && bt[b] == last[b, b]) { for (j = 1; j < i; ++j) { if (bt[buckets[j]] == last[b, buckets[j]]) break @@ -69,7 +77,8 @@ backup() { dest_dir="${1}" src_path="${2}" retain="${3}" - replace="${4}" + newest_in="${4}" + week_start="${5}" [[ "$((dcount++))" -ne 0 ]] && echo echo "[${dcount}] [${retain}] ${src_path} => ${dest_dir}" @@ -82,7 +91,10 @@ backup() { snaps=("${dest_path}" "${snaps[@]}") mapfile -t keep < <( - awk -v "retain=${retain}" -v "replace=${replace}" \ + awk \ + -v "opt_retain=${retain}" \ + -v "opt_newest_in=${newest_in}" \ + -v "opt_week_start=${week_start}" \ "${FILTER_AWK}" < <(printf '%s\n' "${snaps[@]}")) if [[ -z "${keep}" ]]; then echo "WARNING: dirs to keep shouldn't be empty; skipping" >&2 @@ -102,7 +114,8 @@ backup() { declare -A def_opts=( ['retain']='*h*d*w*m' - ['replace']='h' + ['newest_in']='h' + ['week_start']='sun' ) declare -A opts @@ -118,6 +131,8 @@ while IFS='=' read -r f1 f2; do opts["${opt}"]="${f2:-"${def_opts["${opt}"]}"}" else backup "${base_path}/${f1}" "${f2}" \ - "${opts['retain']}" "${opts['replace']}" + "${opts['retain']}" \ + "${opts['newest_in']}" \ + "${opts['week_start']}" fi done < "${1}" -- cgit v1.2.3-70-g09d2