Apply a snapshot backup retention policy (keep recent days plus one weekly copy per week) to input.json and write retention-plan.csv and retention-summary.json. Use whenever the user asks to prune, thin, expire or plan retention for server snapshots with today, keepDays, keepWeeks and a snapshots list, even if they do not say "retention".
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Backup Retention?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/gabrielmoreira-backup-retention-0cfb7ac1)
---
name: backup-retention
description: Apply a snapshot backup retention policy (keep recent days plus one weekly copy per week) to input.json and write retention-plan.csv and retention-summary.json. Use whenever the user asks to prune, thin, expire or plan retention for server snapshots with today, keepDays, keepWeeks and a snapshots list, even if they do not say "retention".
---
# Backup retention
You cannot run code, so do this by careful hand-work, writing out intermediate tables as you go. Follow the steps in order.
## 1. Read input.json
Fields: `today`, `keepDays`, `keepWeeks`, `snapshots` (array of `{id, day, sizeGb}` in the order taken; day never decreases). Number each snapshot by its position in the list: 0, 1, 2, ... (call this `pos`). Later `pos` = newer among same-day snapshots.
## 2. Classify each snapshot
Compute for each one:
- `age = today - day`
- `recent` if `age <= keepDays` (equal counts as recent). Action: `keep-recent`.
- Otherwise it is **older**. Compute `week = floor(day / 7)` (drop the remainder; e.g. day 13 -> 1, day 14 -> 2).
Write a table: pos, id, day, sizeGb, age, recent/older, week (older only).
## 3. Pick weekly copies
Look only at older snapshots (recent ones are ignored here entirely; they neither occupy nor block a week). Group older snapshots by `week`. In each group choose the newest: the largest `day`; ties go to the largest `pos` (listed later). Because the list is in non-decreasing day order, this is simply the **last older snapshot of that week in the list**.
The chosen snapshot becomes `keep-weekly` only if `age <= 7 * keepWeeks` (equal qualifies). If the chosen one is too old, the week gets no weekly copy, and the other snapshots in that week are NOT promoted: they all stay deleted (the newest is the only candidate, and if it is too old every other one is older still or equal-aged, so none qualifies by design; do not substitute another).
Every older snapshot not chosen as weekly is `delete`.
If keepWeeks is 0, the limit is age <= 0; older snapshots have age > keepDays >= 0, so none qualifies, and all older snapshots are deleted.
## 4. Order the rows
Sort newest first: `day` descending; within equal day, `pos` descending. Since the input is sorted by day ascending, this is exactly the input list **reversed** (last entry first). Just reverse the list; do not re-sort by id.
## 5. Write retention-plan.csv (beside input.json)
```
id,action
<id>,<keep-recent|keep-weekly|delete>
```
Header first, one row per snapshot, newline separated, no quotes or extra spaces, ids written exactly as in the input (if an id is a string with a comma or quote, wrap it in CSV double quotes). An empty snapshot list gives only the header.
## 6. Write retention-summary.json (beside input.json)
```
{"kept": K, "deleted": D, "freed_gb": F}
```
- K = count of keep-recent + keep-weekly
- D = count of delete
- F = sum of sizeGb over deleted snapshots (a plain JSON number; 0 if none)
Exactly these three keys. K + D must equal the number of snapshots.
## 7. Verify before finishing
- Row count equals the number of snapshots; every id appears once.
- Each week has at most one keep-weekly.
- Re-check boundaries: age == keepDays is recent; age == 7*keepWeeks qualifies weekly.
- Re-add the freed_gb sum a second time.
## Worked example
today 100, keepDays 7, keepWeeks 4 (weekly limit age 28): day 93 (age 7) recent; day 92 (age 8, week 13) weekly although day 93 shares the week; day 72 (age 28, week 10) weekly if it is the last older snapshot of week 10; day 71 (week 10) deleted; day 69 (age 31, week 9) would be deleted as too old even if it is its week's newest.