diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2023-03-25 23:48:20 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2023-03-25 23:48:20 -0700 |
commit | 0f9c5617c41b0701c5c9935db069249d824a8c7c (patch) | |
tree | 3d547f35c3479507e081a087153dda4c55f57dc2 | |
parent | d3e52a72b19b64f3443204fe9cac44637217f30b (diff) | |
download | backomp-0f9c5617c41b0701c5c9935db069249d824a8c7c.tar.gz backomp-0f9c5617c41b0701c5c9935db069249d824a8c7c.tar.xz |
allow prioritisation of latest backup within a slot
-rwxr-xr-x | backomp | 40 |
1 files changed, 23 insertions, 17 deletions
@@ -7,9 +7,13 @@ function flr(n, s) { return int(n / s) * s } BEGIN { split("h d w m", buckets) - for (i in buckets) + for (i in buckets) { if (match(retain, "([*0-9]+)" buckets[i], md) && md[1]) ret[buckets[i]] = md[1] + + if (index(replace, buckets[i])) + repl[buckets[i]] = 1 + } } { @@ -19,11 +23,17 @@ BEGIN { bt["h"] = flr(t, 60*60) bt["d"] = flr(t, 24*60*60) bt["m"] = mktime(strftime("%Y %m", t, 1) " 01 00 00 00", 1) - bt["w"] = m + flr(t - m, 7*24*60*60) + bt["w"] = bt["m"] + flr(t - bt["m"], 7*24*60*60) + + for (i = 1; i <= length(buckets); ++i) { + b = buckets[i] + + if (!ret[b] || (b in repl && (b, bt[b]) in bkeep)) + continue - for (b in ret) if ((b, bt[b]) in bkeep || ret[b] == "*" || bc[b]++ < ret[b]) bkeep[b, bt[b]] = $1 + } } END { @@ -44,6 +54,7 @@ backup() { dest_dir="${1}" src_path="${2}" retain="${3:-"*h*d*w*m"}" + replace="${4:-"h"}" [[ "$((dcount++))" -ne 0 ]] && echo echo "[${dcount}] [${retain}] ${src_path} => ${dest_dir}" @@ -56,8 +67,8 @@ backup() { snaps=("${dest_path}" "${snaps[@]}") mapfile -t keep < <( - awk -v "retain=${retain}" "${FILTER_AWK}" < <( - printf '%s\n' "${snaps[@]}")) + awk -v "retain=${retain}" -v "replace=${replace}" \ + "${FILTER_AWK}" < <(printf '%s\n' "${snaps[@]}")) if [[ -z "${keep}" ]]; then echo "WARNING: dirs to keep shouldn't be empty; skipping" >&2 return @@ -74,22 +85,17 @@ backup() { printf 'kept %s\n' "${keep[@]}" } +declare -A opts while IFS='=' read -r f1 f2; do [[ -z "${f1}" ]] && continue if [[ "${f1}" =~ ^\[(.*)]$ ]]; then base_path="${BASH_REMATCH[1]}" - retain= - continue - fi - - if [[ "${f1}" == '$'* ]]; then - if [[ "${f1}" == '$retain' ]]; then - retain="${f2}" - fi - - continue + opts=() + elif [[ "${f1}" == '$'* ]]; then + opts["${f1:1}"]="${f2}" + else + backup "${base_path}/${f1}" "${f2}" \ + "${opts['retain']}" "${opts['replace']}" fi - - backup "${base_path}/${f1}" "${f2}" "${retain}" done < "${1}" |