fix(db-backup): age pre-migration dumps out with the nightly ones

Pre-migration dumps were kept by count alone, so an install that updates
rarely kept its last two, and every account deleted since, indefinitely,
while the docs and the backups card stated the nightly retention as the
retention of deleted accounts. A pre-migration dump older than the oldest
nightly dump kept now goes, except the newest one, the way back from the
latest update, which stays until the next update. The card, the disaster
recovery guide and its variable table say so.
This commit is contained in:
Marc-André Bombeck committed 2026-10-25 17:21:52 +01:00
1 parent 26a9efdf52
commit 6f83e7e685
14 files changed
+100 -27

No files matched your search

@@ -49,7 +49,8 @@ describe("<DatabaseDumpRow>", () => {
it("names the newest good dump, where it is, and how long dumps keep deleted accounts", () => {
const html = render(HEALTHY);
expect(html).toContain("kept on this server and off-host");
expect(html).toContain("the newest 7 are kept on this server");
expect(html).toContain("the newest 7 nightly dumps are kept on this server");
expect(html).toContain("before the latest update stays until the next update");
expect(html).toContain("removed after 30 days");
expect(html).not.toContain('data-slot="database-dump-failed"');
expect(html).not.toContain('data-slot="database-dump-setup"');
@@ -284,10 +284,14 @@ describe("retention", () => {
set("pre-migration", "2026-10-20T08:00:00Z"),
set("pre-migration", "2026-10-10T08:00:00Z"),
set("pre-migration", "2026-10-01T08:00:00Z"),
set("daily", "2026-10-24T01:30:00Z"),
// Nightly dumps back to the 8th, so both newer pre-migration dumps are
// inside the nightly retention.
...[8, 9, 10, 11, 12, 13, 14].map((d) =>
set("daily", `2026-10-${String(d).padStart(2, "0")}T01:30:00Z`),
),
];
const doomed = selectForDeletion(sets, {
keep: { daily: 1, "pre-migration": 2 },
keep: { daily: 7, "pre-migration": 2 },
now,
});
expect(doomed).toEqual([
@@ -296,6 +300,49 @@ describe("retention", () => {
]);
});
// The retention an operator states for deleted accounts is the nightly
// one. Kept by count alone, an install that updates rarely held its last
// two pre-migration dumps, and every account deleted since, for good.
it("removes a pre-migration dump older than every nightly dump kept, except the newest", () => {
const sets = [
set("pre-migration", "2026-08-20T08:00:00Z"),
set("pre-migration", "2026-07-10T08:00:00Z"),
set("daily", "2026-10-23T01:30:00Z"),
set("daily", "2026-10-24T01:30:00Z"),
];
const doomed = selectForDeletion(sets, {
keep: { daily: 7, "pre-migration": 2 },
now,
});
expect(doomed).toEqual([
"/b/healthlog-20260710T080000Z-pre-migration.dump",
"/b/healthlog-20260710T080000Z-pre-migration.manifest.json",
]);
// One taken after the oldest nightly kept stays beside the newest.
const recent = [
set("pre-migration", "2026-10-24T08:00:00Z"),
set("pre-migration", "2026-10-23T08:00:00Z"),
set("daily", "2026-10-23T01:30:00Z"),
];
expect(
selectForDeletion(recent, {
keep: { daily: 7, "pre-migration": 2 },
now,
}),
).toEqual([]);
// No nightly dump on disk: as many days as nightly dumps are kept.
const alone = [
set("pre-migration", "2026-10-24T08:00:00Z"),
set("pre-migration", "2026-10-20T08:00:00Z"),
];
expect(
selectForDeletion(alone, { keep: { daily: 2, "pre-migration": 2 }, now }),
).toEqual([
"/b/healthlog-20261020T080000Z-pre-migration.dump",
"/b/healthlog-20261020T080000Z-pre-migration.manifest.json",
]);
});
it("removes a .partial only once it is older than twelve hours", () => {
const fresh = set("daily", "2026-10-25T01:30:00Z", {
dump: false,
+27 -6
View File
@@ -1136,7 +1136,15 @@ export function isGoodSet(set) {
*
* Per kind, the newest `keep` good dumps stay (at least one, so the newest
* good dump is never removed) and every older good dump goes, archive and
* manifest together. A `.partial` or a manifest without its archive goes once
* manifest together.
*
* Pre-migration dumps are bounded by age as well: one older than the oldest
* nightly dump kept goes, except the newest pre-migration dump, which is the
* way back from the latest update. Kept by count alone, an install that
* updates rarely held its last two pre-migration dumps, and every account
* deleted since, with no limit, while the stated retention of deleted
* accounts was the nightly one. With no nightly dump on disk, the bound is
* as many days as nightly dumps are kept. A `.partial` or a manifest without its archive goes once
* it is older than twelve hours, so a run still in progress keeps its files.
* A final-named archive without a manifest is not something this module
* leaves behind, so it is not touched. Encrypted copies (`.enc`) an operator
@@ -1149,17 +1157,30 @@ export function isGoodSet(set) {
export function selectForDeletion(sets, options) {
/** @type {string[]} */
const remove = [];
for (const kind of DUMP_KINDS) {
const keep = Math.max(1, options.keep[kind] ?? 1);
const good = sets
const goodOf = (/** @type {DumpKind} */ kind) =>
sets
.filter((s) => s.kind === kind && isGoodSet(s))
.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime());
for (const set of good.slice(keep)) {
const dailyKeep = Math.max(1, options.keep.daily ?? 1);
const keptDaily = goodOf("daily").slice(0, dailyKeep);
const preMigrationFloor =
keptDaily.length > 0
? keptDaily[keptDaily.length - 1].timestamp.getTime()
: options.now - dailyKeep * 24 * 60 * 60 * 1000;
for (const kind of DUMP_KINDS) {
const keep = Math.max(1, options.keep[kind] ?? 1);
goodOf(kind).forEach((set, index) => {
const stays =
index < keep &&
(kind !== "pre-migration" ||
index === 0 ||
set.timestamp.getTime() >= preMigrationFloor);
if (stays) return;
remove.push(
/** @type {{ path: string }} */ (set.dump).path,
/** @type {string} */ (set.manifest),
);
}
});
}
for (const set of sets) {
for (const leftover of set.leftovers) {