Migrating VPS with virtualmin – Multiple DNS changes


We have had a customer move from one host to a new one with Virtualmin, this is usually ok, except in this case he had DNS hosted on his own VPS, most of it pointing to his old IP address.
The task was to update all DNS records on his VPS without manually clicking each domain and editing it (he had lots).
This was how I ended up doing it.

#!/bin/bash
OLDIP=74.50.xx.xxx
NEWIP=49.50.xxx.x

virtualmin list-domains --with-feature dns --name-only | while read DOMAIN ; do
        virtualmin get-dns --domain $DOMAIN | grep $OLDIP |grep -v SPF | while read DNSLINE ; do
                REC=`echo $DNSLINE | awk '{print $1}'`
                virtualmin modify-dns --domain $DOMAIN --remove-record "$REC A"
                virtualmin modify-dns --domain $DOMAIN --add-record "$REC A $NEWIP"
                done
        done

This did not update the SPF records, just the A records. Since his SPF records were all the same I was able to automate that with the following slightly modified version of the above.
note: SPF records are not always the full IP in virtualmin, they often lop off the last segment

#!/bin/bash
OLDIP=74.50.xx.xxx
NEWIP=49.50.xxx.x

virtualmin list-domains --with-feature dns --name-only | while read DOMAIN ; do
        virtualmin get-dns --domain $DOMAIN | grep $OLDIP |grep SPF | while read DNSLINE ; do
                REC=`echo $DNSLINE | awk '{print $1}'`
                virtualmin modify-dns --domain $DOMAIN --remove-record "$REC SPF"
                virtualmin modify-dns --domain $DOMAIN --add-record "$REC SPF v=spf1 a mx a:$DOMAIN ipv4:$NEWIP"
                done
        done