.. because a friend of mine wants to migrate his nameservers from powerdns to bind9, i wrote two really evil but working scripts to help him creating the zones from the database of powerdns..
the first is for the primary nameserver, it will create the zones available at the database and a file to include the zones into the named.conf
< ?
$serial = 2005120801;
$ttl = 604800;
$refresh = 28800;
$retry = 7200;
$min = 39600;
$dbh = "localhost";
$dbd = "powerdns";
$dbu = "powerdns";
$dbp = "";
$destdir = "./test";
mkdir($destdir, 0755);
mkdir($destdir."/zones", 0755);
mysql_connect($dbh, $dbu, $dbp);
mysql_select_db($dbd);
$domains = mysql_query("select id,name from domains");
while($dom = mysql_fetch_assoc($domains)) {
$zones[] = $dom[name];
unset($soa); unset($ns); unset($mx); unset($r); unset($cn);
unset($cnames); unset($arecords); unset($sdom_exists);
unset($ptr); unset($r6);
$zone = mysql_query("select * from records where domain_id=".$dom[id]);
while($row=mysql_fetch_assoc($zone))
switch($row[type]) {
case "SOA": $soa[] = array("domain"=>$row[name], “content”=>$row[content]); break;
case “NS”: $ns[] = array(”ns”=>$row[content]); break;
case “MX”: $mx[] = array(”name”=>$row[name], “mx”=>$row[content], “prio”=>$row[prio]); break;
case “A”: $r[] = array(”name”=>$row[name], “dest”=>$row[content]); break;
case “PTR”: $ptr[] = array(”name”=>$row[name], “dest”=>$row[content]); break;
case “AAAA”: $r6[] = array(”name”=>$row[name], “dest”=>$row[content]); break;
case “CNAME”: $cn[] = array(”name”=>$row[name], “dest”=>$row[content]); break;
}
$soa = $soa[0];
$domain = $soa[domain];
$content = explode(” “, $soa[content]);
$file = “\$TTL “.$ttl.”\n”;
$file .= “\$ORIGIN .\n”;
$file .= $domain.”\tIN SOA “.$content[0].”. “.$content[1].”. (\n\t\t”.$serial.”\n\t\t”.$refresh.”\n\t\t”.$retry.”\n\t\t”.$ttl.”\n\t\t”.$min.”\n\t\t)\n”;
foreach($ns as $nr)
$file .= “\t\tNS\t”.$nr[ns].”.\n”;
$file .= “\n”;
foreach($mx as $mr)
$file .= $mr[name].” \tMX\t”.$mr[prio].” “.$mr[mx].”.\n”;
$file .= “\n”;
$file .= “\$ORIGIN “.$domain.”.\n”;
foreach($cn as $cr) {
$subdom = explode($domain, $cr[name]);
$sdom = rtrim($subdom[0],”.”);
if ($sdom == “”) $sdom = “@”;
$cnames .= $sdom.” \t\tIN CNAME\t”.$cr[dest].”.\n”;
$sdom_exists[$sdom] = “true”;
}
foreach($r as $rr) {
$subdom = explode($domain, $rr[name]);
$sdom = rtrim($subdom[0],”.”);
if ($sdom == “”) $sdom = “@”;
if ($sdom_exists[$sdom] != “true”) $arecords .= $sdom.” \t\tIN A\t”.$rr[dest].”\n”;
}
foreach($r6 as $rr6) {
$subdom = explode($domain, $rr6[name]);
$sdom = rtrim($subdom[0],”.”);
if ($sdom == “”) $sdom = “@”;
if ($sdom_exists[$sdom] != “true”) $arecords .= $sdom.” \t\tIN AAAA\t”.$rr6[dest].”\n”;
}
$file .= $arecords;
$file .= $cnames;
foreach($ptr as $p) {
$subdom = explode($domain, $p[name]);
$sdom = rtrim($subdom[0],”.”);
if ($sdom == “”) $sdom = “@”;
if ($sdom_exists[$sdom] != “true”) $file .= $sdom.” \t\tIN PTR\t”.$p[dest].”.\n”;
}
$file .= “\n”;
$fp = fopen($destdir.”/zones/db.”.$domain, ‘w’);
fwrite($fp, $file);
fclose($fp);
}
$fp = fopen($destdir.”/named.zones.conf”, ‘w’);
$zonefile = “; zones to load\n\n”;
foreach($zones as $zone)
$zonefile .= “\nzone \”".$zone.”\” {\n\ttype master;\n\tfile \”zones/db.”.$zone.”\”;\n};\n”;
fwrite($fp, $zonefile);
fclose($fp);
?>
and the second one, for the slaves - creates just the file to include the zones into the slave bind9 (+ masterserver)..
< ?
$dbh = "localhost";
$dbd = "powerdns";
$dbu = "powerdns";
$dbp = "";
$destdir = "./test";
$master = "127.0.0.1";
mkdir($destdir, 0755);
mkdir($destdir."/zones", 0755);
mysql_connect($dbh, $dbu, $dbp);
mysql_select_db($dbd);
$domains = mysql_query("select id,name from domains");
while($dom = mysql_fetch_assoc($domains))
$zones[] = $dom[name];
$fp = fopen($destdir."/named.zones.conf", 'w');
$zonefile = "; zones to load\n\n";
foreach($zones as $zone)
$zonefile .= "\nzone \"".$zone."\" {\n\ttype slave;\n\tfile \"zones/db.".$zone."\"; masters { ".$master."; }; \n};\n";
fwrite($fp, $zonefile);
fclose($fp);
?>