The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to remove extra zonefiles from web server that aren't hosted locally?

Discussion in 'General Discussion' started by electric, Oct 28, 2012.

  1. electric

    electric Well-Known Member

    Joined:
    Nov 5, 2001
    Messages:
    697
    Likes Received:
    1
    Trophy Points:
    18
    Hello,

    When we were setting up our DNS Cluster, a mistake was made and all servers were syncing to each other for a while.

    As a result, every web server now has a local copy of every zonefile for every domain on every server. (Several thousands zonefiles on each web server, even though each web server only hosts a few hundred domains...)

    How can we remove all the "extra" zonefiles from each web server?

    The only servers that need a copy of all zonefiles are the cpanel dns-only nameservers. The individual web servers should only have the zonefiles for domains that are hosted on that server.

    Thanks.
     
  2. NixTree

    NixTree Well-Known Member

    Joined:
    Aug 19, 2010
    Messages:
    386
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Gods Own Country
    cPanel Access Level:
    Root Administrator
    Re: How to remove extra zonefiles from web server that aren't hosted locall

    I think you have to do it manually ( checking locally hosted domains and remove the rest; a simple script can do it)...but make sure that you remove the "server" from cluster before removing DNS zone files!
     
  3. electric

    electric Well-Known Member

    Joined:
    Nov 5, 2001
    Messages:
    697
    Likes Received:
    1
    Trophy Points:
    18
    Re: How to remove extra zonefiles from web server that aren't hosted locall

    Anyone from cpanel want to comment on this?

    How can we "clean up" our web servers so that the zonefiles of domains hosted on different servers are removed?
     
  4. electric

    electric Well-Known Member

    Joined:
    Nov 5, 2001
    Messages:
    697
    Likes Received:
    1
    Trophy Points:
    18
    Re: How to remove extra zonefiles from web server that aren't hosted locall

    *bump*

    Should I submit this as a support ticket?
     
  5. electric

    electric Well-Known Member

    Joined:
    Nov 5, 2001
    Messages:
    697
    Likes Received:
    1
    Trophy Points:
    18
    Re: How to remove extra zonefiles from web server that aren't hosted locall

    Can someone from cpanel please review this script and let me know if it will do the job? I am trying to remove all zonefiles from the server that are not locally hosted.

    Thanks.

    Code:
    #!/bin/bash
    IFS="$"
    
    # -----------------------------------------------------------
    #
    # 
    # local_hosted_dns_cleanup.sh
    #
    # This script will remove all zonefiles not hosted 
    # locally on the server.
    #
    # -----------------------------------------------------------
    
    DATE=$(date +"%Y%m%d%H%M")
    BACKUP_FOLDER=/var/named_backup_of_all_zones_${DATE}
    TMP_LOCALZONES_FOLDER=/var/named_localzones_tmp_${DATE};
    
    
    echo "Backing up all current zonefiles to ${BACKUP_FOLDER}";
    mkdir ${BACKUP_FOLDER};
    cp /var/named/*.db ${BACKUP_FOLDER}/;
    
    echo "Making temp folder to hold locally hosted zonefiles";
    mkdir ${TMP_LOCALZONES_FOLDER};
    
    echo "Moving 'localdomains' zonefiles to temp folder";
    
    ### Copy localdomains (zonefiles with local hosted MX records).
    cat /etc/localdomains | while read CPDOMAIN; do 
       if [ -f /var/named/${CPDOMAIN}.db ]; then
          mv /var/named/${CPDOMAIN}.db ${TMP_LOCALZONES_FOLDER}/${CPDOMAIN}.db
       fi
    done
    
    echo "Copying 'removedomains' zonefiles to temp folder";
    
    ### Copy remotedomains (zonefiles with remote hosted MX records).
    cat /etc/remotedomains | while read CPDOMAIN; do 
       if [ -f /var/named/${CPDOMAIN}.db ]; then
          mv /var/named/${CPDOMAIN}.db ${TMP_LOCALZONES_FOLDER}/${CPDOMAIN}.db
       fi
    done
    
    echo "Deleting all remaining zonefiles";
    rm -f /var/named/*.db;
    
    echo "Moving zonefiles from temp folder to /var/named";
    mv ${TMP_LOCALZONES_FOLDER}/* /var/named/;
    
    echo "Restarting BIND";
    /usr/local/cpanel/scripts/restartsrv named
    
    echo "Removing temp folder";
    rmdir ${TMP_LOCALZONES_FOLDER};
    
    echo "Done. This server now has only locally hosted zonefiles."
    exit;
     
  6. electric

    electric Well-Known Member

    Joined:
    Nov 5, 2001
    Messages:
    697
    Likes Received:
    1
    Trophy Points:
    18
    Re: How to remove extra zonefiles from web server that aren't hosted locall

    I cleaned up the script a little and removed unnecessary stuff. Here is the latest:

    Code:
    #!/bin/bash
    IFS="$";
    
    # -----------------------------------------------------------
    #
    # 
    # only_locally_hosted_zonefiles_cleanup.sh
    #
    # This script will remove all zonefiles not hosted 
    # locally on the server.
    #
    # -----------------------------------------------------------
    
    DATE=$(date +"%Y%m%d%H%M")
    BACKUP_FOLDER=/var/named_backup_of_all_zonefiles_${DATE}
    
    echo "Backing up all zonefiles to ${BACKUP_FOLDER}";
    mkdir ${BACKUP_FOLDER};
    cp -p /var/named/*.db ${BACKUP_FOLDER}/;
    
    echo "Removing all zonefiles from /var/named";
    rm -f /var/named/*.db;
    
    echo "Copying back zonefiles listed in 'localdomains' file";
    cat /etc/localdomains | while read CPDOMAIN; do 
       if [ -f ${BACKUP_FOLDER}/${CPDOMAIN}.db ]; then
          cp -p ${BACKUP_FOLDER}/${CPDOMAIN}.db /var/named/;
       fi
    done
    
    echo "Moving back zonefiles listed in 'remotedomains' file";
    cat /etc/remotedomains | while read CPDOMAIN; do 
       if [ -f ${BACKUP_FOLDER}/${CPDOMAIN}.db ]; then
          cp -p ${BACKUP_FOLDER}/${CPDOMAIN}.db /var/named/;
       fi
    done
    
    echo "Done. This server now has only locally hosted zonefiles.";
    
    echo "Attempting to restart BIND";
    /usr/local/cpanel/scripts/restartsrv named;
    
    exit;
     
Loading...

Share This Page