Trigger a command on error or failure

I want to run a curl command like “curl -d “snapshot error” https://notify.example.com/test” but only when there is an error or failure to take snapshot. I ran a test for a successful snapshot and it works fine, I receive the notification - how to only run it when there’s an error with kopia ui?

Edit: ChatGPT says do this, but I’m not sure?

[ $? -ne 0 ] && curl -d "snapshot failed" https://notify.example.com/test

Edit: After successfully failing it didn’t work, so I think that this part is wrong. any advice?

[ $? -ne 0 ] &&

What it suggested should work, IF kopia sets the result code correctly. It is a bit long-winded way to first run kopia, then test the result code for non success, then use the “run only if successful” that looks at the test exist code and if the test was ok (because previous command was not), only then run the curl command. So, the advice is sort-of correct from a shell perspective, but it would have been simpler to just have

kopia <commands and options> || curl -d ...
which means “run second command if first failed” instead of using && that only runs if the previous worked, and have that one inverted from the kopia result code.

Now, someone needs to check if kopia does set result codes correctly or not, and my guess is that it might not do that, but if it does, then || is easier I guess.

So based on what you said in the “after snapshot” of the policy in the UI I should put the following?

|| curl -d "snapshot failed" https://notify.example.com/test

I’m fine with adding a command before/after snapshots or files since it allows me to do either one, but I just don’t know what to put so it triggers ONLY on failure/error. Like I said it works great on successful completion of snapshot.

No, it was based on if you run kopia from command-line or a script, not what to type in a gui window, since that “command” will most probably not be run in a context where it can read out the previous kopia run exit/return code.
The list of informational environment variables listed here (for the After) actions should need one that says if the snap was successful or not, and as of now it doesn’t seem to have any such indicator:

For anyone interested in following along I’ve created a git issue on this as I’ve now asked in the forum and on reddit as well as checked stackoverflow and chatgpt and cannot find any solutions to what I imagine would be a common requirement of knowing when snapshots fail for Kopia’s GUI version.

https://github.com/kopia/kopia/issues/4340