That script seems excessively complicated for just the purpose of maintaining a working inbound route.
Try instead just pinging your router once every 10 minutes like this;
ip route show | grep default | cut -d\ -f3
while ( true ); do
ping -c1 $GWIP
The part of that that mangled into code tags is within a pair of backticks, which inconveniently, are the “code” delimiters for this forum software.
But ping is pretty light, especially operating on a local network, so you might even prefer dropping the sleep lower than that.
As for sending an email with your IP address… your db’s ip address shouldn’t change on account of this glitch. If it does, then your ROUTER is misconfigured. A much better option would be to assign a hostname to the db’s MAC address in your router’s DNS settings, and use that instead of the super messy email approach. I.e., OpenWRT and similar use dnsmasq for DHCP and DNS, so it automatically sets up a DNS entry for every IP address it hands out. As long as EITHER (a) your device provides a non-duplicated hostname, or (b) you assign a hostname to the MAC address, then you have a DNS record available from the router for that device.