File size: 4,978 Bytes
21ff762 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | """
backup_retention.py -- Policy-aware snapshot retention planning.
Pure-function module: given a list of :class:`SnapshotInfo` and a
:class:`BackupRetention` policy, return the snapshots that should be
kept and the snapshots that should be deleted. No filesystem writes
happen here — the caller decides whether to act on the plan.
Policy semantics
----------------
``keep_latest = N``
Always keep the ``N`` most-recent snapshots (by ``created_at``).
``keep_daily = M``
For each of the ``M`` most-recent calendar days (UTC) that have
at least one snapshot, keep the newest snapshot from that day.
The protected set is the **union** of the two rules. A snapshot only
gets pruned when it is in neither set. When both rules are zero, every
snapshot is prunable — that is the opt-in "keep nothing" setting.
Snapshots with ``created_at <= 0`` (malformed manifest / missing
timestamp) are always protected. We refuse to delete something we
can't confidently place in time.
"""
from __future__ import annotations
import time
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import Sequence
from backup_config import BackupRetention
@dataclass(frozen=True)
class RetentionPlan:
"""Result of planning a prune pass against a retention policy."""
keep: tuple[str, ...] # snapshot_ids preserved
delete: tuple[str, ...] # snapshot_ids to prune
protected_by_latest: tuple[str, ...] # subset of keep, by keep_latest rule
protected_by_daily: tuple[str, ...] # subset of keep, by keep_daily rule
def to_dict(self) -> dict:
return {
"keep": list(self.keep),
"delete": list(self.delete),
"protected_by_latest": list(self.protected_by_latest),
"protected_by_daily": list(self.protected_by_daily),
}
def _utc_date(epoch: float) -> str:
"""YYYY-MM-DD string for a Unix timestamp, UTC."""
return datetime.fromtimestamp(epoch, tz=timezone.utc).strftime("%Y-%m-%d")
def plan_prune(
snapshots: Sequence, # Sequence[SnapshotInfo]
retention: BackupRetention,
*,
now: float | None = None,
) -> RetentionPlan:
"""Decide which snapshots survive a retention sweep.
Parameters
----------
snapshots
Iterable of objects with ``snapshot_id: str`` and
``created_at: float`` attributes. Order does not matter.
retention
Policy to apply.
now
UTC timestamp treated as "right now" for the keep_daily window.
Defaults to :func:`time.time`. Tests pin this for determinism.
Returns
-------
RetentionPlan
Structured report of which snapshot IDs should be kept and
which can be deleted. Never raises on empty input.
"""
if retention.keep_latest < 0 or retention.keep_daily < 0:
raise ValueError("retention.keep_latest and keep_daily must be >= 0")
now_ts = now if now is not None else time.time()
items = list(snapshots)
# Stable newest-first ordering; ties broken by snapshot_id so the
# plan is deterministic even when two snapshots share a timestamp.
items.sort(key=lambda s: (-float(s.created_at), s.snapshot_id))
# Any snapshot missing a usable timestamp is preserved untouched.
# Rationale: we never want to silently delete something we can't
# place in time — that's how operators lose forensic evidence.
undated_ids = [s.snapshot_id for s in items if float(s.created_at) <= 0]
dated = [s for s in items if float(s.created_at) > 0]
# Rule 1 — keep_latest.
latest_ids = [s.snapshot_id for s in dated[: retention.keep_latest]]
# Rule 2 — keep_daily: newest snapshot per UTC day, for the most
# recent M days that actually contain snapshots.
daily_ids: list[str] = []
if retention.keep_daily > 0:
today = _utc_date(now_ts)
seen_days: dict[str, str] = {} # day → snapshot_id of the day's newest
for s in dated: # already newest-first
day = _utc_date(float(s.created_at))
# Don't protect future-dated snapshots under "past M days".
if day > today:
continue
if day not in seen_days:
seen_days[day] = s.snapshot_id
# Take the M most-recent days that actually appeared.
recent_days = sorted(seen_days.keys(), reverse=True)[: retention.keep_daily]
daily_ids = [seen_days[d] for d in recent_days]
protected = set(latest_ids) | set(daily_ids) | set(undated_ids)
keep: list[str] = []
delete: list[str] = []
# Preserve newest-first ordering in both output tuples.
for s in items:
if s.snapshot_id in protected:
keep.append(s.snapshot_id)
else:
delete.append(s.snapshot_id)
return RetentionPlan(
keep=tuple(keep),
delete=tuple(delete),
protected_by_latest=tuple(latest_ids),
protected_by_daily=tuple(daily_ids),
)
|