Script for disabling urbackupclient when connecting to a certain wlan connection

I just publish a short little script here for anybody who needs from time to time to use a metered connection like the hotspot of his mobile phone. This script will disable urbackupclient temporarly. We just had an other blackout and urbackupclient on my laptop ate all my mobile data in less than 15 minutes.
It works on debian, but I guess it should work on most other distributions, too.
Copy following script into /etc/network/if-up.d/ and give it a name like “urbackclient”, and “chmod +x” - it:

#!/bin/bash

ssid=$(iwgetid -r)
if [ "$ssid" == "WLAN-Network-Name" ]; then
    systemctl stop urbackupclientbackend
elif ! systemctl is-active urbackupclientbackend > /dev/null ; then
    systemctl start urbackupclientbackend
fi

Replace WLAN-Network-Name with the ssid of the wlan over which you don’t want to do backups. The script will also restart urbackupclient when on any other connection (should also work when you connect to an ethernet connection, but I can’t test it). It also works when you use Network-Manager.

I know it’s a ridiculously short script. But maybe it helps somebody else. It took me 25 minutes (including finding out where to put it) to write it. I still remember how to do comparisons and conditional jumps in assembler on Sparc architecture, but when ever I have to do it in Bash (or any other shell) it takes me forever to get it working…

I’m never happy, before I can’t generalize functions. I don’t only want not to eat up my own mobile data, but neither those of my friends, when they borrow me their connection.
So I remembered that android (I don’t know about iOS), send a certain value over dhcp to mark the connection as metered. So the following script will detect that but only if you use network-manager. I use the same script to also disable nextcloud synchronization.

#!/bin/bash

default_route=$(ip route list 0/0 | sed -r 's/.*dev (\S*).*/\1/g')

if nmcli -t -f GENERAL.METERED dev show $default_route |  grep -q yes ; then
    systemctl stop urbackupclientbackend
elif ! systemctl is-active urbackupclientbackend > /dev/null ; then
    systemctl start urbackupclientbackend
fi

Or/and you add your mobile provider to https://github.com/uroni/dataplan_db

yes, but that only works, as long as I don’t change mobile provider (changing country, using a friends connection, etc.).
How important is the MB setting for the dataplan_db? My current provider has offers between 20MB (yes MB) to 40GB of data you can transfer…