Restoring DNS Zones on a cPanel Server

2.6/5 - (70 votes)

In rare situations, the DNS zones located in /var/named might disappear. We’ve heard of this happening after certain bind package updates, or simply due to administrator error. In either case, it may be possible to restore or at least recreate the missing zones.  Even if the server in question does not act as a nameserver, certain functions within cPanel require the zones to exist locally.  These instructions are geared toward a hosting server (not solely a nameserver) that may or may not be clustered with a cPanel DNSONLY server.  For more information about clustering, see this article.

Restoring from the DNS cluster

 

Firstly, if your server is clustered with another cPanel server, your job is going to be easy. All you have to do is have cPanel sync the zones from one of the other clustered server(s).  Here’s a very simple Python script that will pull out a list of main, addon, and parked domains from the cPanel data and sync them back to the server.  For this to work, you’ll need to make sure your remote DNS cluster is set to “synchronize” status.  To do this, you can either:

 

1) Log into WHM -> Configure Cluster and set the cluster’s role to “Synchronize changes”  OR

2) Run the following command, replacing $ip with the IP address of your DNS cluster (noting also that ‘root’ implies that this is the cluster config for the server itself. If you’re a reseller, replace “root” with your reseller username)

echo -n “sync” > /var/cpanel/cluster/root/config/$ip-dnsrole

Now create a file on your server called, for example, synczones.py and chmod it to 755.  Paste in the following contents:

#!/usr/bin/env python

import os
import subprocess
import yaml

data_dir = '/var/cpanel/userdata'
user_list = os.listdir('/var/cpanel/users')

for user in user_list:
    data_file = '%s/%s/main' % (data_dir, user)

    data_map = None
    try:
        f = open(data_file, 'r')
        data_map = yaml.load(f)
        f.close()
    except:
        pass

    if data_map is not None:
        domains = []

        domains.append(data_map['main_domain'])
        domains.extend(data_map['parked_domains'])

        for addon in data_map['addon_domains']:
            domains.append(addon)

        for domain in domains:
            subprocess.call(['/scripts/dnscluster', 'synczone', domain])

Now, simply run the script and your zones will be copied from the remote cluster to the local server.  If this server is standalone and/or is a nameserver that does not have another clustered server attached to it, you basically only have the choice of either restoring from a backup or recreating the zones.

 

Restoring from Backups

 

Assuming you use the cPanel backup feature and have the system files backed up, you can find them in your cPanel backup directory.  If you’re not sure where they are being stored, look in cPanel -> Configure Backup where the “Backup Destination” value is set. Or simply:

grep BACKUPDIR /etc/cpbackup.conf

Assuming the backup location is /backup, you will find the files in /backup/cpbackup/weekly/dirs (for a weekly backup – it could also be named ‘daily’ or ‘monthly’).

To restore, go to the dirs folder mentioned above and run:

tar -C / -xvf _var_named.tar.gz

 

Recreating Zones

 

If you don’t have backups, your only choice may be to recreate the zones.  To do this, you can use the adddns script provided by cPanel:

/scripts/adddns $domain [$Ip]

If the $ip parameter is not passed, the main shared IP will be used instead.  Keep in mind that if you recreate the zone using this method, a default zone using cPanel’s zone template will be used – so any DNS modifications that were previously made to the zones will be lost.  You can use a similar script to the one above to do all domains on your server:

#!/usr/bin/env python

import os
import subprocess
import yaml

data_dir = '/var/cpanel/userdata'
user_list = os.listdir('/var/cpanel/users')

for user in user_list:
    data_file = '%s/%s/main' % (data_dir, user)

    f = open(data_file, 'r')
    data_map = yaml.load(f)
    f.close()
    domains = []

    domains.append(data_map['main_domain'])
    domains.extend(data_map['parked_domains'])

    for addon in data_map['addon_domains']:
        domains.append(addon)

    for domain in domains:
        subprocess.call(['/scripts/adddns', domain])

3 Comments

  1. Lokesh Jangir Reply

    I do not want to setup cluster for cpanel server. I have two servers master and slave, both are cpanel installed, both servers are in file level sync. Can you please help me how can i sync new account creation and update & DNS zone changes to the slave server.

    Thanks and Regards,
    Lokesh Jangir

Leave a Reply to Vanessa Vasile Cancel reply

Your email address will not be published. Required fields are marked *

Log in