Restic diff latest snapshot to its parent

Hej it’s me again! (Thanks for the help last time. I’m getting there)
I’m almost sure this is a feature, but I tried figuring it out with the documentation and the --help, (and didn’t figure out how to do it)

Is there a way to learn which files were added (and/or removed, ideally excluding modified) in the latest snapshot compared to its parent, using a single command? (Ideally with output that looks like the tree-command, but I don’t mind if it doesn’t)

I can get the info I’m looking for with restic snapshots, manually copying the IDs, using restic diff with them, (and massaging the output with grep, dirname etc could get me the rest of the way to something e.g. tree --fromfile can digest), but is there something like restic diff latest --parent-of=latest ?

Thanks in advance!

To get it the way you want will take a little work; sounds like you’re on the right track.

Here’s a one-liner that I think does what you’re looking for:

restic diff $(restic snapshots | tail -n4 | head -n2 | cut -d' ' -f1) | awk '($1=="+")' | cut -f 2- -d ' ' | tree --fromfile .

For removed (or modified), change the plus to a minus (or M).

Note that this version assumes the penultimate snapshot is the parent.

Please use the json output of a command if available and don’t parse the text output format, which is not guaranteed to be stable, in that case.

That is I would change the snapshot selection as follows:
restic diff $(restic snapshots --json latest | jq -r ".[0].id, .[0].parent") | awk '($1=="+")' | cut -f 2- -d ' ' | tree --fromfile .

3 Likes

And your solution has the added benefit of pointing to the true parent, rather than to the previous snapshot.

I guess it’s time for me to learn how to parse json. Is jq your recommended tool for other tasks such as this @MichaelEischer? Thanks!

I think jq is the de facto standard for parsing json on the command line. At least I haven’t used anything else ^^ .

Btw, there’s now support for diff --json in the master branch. So awk and cut can be replaced by another jq invocation.

This may be slightly off topic, but I was just playing around with this command and noticed that in many of my snapshots the parent attribute pointed to a non-existent snapshot.

I had coincidentally just updated a bunch of tags right before that, so I assume that is the underlying reason, but I was curious: is it a known issue that when you change tags on a snapshot, any parents referencing that snapshot get orphaned?

Yes, a snapshot’s ID will, by design, change if you change its tags. This is a consequence of restic’s repository format, as described here.

If you need the original ID of a snapshot for some reason, this is still accessible with the command restic cat snapshot, where it will be listed under the field original.