Apply a snapshot backup retention policy (keep recent days, one weekly copy per week up to a limit, delete the rest) to an input.json list of snapshots, and write retention-plan.csv and retention-summary.json. Use whenever a task mentions snapshot retention, keepDays, keepWeeks, weekly copies, pruning backups, or freed gigabytes, even if no files are named.
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-14b727cb)
---
name: backup-retention
description: Apply a snapshot backup retention policy (keep recent days, one weekly copy per week up to a limit, delete the rest) to an input.json list of snapshots, and write retention-plan.csv and retention-summary.json. Use whenever a task mentions snapshot retention, keepDays, keepWeeks, weekly copies, pruning backups, or freed gigabytes, even if no files are named.
---
# Backup retention
You cannot run code, so do this by hand, carefully, writing down intermediate work. Read `input.json` (in the working folder) and write both outputs in the same folder.
Input: `today`, `keepDays`, `keepWeeks` (whole numbers) and `snapshots`, an array of `{id, day, sizeGb}` in the order taken (day never decreases down the list; ties on a day are possible).
## Procedure
1. **Number the snapshots** by their position in the file: 1, 2, 3, ... Position matters for ties.
2. **Work out a table.** For each snapshot compute:
- `age = today - day`
- `recent` if `age <= keepDays` (exactly keepDays is recent). Otherwise it is *older*.
- `week = floor(day / 7)` (integer division, drop the remainder; e.g. day 13 -> 1, day 14 -> 2).
3. **Recent snapshots**: action `keep-recent`. They take no part in the weekly step, so a recent snapshot never occupies a week and never blocks an older one.
4. **Weekly copies**: look only at older snapshots. Group them by `week`. In each group pick the winner: the one with the largest `day`; on a tie, the one with the largest position (listed later). Because the file is ordered by day, the winner is simply the *last-listed older snapshot of that week*.
- The winner becomes `keep-weekly` only if its `age <= 7 * keepWeeks` (exactly equal qualifies). Compute `7 * keepWeeks` once and compare.
- If the winner is too old, the whole week gets no weekly copy (all its other snapshots are even older), so everything in it is deleted. Do not promote a runner-up.
- A week group is judged by the winner only; non-winners are always `delete`.
5. **Everything else** (older and not a weekly winner) is `delete`.
6. **Order the rows** newest first: day descending; for equal days, later-listed first. Since input is already sorted by day, this is exactly the input list reversed (last snapshot first). If you notice the input is not actually sorted, sort by day descending, then position descending.
7. **Write `retention-plan.csv`**: first line `id,action`, then one line per snapshot (every snapshot appears exactly once) as `id,action`. Use plain text with no spaces or quotes (quote an id only if it contains a comma or quote). End each line with a newline.
8. **Write `retention-summary.json`** with exactly these keys:
```json
{"kept": 0, "deleted": 0, "freed_gb": 0}
```
- `kept` = count of `keep-recent` + `keep-weekly`
- `deleted` = count of `delete`
- `freed_gb` = sum of `sizeGb` over deleted snapshots, as a bare JSON number (no quotes). 0 if none deleted.
- `kept + deleted` must equal the number of snapshots; recheck.
## Checks before finishing
- Re-add the sizeGb of the deleted snapshots a second time to confirm the total.
- Confirm each week has at most one `keep-weekly`.
- Re-verify boundaries: age == keepDays is recent; age == 7*keepWeeks qualifies weekly; days 7, 14, 21 start new weeks.
- Empty `snapshots`: CSV has only the header; summary is `{"kept":0,"deleted":0,"freed_gb":0}`.
- Numbers in the input are used as given; do not rename keys or add extra ones.
## Worked example
today=100, keepDays=7, keepWeeks=4 (weekly limit age 28):
- day 93: age 7 -> keep-recent (week 13).
- day 92: age 8, older, week 13; the only older one there -> keep-weekly (the recent day-93 doesn't block it).
- day 72: age 28, week 10, newest older there -> keep-weekly.
- day 71: week 10, not the newest older -> delete.
- day 60: age 40 > 28 -> delete even if it is its week's newest.