Script "restic diff" between the last two snapshots

So I wrote a backup script for a client:

@ECHO OFF

ECHO Setting environment variables, please wait…
SET B2_ACCOUNT_ID=
SET B2_ACCOUNT_KEY=
SET RESTIC_REPOSITORY=
SET RESTIC_PASSWORD=
ECHO.

ECHO Updating Restic, please wait…
C:\scripts\restic.exe self-update
ECHO.

ECHO Backing up, please wait…
C:\scripts\restic.exe backup “C:\scripts” “E:\data” >C:\Scripts\backup-b2.log
ECHO.

ECHO Emailing results, please wait…
C:\scripts\sendemail.exe -f client@domain.com -t client@domain.com -u Backblaze Backup Log -s smtp.server.com:587 -xu client@domain.com -xp PasswordGoesHere -o message-file=C:\Scripts\backup-b2.log

I tried various levels of verbosity (1-4) but couldn’t find one that shows JUST added/deleted/modified files - it seems 3 and 4 both show all skipped files too?

Is there a way to show just added / deleted / modified files? OR is there a way to automatically do a restic “diff” between the last two snapshots and pipe that out to a text file? Either one would work.

Just curious. Thanks! :slight_smile:

Unless I’m missing something, restic diff without -v does exactly what you’re describing.

Oh, sorry, I wasn’t clear. I meant I tried -v -vv -vvv and -vvvv with “restic backup” in the script with >backup.log at the end. I was hoping for a level that would only show what’s changed, omitting skipped files.

Separately, I was wondering if there was a way to do “restic diff” and automatically compare the last two snapshots without specifying their IDs (or a way to programmatically grab them in order to specify them, perhaps from a “restic snapshots” listing).

Either option would get me what I’m wanting - just a list of added/modified/deleted files, which I could then email myself after a script execution.

There’s probably an easier way but this is what I use on Linux:

PREV=$(/opt/restic/restic snapshots --compact --repo /path/to/Restic | tail -5 | head -1 | awk '{print $1}')
LAST=$(/opt/restic/restic snapshots --compact --repo /path/to/Restic | tail -3 | head -1 | awk '{print $1}')
/opt/restic/restic diff "$PREV" "$LAST" --repo /path/to/Restic >> ${LOG} 2>&1

It would be great if this was easier, perhaps it could be what restic diff does by default?

3 Likes

Sure, that’s what --json is for. Assuming the hostname is foobar and all snapshots have the same paths, something like this will work on Linux where command substitution and jq are available:

restic diff $(restic snapshots --host foobar | jq -r '.[-2:][].id')

Presumably you can do something similar on Windows using PowerShell – or maybe even in batch files, though it will likely be a bit trickier.

4 Likes

Hi guys,

I too need to compare the latest to the previous one with different paths and even more with a single snapshot have multiple sources on windows

The diff command helps to create a very nice report to show what changes have happened. I think it can be enhanced a lot more for reporting and logging results.
–host
–group-by-path
Combination of the above 2 (would help me😊)
–group-by-host
Find missed dates (dates on which backups did not run from a host or path)

I can raise a issue on github if the enhancement is worth.

Just found this while trying to figure some things about my repo out…

Just curious why did you explicitly mention --json when your subsequent command did not use it? :wink:

(So, just to make that clear to other people finding this, the command should be

restic diff $(restic snapshots --host foobar --json | jq -r '.[-2:][].id')

2 Likes

has anyone worked on the restic diff append log within termux? i see here on linux nztim had >> ${LOG} 2>&1 functioning. i’ve tried every combination i can think of:
>> ${LOG} 2>&1
&>> ${LOG} 2>&1
&> ${LOG} 2>&1
>& $LOG} 2>&1
either they don’t append an existing log or they simply overwrite the entire log with a new log file.
Thanks,
R