diff options
-rwxr-xr-x | backomp | 33 |
1 files changed, 26 insertions, 7 deletions
@@ -2,9 +2,17 @@ set -e +# FIXME: clean up repetition ! read -rd '' FILTER_AWK <<'EOF' function flr(n, s) { return int(n / s) * s } +BEGIN { + if (match(retain, /([*0-9]+)h/, md)) hr = md[1] + if (match(retain, /([*0-9]+)d/, md)) dr = md[1] + if (match(retain, /([*0-9]+)w/, md)) wr = md[1] + if (match(retain, /([*0-9]+)m/, md)) mr = md[1] +} + { t = mktime(gensub(/(.*\/|^)(.+)-(..)-(..)-(..)(..)(..)/, "\\2 \\3 \\4 \\5 \\6 \\7", 1), 1) @@ -15,10 +23,10 @@ function flr(n, s) { return int(n / s) * s } w = m + flr(t - m, 7*24*60*60) } -h in hkeep || hc++ < 24 { hkeep[h] = $1 } -d in dkeep || dc++ < 7 { dkeep[d] = $1 } -w in wkeep || wc++ < 4 { wkeep[w] = $1 } -{ mkeep[m] = $1 } +h in hkeep || hr == "*" || hc++ < hr { hkeep[h] = $1 } +d in dkeep || dr == "*" || dc++ < dr { dkeep[d] = $1 } +w in wkeep || wr == "*" || wc++ < wr { wkeep[w] = $1 } +m in mkeep || mr == "*" || mc++ < mr { mkeep[m] = $1 } END { for (i in hkeep) keep[hkeep[i]]++ @@ -39,9 +47,10 @@ run() { backup() { dest_dir="${1}" src_path="${2}" + retain="${3:-"*h*d*w*m"}" [[ "$((dcount++))" -ne 0 ]] && echo - echo "[${dcount}] ${src_path} => ${dest_dir}" + echo "[${dcount}] [${retain}] ${src_path} => ${dest_dir}" mapfile -t snaps < <(shopt -s nullglob; printf '%s\n' "${dest_dir}/"* | sort -r | head -c -1) @@ -51,7 +60,8 @@ backup() { snaps=("${dest_path}" "${snaps[@]}") mapfile -t keep < <( - awk "${FILTER_AWK}" < <(printf '%s\n' "${snaps[@]}")) + awk -v "retain=${retain}" "${FILTER_AWK}" < <( + printf '%s\n' "${snaps[@]}")) if [[ -z "${keep}" ]]; then echo "WARNING: dirs to keep shouldn't be empty; skipping" >&2 return @@ -73,8 +83,17 @@ while IFS='=' read -r f1 f2; do if [[ "${f1}" =~ ^\[(.*)]$ ]]; then base_path="${BASH_REMATCH[1]}" + retain= + continue + fi + + if [[ "${f1}" == '$'* ]]; then + if [[ "${f1}" == '$retain' ]]; then + retain="${f2}" + fi + continue fi - backup "${base_path}/${f1}" "${f2}" + backup "${base_path}/${f1}" "${f2}" "${retain}" done < "${1}" |