Let’s say I have a crontab task that automatically runs a Restic backup every night.
Let’s also say that I screwed up one of my files, but didn’t realize it for a couple of days.
Is that file overwritten in the Restic repository? Or can I restore an old version of that file?
If I can restore an older version of that file, how old can I go? What determines how long my Restic repository will keep an older copy of my files? Obviously I don’t want it go to back infinitely – I’d run out of space on my backup media. But, say, 30 days or so? How do I set that up?
Every time you run restic backup, a new snapshot is created. A snapshot is a moment-in-time capture of the files you have backed up. You can run restic snapshots to see a list of all of your snapshots.
Snapshots are never automatically deleted. You need to run restic forget with appropriate options to discard old snapshots. Further reading on this topic:
restic backup never deletes anything, it only adds another snapshot. The prior snapshots you took will still be there, so yes, you can restore older versions by selecting an older snapshot to restore from.
Note that restic does deduplication, so if you take a backup, create a single file, and take a backup again, only that one new file is added (plus the data required to store the different directory structure) – and restic can even deduplicate across all files in all snapshots, so if the new file shares some data in common with other files, the amount of data added will probably be lower than the size of the file.
In other words, if not very much data changes between backups then not much data is added to the repository each time you perform a backup.
The topic I linked above goes into more detail, but as an example, if you wanted to retain one snapshot per day for the last 30 days and discard everything else, you could run restic forget --keep-daily 30 --prune on a schedule (I’d suggest weekly instead of daily since pruning may need to rewrite older packs to discard unused data, which can be fairly I/O intensive, and can incur transfer fees if you’re using a cloud storage provider to store the repository).