#!/usr/bin/env bash # # qdb-logprune.sh --- trim ii's append-only `out` logs to a recent # window. ii has no built-in rotation: it appends every channel line # to one `out` file per channel, forever. ii lines are epoch-prefixed, # so "keep the last N days" is a one-line awk filter. # # ii holds `out` open for writing, so rewriting it underneath a # running ii is racy. This script stops qdb-ii and qdb-watch for the # few seconds the rewrite takes, then starts them again --- ii # reconnects and re-joins the channel. (BindsTo propagates the stop # but not the start, so both units are managed explicitly here.) # # Wired up as qdb-logprune.timer (weekly). Config via environment: # QDB_LOG_KEEP_DAYS days of log to keep (14) # QDB_IRC_BASE ii's base dir ($HOME/irc) # # qdb snapshots in quotes/ are NEVER touched --- they are permanent, # which is the whole point of qdb. set -u KEEP_DAYS="${QDB_LOG_KEEP_DAYS:-14}" IRC_BASE="${QDB_IRC_BASE:-$HOME/irc}" cutoff=$(date -d "${KEEP_DAYS} days ago" +%s) export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}" # Pause the capture pipeline only if it is actually running, and only # restart it if so. Stop watch before ii, start ii before watch. was_active=$(systemctl --user is-active qdb-ii 2>/dev/null || true) [ "$was_active" = active ] && systemctl --user stop qdb-watch qdb-ii shopt -s nullglob for f in "$IRC_BASE"/*/*/out "$IRC_BASE"/*/out; do [ -f "$f" ] || continue # Keep lines newer than the cutoff; keep any non-epoch line verbatim # (defensive --- never silently drop content we don't understand). if awk -v c="$cutoff" '!/^[0-9]+ / || $1 + 0 >= c' "$f" > "$f.prune.$$"; then mv "$f.prune.$$" "$f" else rm -f "$f.prune.$$" fi done [ "$was_active" = active ] && systemctl --user start qdb-ii qdb-watch