#!/usr/bin/env bash set -e ! read -rd '' AWK_PREFILTER <<'EOF' /(\/|^)[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{6}$/ { a[$0] } END { n = asorti(a) for (i = n; i >= 1; --i) print a[i] } EOF ! read -rd '' AWK_FILTER <<'EOF' function flr(n, s) { return int(n / s) * s } function mstart(t) { return mktime(strftime("%Y %m 01 00 00 00", t, 1), 1) } BEGIN { n = split("h d w M", arr) for (i = 1; i <= n; ++i) { b = arr[i] if (!match(opt_retain, "([*0-9]+)(\\^?)" b, md)) continue ret[b] = md[1] newest_in[b] = (md[2] == "^") buckets[++n_buckets] = 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 } NR == 1 { ++keep[$0] } { t = mktime(gensub(/(.*\/|^)(.+)-(..)-(..)-(..)(..)(..)$/, "\\2 \\3 \\4 \\5 \\6 \\7", 1), 1) if (t < 0) { ++keep[$0] next } 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"] = mstart(t) for (i = 1; i <= n_buckets; ++i) { b = buckets[i] if (b == "M" && "w" in ret) { if (newest_in["w"]) { if (mstart(bt["w"] + 6*24*60*60) > bt["M"]) next } else if (bt["w"] < bt["M"]) { next } } if (newest_in[b] && bt[b] == last[b, b] && (i == 1 || bt[buckets[i-1]] != last[b, buckets[i-1]])) next if ((b, bt[b]) in bkeep || ret[b] == "*" || bc[b]++ < ret[b]) bkeep[b, bt[b]] = $0 for (j in buckets) last[b, buckets[j]] = bt[buckets[j]] } } END { for (i in bkeep) ++keep[bkeep[i]] n = asorti(keep) for (i = 1; i <= n; ++i) print keep[i] } EOF run() { echo "* ${@}" "${@}" } current() { local str="${1}" interval="${2}" local s=0 val d while [[ "${interval}" =~ ([0-9]+)(.?) ]]; do val="${BASH_REMATCH[1]}" case "${BASH_REMATCH[2]}" in d) ((s += val * 24*60*60)) ;; h) ((s += val * 60*60)) ;; m) ((s += val * 60)) ;; *) ((s += val)) ;; esac interval="${interval#*${BASH_REMATCH[0]}}" done ((s < 1)) && return 1 d="${str:0:10} ${str:11:2}:${str:13:2}:${str:15:2}" (($(date -u +%s) / s == $(date -ud "${d}" +%s) / s)) } backup() { local dest_dir="${1}" src_path="${2}" local retain="${3}" interval="${4}" week_start="${5}" local dest_path snaps keep skip i ki mapfile -td $'\0' snaps < <(shopt -s nullglob; printf '%s\0' "${dest_dir}/"* | awk -v 'RS=\0' -v 'ORS=\0' \ -- "${AWK_PREFILTER}") [[ -n "${snaps}" ]] && current "${snaps[0]##*/}" "${interval}" && \ skip=1 printf '[%d] [%s] %s => %s%s\n' "$((++dcount))" "${retain}" \ "${src_path}" "${dest_dir}" "${skip:+ [SKIPPED]}" [[ -n "${skip}" ]] && return dest_path="${dest_dir}/$(date -u '+%Y-%m-%d-%H%M%S')" run rsync -ac --mkpath ${snaps[0]:+"--link-dest=${snaps[0]}"} \ "${src_path}" "${dest_path}" snaps+=("${dest_path}") mapfile -td $'\0' snaps < <( printf '%s\0' "${snaps[@]}" | awk -v 'RS=\0' -v 'ORS=\0' \ -- "${AWK_PREFILTER}") mapfile -td $'\0' keep < <( printf '%s\0' "${snaps[@]}" | awk -v 'RS=\0' -v 'ORS=\0' \ -v "opt_retain=${retain}" \ -v "opt_week_start=${week_start}" \ -- "${AWK_FILTER}") if [[ -z "${keep}" ]]; then echo "WARNING: dirs to keep shouldn't be empty; skipping" >&2 return fi for ((i = ${#snaps[@]} - 1, ki = 0; i >= 0; --i)); do if [[ "${snaps[i]}" == "${keep[ki]}" ]]; then ((++ki)) else run rm -r "${snaps[i]}" fi done } declare -A def_opts=( ['retain']='*h' # e.g. 24h7d4w12M or 24^h10d*M ['interval']='0' ['week_start']='mon' ) declare -A opts while IFS='=' read -r f1 f2; do [[ -z "${f1}" ]] || [[ "${f1}" == '#'* ]] && continue if [[ "${f1}" =~ ^\[(.*)]$ ]]; then base_path="${BASH_REMATCH[1]}" declare -A opts="$(declare -p def_opts | sed '1s/[^=]*=//')" elif [[ "${f1}" == '$'* ]]; then opt="${f1:1}" [[ "${f2}" == '-' ]] && opts["${opt}"]= || opts["${opt}"]="${f2:-"${def_opts["${opt}"]}"}" else backup "${base_path}/${f1}" "${f2}" \ "${opts['retain']}" \ "${opts['interval']}" \ "${opts['week_start']}" fi done < "${1}"