Get UnifiedPush notification from Borg backup job

adapted from gemini blog for personal reference (in galician)

Borg backup «result» in your mobile device

if you are using borg as backup method in your YNH this is the info you get in the GUI :house:/ Apps / Borg

So we can read that Backup State and Last Run values and send them as a UnifiedPush 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

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

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:

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

cd
crontab -e

adding this job

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

3 Likes