4u123

Well-Known Member
PartnerNOC
Jan 2, 2006
877
15
168
Hi, I'm looking for a way to bulk change all MX records for domains listed in /etc/localdomains back to %domain%

I've seen threads on this forum for using find/replace on DNS records and I know how to update the timestamps etc. The part I'm unsure about is how to add each individual domain back in via a bulk operation.

The reason I need to do this is because I have previously been using an external Email cluster for all inbound mail, therefore setting all MX records to mx1.external.com and mx2.external.com (this was done via a plugin).

What I need to do for all domains that appear in /etc/localdomains is remove any instance of mx1.external.com and mx2.external.com from each zone file and set up a new MX with priority 0 to %domain% (the actual domain itself).

Any help would be greatly appreciated. I'm sure others would also benefit from an answer to this.
 

cPanelMichael

Technical Support Community Manager
Staff member
Apr 11, 2011
47,911
2,234
363
cPanel Access Level
DataCenter Provider
Twitter
Hello @4u123,

Have you considered using the Reset A DNS Zone functionality for the domain names in-question? This will revert the MX entries to the default value. However, all custom entries are also lost (except for TXT records). You'd simply run a loop that runs the following WHM API 1 command for all domain names in the /etc/localdomains file:

Code:
whmapi1 resetzone domain=example.com
If you are concerned about losing other custom DNS changes to the zones, then you could instead use a custom bash loop command that runs the "replace" command for all domain names in /etc/localdomains like the examples on the following posts:

Bulk DNS insert
How to remove a few DNS records from all zone files?

Thank you.
 

4u123

Well-Known Member
PartnerNOC
Jan 2, 2006
877
15
168
Thanks Michael

I don't want to reset any zones.
I've read those threads as mentioned.

I'm trying to figure out how to insert the MX record for each domain that contains the domain name itself. That's what I'm stuck on here. I suppose other than asking someone to write a script to do it, there isn't an easier way.
 

cPanelMichael

Technical Support Community Manager
Staff member
Apr 11, 2011
47,911
2,234
363
cPanel Access Level
DataCenter Provider
Twitter
I'm trying to figure out how to insert the MX record for each domain that contains the domain name itself. That's what I'm stuck on here.
Hello @4u123,

Can you provide some more information to explain how the example scripts are not suitable for this action? For instance, if you are already defining the domain name in /etc/localdomains as "$X", and you want the MX record restored to the domain name, then you should be able to use the "$X" variable in the bash script similar to how it's utilized in the linked threads.

If you provide me with a specific example (using a fake domain name) of the exact DNS entries you need to modify, I can provide you with an example script.

Thank you.
 

4u123

Well-Known Member
PartnerNOC
Jan 2, 2006
877
15
168
Thanks again Michael.

The entire requirement is this...

For each zone file in /var/named that matches a domain in /etc/localdomains...

1. Remove existing MX records that will match mx1.external.com and mx2.external.com.
2. Create new MX record with priority 0 for the matching domain within its own zone file.
3. Update the serial number.

Having re-read the threads you linked to, I can see in the example how I would add the new MX record in, so thank you for that.

Not sure how to remove the existing MX record lines, some of them would have different TTL's. So I'd need to remove an entire line if it contained only the matching MX records and only for domains that exist in /etc/localdomains.

Same for serial number, I'd only want to update the serials within files that are in /etc/localdomains
 

cPanelMichael

Technical Support Community Manager
Staff member
Apr 11, 2011
47,911
2,234
363
cPanel Access Level
DataCenter Provider
Twitter
Hello @4u123,

1. Can you let me know which priority values are configured for the two MX records you want to remove from the zone files?

2. 14400 is the default TTL value for the MX records. Do a significant number of domain names use something other than 14400, and if so, do those domain names all use the same alternate TTL value? If so, we can copy /etc/localdomains to a new file name (e.g. /etc/localdomains-14400), remove any domains with zones that don't make use of 14400, and run a command like the one in the example post linked earlier against all domain names in the /etc/localdomains-14400 file. We'd then create a separate file for domain names using a different TTL value for the MX records (e.g. /etc/localdomains-18000) and remove those DNS entries as part of a separate command.

Thank you.
 

4u123

Well-Known Member
PartnerNOC
Jan 2, 2006
877
15
168
Actually I think it would be easier just to perform a simple replace of the hostnames in the MX records. It will result in two MX records with the same hostname but would be simpler to achieve I think.

So for each domain name listed in /etc/localdomains I'd need to match up with the zone file in /var/named and find the string mx1.external.com and mx2.external.com and replace each of those with the domain name associated with that zone file. The TTL on the MX is then of no consequence.

Then I'd need to update the serials.
 

4u123

Well-Known Member
PartnerNOC
Jan 2, 2006
877
15
168
This might be even easier. I'm working on the premise that only the domains using those MX records will need changing, so as long as they exist within a zone file, I don't need to use /etc/localdomains to identify them.

I hadn't considered that merely the presence of the MX records themselves identify the files that need changing.

Do you think this will work?



Code:
cp -rf /var/named /var/named.bk

cd /var/named

for i in $(ls *.db | sed 's/.db//g') ; do sed -i.bak "s/mx1.remote.com/$i/g" $i.db ; done
for i in $(ls *.db | sed 's/.db//g') ; do sed -i.bak "s/mx2.remote.com/$i/g" $i.db ; done

find /var/named/*.db -mtime -0.08 -exec perl -pi -e 'if (/^\s+(\d{10})\s+;\s+serial/i) { my $i = $1+1; s/$1/$i/;}' '{}' \;
 

cPanelMichael

Technical Support Community Manager
Staff member
Apr 11, 2011
47,911
2,234
363
cPanel Access Level
DataCenter Provider
Twitter
Hi @4u123,

I setup a couple of DNS zones on a test server with MX entries pointing to "mx1.remote.tld" and "mx2.remote.tld" and ran the commands you provided. The commands successfully replaced "mx1.remote.tld" and "mx2.remote.tld" with "$domain.tld".

Thank you.