Help with forget policy

Hi all,

Apologies for another post on forget policies, I’ve searched in the forum and couldn’t find my exact question, I hope I haven’t missed it.

I’m trying to make sense of a very basic forget policy:

restic forget --group-by=host,path --keep-monthly 4

My understanding is that this policy would keep only the last 4 “monthly” snapshots. That is, it would only keep the most recent snapshot for each of the last 4 calendar months that have at least 1 snapshot.

So, my first question would be, am I interpreting this correctly?

I have a repo with the following snapshots:

repository bd9a9bcc opened (version 2, compression level auto)
ID        Time                 Host        Tags        Paths
----------------------------------------------------------------
8910a903  2024-01-02 13:42:44  myhost      mytag       /tmp/data
ab49e6f7  2024-01-03 01:04:38  myhost      mytag       /tmp/data
209e47d4  2024-01-04 20:09:54  myhost      mytag       /tmp/data
acdde683  2024-01-04 20:20:32  myhost      mytag       /tmp/data
----------------------------------------------------------------
4 snapshots

If I understood correctly, that forget policy would remove all but the last snapshot (acdde683), since all of them were taken in the same calendar month. However, I get the following result:

repository bd9a9bcc opened (version 2, compression level auto)
Applying Policy: keep 4 monthly snapshots
snapshots for (host [myhost], paths [/tmp/data]):
keep 2 snapshots:
ID        Time                 Host        Tags        Reasons           Paths
----------------------------------------------------------------------------------
8910a903  2024-01-02 13:42:44  myhost      mytag       monthly snapshot  /tmp/data
acdde683  2024-01-04 20:20:32  myhost      mytag       monthly snapshot  /tmp/data
----------------------------------------------------------------------------------
2 snapshots

remove 2 snapshots:
ID        Time                 Host        Tags        Paths
----------------------------------------------------------------
ab49e6f7  2024-01-03 01:04:38  myhost      mytag       /tmp/data
209e47d4  2024-01-04 20:09:54  myhost      mytag       /tmp/data
----------------------------------------------------------------
2 snapshots

Would have removed the following snapshots:
{209e47d4 ab49e6f7}

What is the reason for also keeping the first snapshot (8910a903)?

In case it helps, I’m using restic version 0.16.2 compiled with go1.21.3 on linux/amd64 and I used the following script to create the repo:

#!/bin/bash

mkdir -p /tmp/{repo,data}
touch /tmp/data/foo

export RESTIC_PASSWORD="1234"

restic init -r /tmp/repo
restic backup -r /tmp/repo --time="2024-01-02 13:42:44" --tag="mytag" /tmp/data
restic backup -r /tmp/repo --time="2024-01-03 01:04:38" --tag="mytag" /tmp/data
restic backup -r /tmp/repo --time="2024-01-04 20:09:54" --tag="mytag" /tmp/data
restic backup -r /tmp/repo --time="2024-01-04 20:20:32" --tag="mytag" /tmp/data

restic snapshots -r /tmp/repo
restic forget --dry-run --verbose=2 -r /tmp/repo --host $(hostname) --group-by=host,path --keep-monthly 4

PS. I’m really liking restic, many thanks for the hard work!

Thanks!

Here you are:

The oldest snapshot is always kept.

Thank you! I had thought about this but when using --keep-monthly 1 the oldest would be forgotten too, so I thought it would be something else.

From what I read in the PR, the important bit is that the oldest snapshot is always kept when there aren’t enough snapshots to keep. Thanks for sharing :slight_smile: