MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //scripts.20110531.215904.25158/archive_sync_zones
#!/usr/bin/perl
# cpanel - archive_sync_zones                     Copyright(c) 2010 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

BEGIN { unshift @INC, '/usr/local/cpanel'; }

require Cpanel::Sys::OS;
require Cpanel::Config::Users;
require Cpanel::Config::LoadCpUserFile;

######[ set defaults based on distro/OS ]##########################################################
my $def_basedir   = '/var/named';
my $def_namedconf = '/etc/named.conf';
my $distro        = Cpanel::Sys::OS::getos();
if ( $distro eq 'freebsd' ) {
    $def_basedir   = '/etc/namedb';
    $def_namedconf = '/etc/namedb/named.conf';
}

open( my $ndc_fh, '<', $def_namedconf ) or die "Could not open $def_namedconf for reading: $!\n";
my @conf_zones = readline($ndc_fh);
close $ndc_fh;

my %zones;
foreach my $line (@conf_zones) {
    if ( $line =~ m/\s*zone\s+["']([\w\-\.]+)["']/ ) {
        next if $1 eq '.';
        my $zone_name = $1;
        next if $zone_name =~ m/^\./;
        next if $zone_name =~ m/\.arpa$/;
        next if ( exists $zones{$zone_name} );    # Seen in another view
        $zones{$zone_name} = 1;

    }
}

if ( opendir my $zone_dh, $def_basedir ) {
    while ( my $file = readdir $zone_dh ) {
        next if $file !~ m/(.+)\.db$/;    # Only look at cPanel managed zone file
        $zone_name = $1;
        next if $zone_name =~ m/\.arpa$/;
        next if exists $zones{$zone_name};
        $zones{$zone_name} = 1;
    }
    closedir $zone_dh;
}

my ($live_domains, $dead_domains) = get_domains_managed_domains();

system 'tar', 'cf', $def_basedir . '/zone_sync_backup.tar', $def_namedconf;
foreach my $zone ( sort keys %zones ) {
    next if ( !$live_domains->{$zone} && !$dead_domains->{$zone} );
    if ( -e $def_basedir . '/' . $zone . '.db' ) {
        system 'tar', '-r', '-f', $def_basedir . '/zone_sync_backup.tar', $def_basedir . '/' . $zone . '.db';
    }
    else {
        print "Unable to archive zone file for $zone\n";
    }
    system '/scripts/dnscluster', 'synczone', $zone;
}

print "Archive of pre-sync zones located at: ${def_basedir}/zone_sync_backup.tar\n";

sub get_domains_managed_domains {
    my %live_domains;
    my %dead_domains;
    foreach my $user ( Cpanel::Config::Users::getcpusers() ) {
        my $cpuser_ref = Cpanel::Config::LoadCpUserFile::loadcpuserfile($user);

        if ( $cpuser_ref->{'DOMAIN'} ) {
            $live_domains{ $cpuser_ref->{'DOMAIN'} } = 1;
        }
        if ( ref $cpuser_ref->{'DOMAINS'} eq 'ARRAY' ) {
            my $_tmp = $cpuser_ref->{'DOMAINS'};
            map { $live_domains{$_} = 1 } @$_tmp;
        }

        if ( ref $cpuser_ref->{'DEADDOMAINS'} eq 'ARRAY' ) {
            my $_tmp = $cpuser_ref->{'DEADDOMAINS'};
            map { $dead_domains{$_} = 1 } @$_tmp;
        }
    }
    return ( \%live_domains, \%dead_domains );
}