Run script upon **Successful** backup

I found this thread on how to read environment variables in Windows.

How is this done in Linux?

See, I was doing a simple rsync to a offsite Raspberry Pi, and was able to drop a date and time into a file on the pi, then display via LCD display

I’ve since switched from rsync to Duplicati. And I’m definitely keeping Duplicati, I’d just like to keep an eye on the sync status at the other end. In order to do that, I need to get some mechanism to drop a date and time if backup is successful.

The variables should have the same names in Linux, you just access them differently depending on what language your script is in.

Are you able to trigger a basic across (such as write to a file) now?

Yes, via an ssh key exchange, I can execute code remotely or deposit files from one machine to the other.

Also, I ran printenv and I don’t see the variables mentioned in the post.I also tried to echo the variables manually ($DUPLICATI__PARSED_RESULT for example) and they came back empty.

The environment variables are only set within the Duplicati process itself, and inherited by any child processes it runs (like post-backup scripts).

Use the --run-script-after option in your backup to specify a script to run after backup. Then within the script you can check the environment variable to do something on a successful backup.

I do something like this myself for only when a Backup job runs (not Restores) and when it is also successful:

#!/bin/sh

if [[ "$DUPLICATI__OPERATIONNAME" == "Backup" && "$DUPLICATI__PARSED_RESULT" == "Success" ]] ; then

  do something here

fi
1 Like

So, after some hectic IRL homeowner issues, I’ve finally been able to get back with you concerning the script. I’ve created the script:

#!/bin/sh

if [[ "$DUPLICATI__OPERATIONNAME" == "Media Center Backup" && 
"$DUPLICATI__PARSED_RESULT" == "Success" ]] ; then
  #Backup was successful, update Pi's display
  date +%a\ %b\ %d\ %R\ %Y >/tmp/last_sync && rsync -va --delete -e "ssh -p (somenumber) -i 
/root/key" /tmp/last_sync user@domain:~/last_sync
fi

Obviously, some items sensitive in nature have been removed, but you get the idea.

Anyway, backup has been successful for sometime with the script in place, using the “run-script-after” flag “/root/backup_success.sh”, and the display hasn’t updated. How can I troubleshoot this further?

First, make sure the script is executable: chmod +x /root/backup_success.sh.

Then I usually put in some logging into the script itself, which you can use to see if the script is executed at all, something like:

#!/bin/sh

echo "${date +%a\ %b\ %d\ %R\ %Y} Invoked run script" > /var/log/run-script-debug

if [[ "$DUPLICATI__OPERATIONNAME" == "Media Center Backup" && 
"$DUPLICATI__PARSED_RESULT" == "Success" ]] ; then
  #Backup was successful, update Pi's display
  date +%a\ %b\ %d\ %R\ %Y >/tmp/last_sync && rsync -va --delete -e "ssh -p (somenumber) -i 
/root/key" /tmp/last_sync user@domain:~/last_sync
fi

If the script is executed, you can then debug the actual commands

Got it, looks like the name of my backup was just “Backup”, and I vaguely remember initially naming the backup this, then renaming it to what it is now.

I figured this out by echoing those variables into that log file.