All notes

restic forget never removed a snapshot: the backup's paths changed every night

resticBackupsLinuxSelf-hosting

My VPS backups had never lost a snapshot, though the forget line asked restic to keep only seven dailies, four weeklies and twelve monthlies. restic applies those rules to each group of snapshots with the same host and the same list of paths, and my list of paths changed nearly every night. Adding --group-by host put them all in one group, and the next run removed two.

The backup runs every night with restic 0.18.0, and the forget line looked fine:

restic forget --host "$host" --keep-daily 7 --keep-weekly 4 --keep-monthly 12

I found the problem by accident. I’d just added a folder to the backup and wanted to see it in the new snapshot, so I ran restic ls. Its header lists every path the snapshot was taken from, and the sites’ release folders were among them, each named after the moment it was deployed.

That part is on purpose. The script names what to back up item by item: the files and folders it finds under /opt, plus the release folder each site’s current link points to, so the backup holds the code that’s actually running. But a deploy changes that list. So does anything new under /opt.

So I grouped restic snapshots --json by its paths field:

9 snapshots in 8 path groups

Seven groups held a single snapshot. A group’s newest snapshot is its newest daily, weekly and monthly all at once, so a group of one can’t lose anything.

One group per host

restic forget --host "$host" --group-by host --keep-daily 7 --keep-weekly 4 --keep-monthly 12

Now the paths don’t matter. I ran it with --dry-run first. Three of the nine snapshots were second or third runs on a day that already had one, so I expected three removals. It said keep 7 snapshots and remove 2 snapshots.

The one it spared was the oldest, from the first night. The restic docs say “the oldest snapshot is kept additionally” when the rules have room for it, and seven dailies over six days left room.

Two days later the problem came back once. A manual run used a copy of the script without the flag, showed eight groups again and removed nothing. The flag only helps in the copy that runs.

Since then every nightly run has shown one group and removed one or two snapshots. Today there are nine: seven dailies, one weekly and that first snapshot.