# Get UnifiedPush notification from Borg backup job

**URL:** https://forum.yunohost.org/t/get-unifiedpush-notification-from-borg-backup-job/31018
**Category:** Tutorials
**Tags:** ntfy, tutorial, borg-app, english
**Created:** [September 4, 2024, 5:02am UTC](https://forum.yunohost.org/t/get-unifiedpush-notification-from-borg-backup-job/31018 "2024-09-04T05:02:24Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![ghose](https://forum.yunohost.org/user_avatar/forum.yunohost.org/ghose/32/10939_2.png) [@ghose](https://forum.yunohost.org/u/ghose)
#### Post date: [September 4, 2024, 5:02am UTC](https://forum.yunohost.org/t/get-unifiedpush-notification-from-borg-backup-job/31018/1 "2024-09-04T05:02:24Z")

</div>

adapted from gemini blog for personal reference (in galician)

## Borg backup «result» in your mobile device

if you are using [borg](https://github.com/YunoHost-Apps/borg_ynh) as backup method in your YNH this is the info you get in the GUI _🏠/ Apps / Borg_

 ![image](https://forum.yunohost.org/uploads/default/original/2X/9/93b6c6d6939afaa07dda5f3de6282da5b09e042f.png)

So we can read that **Backup State and Last Run** values and send them as a [UnifiedPush](https://unifiedpush.org/) notification to our mobile device.

We can notify _every result_, or limit the amount to just **failed** state, this is a basic configuration to test (it works!) and be a draft/model to send any info or alerts from our server.

## Requirements

- borg backup service in your YNH
- a [distributor](https://unifiedpush.org/users/distributors/) in your mobile device. I’m using [ntfy](https://f-droid.org/packages/io.heckel.ntfy/) in this example
- public [ntfy server topic](https://ntfy.sh/app) or **optional** ¹,² [ntfy ynh package](https://github.com/YunoHost-Apps/ntfy_ynh) with custom config

### create ntfy topic

- go to https:ntfy.sh/app
- click on _+ Subscribe topic_ on left panel menu
- click on _Create name_ button to get a randomized name **you have to use later in the script**
- copy this _topic_ URL  
you get something like `https://ntfy.sh/app/random-NTFY-TOPIC`

 ![pantallazo_20240904_070059](https://forum.yunohost.org/uploads/default/original/2X/6/6fd9d802e41ea63d0907ba7448493c97f46208f4.png)

you can now install android’s ntfy app from f-droid and add this topic (“+” button) and test it (just publish any text in your browser and it should appear in your mobile app)

## bash and crontab

the whole process is just a bash script that reads borg state and a cron job to send it to our mobile device.

**my** main goal with this script is getting a notification **if borg state is «failed»** for any reason, so I get an alert first thing in the morning. I can debug it later, I don’t need to include details about the problem (from logs) but you can add it to this notification.

### bash script

`sudo su -` to be root user and then `nano /home/USER/ntfy-borg`

copy and paste this:

```auto
#!/bin/bash

## owned by root and added to root's crontab

## read borg state, only root user can

estado=$(sudo yunohost app setting borg state)
ultima=$(sudo yunohost app setting borg last_run)

## these are the values we read in web admin interface

## setting priority to later manage/filter notifications in mobile app

if [$estado == successful]; then
        prio="low"
else
        prio="max"
fi

## «priority» can also be set using numbers (1-5)
## the job itself, barely adapted from ntfy documentation, using ntfy.sh/app 

notify () {
    # $prio = priority (eg: default)
    # $1 = tags (eg: +1)
    # $2 = message (eq "Success")
    curl \
        -H "Title: Borg result from ${ultima}" \
        -H "Priority: ${prio}" \
        -H "Tags: borg,${1}" \
        -d "${2}" \
        https://ntfy.sh/app/random-NTFY-TOPIC
}

## if you have several servers or run other jobs to the same topic it can be useful to tag them (YNH, updates, etc)

notify "YNH" "Today's Borg result was ${estado}"

exit

```

save and exit and make it executable

`chmod +x ntfy-borg`

### add it to root’s crontab

```auto
cd
crontab -e

```

adding this job

```auto
## send notification every day at 8AM
0 8 * * * /home/USER/ntfy-borg

```

and that’s it, you can run the script to test it or wait until tomorrow at 8AM (server’s time)

### filter notifications

as said, I just want a notification when borg fails, so using app settings you can set being notified “when priority is over X value” and not get repetitive notifications when everything works just as it should.

Not so dificult, but useful to **me** so I have not to manually check borg result. I hope this helps you to further add new alerts or notification services.

### suggestion to Borg-ynh packagers

**It could also be added** to borg package default configuration as is _email notification_ (much more dificult to correctly config email server, in my experience), a simple checkbox that enables a input area to enter topic’s URL (from ntfy or own server).

I’ve also tried to manually add some _curl_ to borg’s job config, but I’ve failed and more likely it will be modifed after every borg app update.

* * *

¹ I’ve installed YNH package to use my own server  
² add token parameter `-u :tk_5al1bk--token-value--q6ckpq \` as auth method if you use your own server and it is password protected (as it should for personal use). You can create as many access tokens in the web interface of your ntfy server
