chainkeep plans backup retention for full and incremental chains. It applies grandfather-father-son rules (last, daily, weekly, monthly, yearly, a time window and legal-hold tags) to a catalog of backup sets, then adds every set a kept restore point depends on, so a kept incremental never loses its full. The delete plan is ordered leaves first, coverage reports which calendar buckets have no restorable backup, and a seeded simulation compares the storage cost of two policies. Selection is checked with property-based tests against a brute-force oracle. The demo data is synthetic.
A managed service provider backs up the practice-management servers of 30 dental clinics nightly (incrementals, with a weekly full) to a NAS, under a contract to keep 14 dailies, 8 weeklies, 12 monthlies and 7 yearlies. Age-based pruning deletes the full that a kept incremental depends on, which makes that restore point useless. It also counts failed or partial backups as restore points, and it gives no way to see what a policy change costs in storage or which rule is not actually being met.
flowchart LR
A[Catalog JSON or CSV] --> B[Validate chains<br/>unknown parent, cycles,<br/>failed ancestors]
P[YAML policy] --> C
B --> C[Select by bucket<br/>per rule, newest first]
C --> D[Dependency closure<br/>add bases of kept sets]
D --> E[Leaves-first delete plan<br/>storage before and after]
E --> F[apply to store]
B --> G[Coverage<br/>gaps and empty buckets]
P --> G
P --> H[Simulate<br/>a year, two policies]
Selection semantics, per rule: take the restorable sets (status ok, chain intact back to a full) newest first, and keep the newest set in each distinct bucket until the rule's count is reached. Buckets are local calendar days, ISO weeks, months and years in the policy's time zone. A bucket with no restorable set does not count towards the number. keep_last keeps the N newest restorable sets, keep_within keeps everything newer than the duration, and a set carrying a hold_tags tag is kept together with its chain.
python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]" # or: uv sync --extra dev, which installs the versions in uv.lock
chainkeep demo
chainkeep synth catalog.json
cat > policy.yaml <<'EOF'
keep_daily: 14
keep_weekly: 8
keep_monthly: 12
keep_yearly: 7
hold_tags: [legal-hold]
timezone: America/Denver
EOF
chainkeep plan catalog.json --policy policy.yaml --now 2026-11-26T12:00:00Z
chainkeep coverage catalog.json --policy policy.yaml --now 2026-11-26T12:00:00Z --unmet-only
cat > other.yaml <<'EOF'
keep_daily: 7
keep_weekly: 4
keep_monthly: 12
keep_yearly: 7
hold_tags: [legal-hold]
timezone: America/Denver
EOF
chainkeep simulate --policy policy.yaml --policy other.yaml
chainkeep apply catalog.json --policy policy.yaml --store /mnt/nas/backups --yes
plan changes nothing. apply deletes from a local directory store (one payload file plus a JSON sidecar per set) in plan order and is safe to rerun.
A real chainkeep demo run (rows for clinic-04 to clinic-29 elided with ...):
Synthetic catalog: 30 clinics, 11991 sets, 503.0 TB, seed 13, ending 2026-11-25.
Reference time 2026-11-26T12:00:00Z
Retention plan: daily 14, weekly 8, monthly 12, yearly 7, hold
legal-hold
┏━━━━━━━━━━━━┳━━━━━━┳━━━━━━┳━━━━━━━━━┳━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┓
┃ Client ┃ Kept ┃ Deps ┃ Deleted ┃ Held ┃ Before ┃ After ┃
┡━━━━━━━━━━━━╇━━━━━━╇━━━━━━╇━━━━━━━━━╇━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━┩
│ clinic-01 │ 29 │ 37 │ 334 │ 0 │ 27.0 TB │ 7.1 TB │
│ clinic-02 │ 30 │ 40 │ 330 │ 0 │ 19.9 TB │ 6.2 TB │
│ clinic-03 │ 30 │ 43 │ 327 │ 0 │ 15.3 TB │ 4.4 TB │
...
│ clinic-30 │ 30 │ 44 │ 326 │ 0 │ 21.7 TB │ 5.8 TB │
├────────────┼──────┼──────┼─────────┼──────┼──────────┼──────────┤
│ 30 clients │ 896 │ 1239 │ 9856 │ 1 │ 503.0 TB │ 142.7 TB │
└────────────┴──────┴──────┴─────────┴──────┴──────────┴──────────┘
Kept = chosen by a rule. Deps = kept only because a kept set depends on it (76.9
TB).
1 set(s) held by legal-hold tags, kept with their chains:
clinic-09: clinic-09-20260331T070000Z-i
1 catalog issue(s), those sets are not restorable:
clinic-05-20260815T070000Z-i: unknown_parent (parent clinic-05-missing-base is not in the catalog)
Coverage (last 30 days, complete calendar buckets)
┏━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Client ┃ Worst gap ┃ Rules not met ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ clinic-01 │ 72 h │ daily 11 of 14: 2026-11-15, 2026-11-16 and │
│ │ │ 2026-11-24 have no restorable backup │
│ clinic-05 │ 48 h │ daily 13 of 14: 2026-11-16 has no restorable backup │
│ clinic-06 │ 72 h │ daily 13 of 14: 2026-11-19 has no restorable backup │
│ clinic-12 │ 48 h │ daily 13 of 14: 2026-11-20 has no restorable backup │
│ clinic-17 │ 140 h │ weekly 6 of 8: 2026-W40 and 2026-W44 have no │
│ │ │ restorable backup │
│ clinic-18 │ 48 h │ daily 13 of 14: 2026-11-20 has no restorable backup │
│ clinic-20 │ 48 h │ daily 13 of 14: 2026-11-22 has no restorable backup │
│ clinic-22 │ 48 h │ daily 13 of 14: 2026-11-12 has no restorable backup │
│ clinic-24 │ 48 h │ daily 13 of 14: 2026-11-20 has no restorable backup │
│ clinic-25 │ 48 h │ daily 13 of 14: 2026-11-15 has no restorable backup │
│ clinic-26 │ 48 h │ daily 13 of 14: 2026-11-19 has no restorable backup │
│ clinic-28 │ 48 h │ daily 13 of 14: 2026-11-22 has no restorable backup │
│ clinic-29 │ 48 h │ daily 13 of 14: 2026-11-17 has no restorable backup │
└───────────┴───────────┴──────────────────────────────────────────────────────┘
13 of 30 clients have at least one unmet rule.
Simulation: seed 13, 3 clients, 365 days
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━┓
┃ Policy ┃ Steady state ┃ Peak ┃ Final ┃ Sets kept ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━┩
│ daily 14, weekly 8, monthly │ 16.5 TB │ 18.5 TB │ 17.5 TB │ 210 │
│ 12, yearly 7, hold legal-hold │ │ │ │ │
│ daily 30, weekly 8, monthly │ 16.8 TB │ 18.7 TB │ 17.6 TB │ 228 │
│ 12, yearly 7, hold legal-hold │ │ │ │ │
└───────────────────────────────┴──────────────┴─────────┴─────────┴───────────┘
Second policy steady-state storage vs first: +1.8% (mean of the last 90 days).
- Restorable, not just present. A failed set is never a restore point and a partial set never fills a bucket. A child of a failed or missing parent is marked unrestorable and reported, so a chain broken by a failed full is not counted as coverage. The cost is that some sets are kept only to satisfy chain structure, and the plan reports their bytes as dependency-only.
- Closure after selection. Rules pick restore points by bucket first, then the closure adds their bases. This keeps the rules easy to state and test against an oracle, at the price that storage can exceed a naive count of kept sets.
- Leaves first. Deletes are ordered deepest first, so an interrupted apply never leaves an orphaned child.
applyalso refuses to delete a set while any set still in the store names it as a parent. It checks the whole order before the first delete, so a catalog with a parent cycle, where every member has a dependent and no leaves-first order exists, is refused with nothing deleted. - Local calendar buckets. Daily buckets use the policy time zone, so a 01:30 backup on a daylight-saving fall-back day counts once. UTC would split or merge days around the change.
- Properties over examples. Restorability, idempotence and monotonicity are hypothesis properties on random catalogs, and one hand-worked 60-day catalog pins an exact kept set. Properties cover the space, the worked example guards against a wrong definition that is still self-consistent.
- A fake store beside the real one. The in-memory store lets tests replay the delete plan and check that no set is deleted while a child exists, without touching a disk.
.venv/bin/python -m pytest -q
112 tests, including five hypothesis properties at 150 examples each and a brute-force bucket oracle. Lint and format: ruff check . && ruff format --check ..
src/chainkeep/
catalog.py loader (JSON, CSV) and chain validation
policy.py YAML policy and duration parsing
buckets.py day, ISO week, month and year bucket keys in a time zone
select.py kept restore points and the rules that kept each
closure.py dependency closure, dependency-only labels
plan.py leaves-first delete plan and apply
store.py local-directory store and in-memory fake
coverage.py worst gap and per-rule calendar coverage
simulate.py year-long replay and policy comparison
synth.py seeded synthetic MSP catalog (seed 13)
report.py rich tables
cli.py typer commands
tests/ unit tests, hand-worked catalog, hypothesis properties
MIT
