# Run script upon \*\*Successful\*\* backup

**URL:** https://forum.duplicati.com/t/run-script-upon-successful-backup/5274
**Category:** Support
**Created:** [November 12, 2018, 10:41pm UTC](https://forum.duplicati.com/t/run-script-upon-successful-backup/5274 "2018-11-12T22:41:23Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Bigun](https://forum.duplicati.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@Bigun](https://forum.duplicati.com/u/Bigun)
#### Post date: [November 12, 2018, 10:41pm UTC](https://forum.duplicati.com/t/run-script-upon-successful-backup/5274/1 "2018-11-12T22:41:23Z")

</div>

I found [this](https://forum.duplicati.com/t/run-script-after-only-if-backup-finished-how/764/2) 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](http://www.pwnedclips.com/stuff/offsitebackup/last_sync.jpg)

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.

---

<div class="post-metadata">

### Author: ![JonMikelV](https://forum.duplicati.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@JonMikelV](https://forum.duplicati.com/u/JonMikelV)
#### Post date: [November 12, 2018, 10:57pm UTC](https://forum.duplicati.com/t/run-script-upon-successful-backup/5274/2 "2018-11-12T22:57:09Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Bigun](https://forum.duplicati.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@Bigun](https://forum.duplicati.com/u/Bigun)
#### Post date: [November 12, 2018, 11:04pm UTC](https://forum.duplicati.com/t/run-script-upon-successful-backup/5274/3 "2018-11-12T23:04:47Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![drwtsn32](https://forum.duplicati.com/user_avatar/forum.duplicati.com/drwtsn32/32/2591_2.png) [@drwtsn32](https://forum.duplicati.com/u/drwtsn32)
#### Post date: [November 13, 2018, 4:23am UTC](https://forum.duplicati.com/t/run-script-upon-successful-backup/5274/4 "2018-11-13T04:23:39Z")

</div>

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
```

---

<div class="post-metadata">

### Author: ![Bigun](https://forum.duplicati.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@Bigun](https://forum.duplicati.com/u/Bigun)
#### Post date: [November 29, 2018, 10:10am UTC](https://forum.duplicati.com/t/run-script-upon-successful-backup/5274/5 "2018-11-29T10:10:43Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![kenkendk](https://forum.duplicati.com/user_avatar/forum.duplicati.com/kenkendk/32/5305_2.png) [@kenkendk](https://forum.duplicati.com/u/kenkendk)
#### Post date: [November 29, 2018, 10:18am UTC](https://forum.duplicati.com/t/run-script-upon-successful-backup/5274/6 "2018-11-29T10:18:43Z")

</div>

> [@Bigun](#):
>
> 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:

```plaintext
#!/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

---

<div class="post-metadata">

### Author: ![Bigun](https://forum.duplicati.com/letter_avatar_proxy/v4/letter/b/65b543/32.png) [@Bigun](https://forum.duplicati.com/u/Bigun)
#### Post date: [November 29, 2018, 11:24am UTC](https://forum.duplicati.com/t/run-script-upon-successful-backup/5274/7 "2018-11-29T11:24:50Z")

</div>

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.
