Forget policy relative to current time

Hi,

Sorry if this has been asked before.

How to forget snapshots in relative to the current time instead of latest snapshot?

For eg: a user repo might has snapshots like these.

> restic snapshots

ID        Time                 Host        Tags            Paths
---------------------------------------------------------------------------
9d838e8e  2020-09-30 00:00:00  server-1    database,myapp  /tmp/dump_9f5b0c
7134a5e4  2020-10-10 00:00:00  server-1    database,myapp  /tmp/dump_c45fa4
faa55269  2020-10-15 00:00:00  server-1    database,myapp  /tmp/dump_7fd574
2f24f1a1  2020-10-20 00:00:00  server-1    database,myapp  /tmp/dump_19e9e2
---------------------------------------------------------------------------
4 snapshots

I would like to keep user snapshots for the last one month only (now is Jan 2021).

There are many users using our backup solution. For users who actively creating backup, this should be easily done with --keep-within 1m --group-by ''. But for inactive one, its quite tricky to get this done right.

Restic is always using the latest snapshot as a reference. In your example above all 4 snapshots would be kept as 2f24f1a1 is the latest snapshot and the other 3 snapshots are within the timespan of 1 month (2020-09-20 until 2020-10-20). Unless you have something different in mind, restic is doing the ‘right thing’ by default.

PS: You can test your policies by using the --dry-run flag.

1 Like

I’d say that suggests ‘something different’ is indeed intended.

1 Like

OK, I just read OP’s post again. I initially assumed that 1 month of backups should be kept no matter how old they are. But if the intention is to delete everything older than 1 month (and leave inactive users with no backup), then there is no way to do this with restic that I know of.

One “hacky” possibility would be to just do a “fake” backup before e.g. of an empty dir (and get the snapshot ID) , use your proposed --keep-within 1m --group-by '' and then run forget a second time to explicitly remove that “fake” snapshot.

For a proper solution, you should run restic snapshots --json, parse the output by some script which determines the snpahot IDs to forget and run forget with those IDs.

1 Like

Brilliant, this should work, and probably the easiest among others.