mydns_to_powerdns_migration.php 3.3 KB
Newer Older
1 2 3 4
<?php
$host="localhost";
$user="root";
$password="MYSQL-ROOT-PASSWD";
5
mysql_connect($host, $user, $password) or die(mysql_error());
6 7 8 9 10 11

mysql_select_db("dbispconfig");
$sql1 = mysql_query("SELECT id, substr(origin,1, LENGTH(origin)-1) AS origin, substr(ns,1, LENGTH(ns)-1) AS ns, substr(mbox,1, LENGTH(mbox)-1) AS mbox,ttl FROM dns_soa order by id asc;");
mysql_select_db("powerdns");
while($row1 = mysql_fetch_array($sql1))
{
12 13
	mysql_query("INSERT INTO domains (id,name,type,ispconfig_id) values ('$row1[id]','$row1[origin]','NATIVE','$row1[id]');");
	mysql_query("INSERT INTO records (domain_id,name,content,ispconfig_id,type,ttl,prio,change_date) values ('$row1[id]','$row1[origin]','$row1[ns] $row1[mbox] 0','$row1[id]','SOA','$row1[ttl]','0','1260446221');");
14 15 16 17 18 19 20
}

mysql_select_db("dbispconfig");
$sql2 = mysql_query("SELECT id,zone,name,data,aux,ttl,type FROM dns_rr order by id asc;");
mysql_select_db("powerdns");
while($row2 = mysql_fetch_array($sql2))
{
21 22 23 24 25 26 27 28 29 30 31 32 33
	if (strlen($row2['name']))
	{
		$file1=substr($row2['data'], -1);
		if ($file1==".")
		{
			$text = $row2['data'];
			$laenge = strlen($row2['data'])-1;
			$file2 = substr($text, 0, strlen($text)-1);
		}
		else
		{
			$file2=$row2['data'];
		}
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

                //
                // Fix for 'domain.ext.' apex notation
                //
                $record_name_end=substr($row2['name'], -1);
                if ($record_name_end==".")
                {
                       // remove trailing dot from apex
                       $record_name = substr($row2['name'], 0, strlen($row2['name'])-1);
                }
                else
                {
                       // add domain to make it a fqdn
                       $record_name = $row2['name'] . "." . $row3['origin'];
                }

                print "$row2[name].$row3[origin]" . " $record_name\r\n"; 
51 52 53 54
		mysql_select_db("dbispconfig");
		$sql3 = mysql_query("SELECT substr(origin,1, LENGTH(origin)-1) AS origin FROM dns_soa where id=$row2[zone];");
		$row3 = mysql_fetch_array($sql3);
		mysql_select_db("powerdns");
55
		mysql_query("INSERT INTO records (domain_id,name,content,ispconfig_id,type,ttl,prio,change_date) values ('$row2[zone]','$record_name','$file2','$row2[id]','$row2[type]','$row2[ttl]','$row2[aux]','1260446221');");
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	}
	else
	{
		$file1=substr($row2['data'], -1);
		if ($file1==".")
		{
			$text = $row2['data'];
			$laenge = strlen($row2['data'])-1;
			$file2 = substr($text, 0, strlen($text)-1);
		}
		else
		{
			$file2=$row2['data'];
		}
		mysql_select_db("dbispconfig");
		$sql3 = mysql_query("SELECT substr(origin,1, LENGTH(origin)-1) AS origin FROM dns_soa where id=$row2[zone];");
		$row3 = mysql_fetch_array($sql3);
		mysql_select_db("powerdns");
		mysql_query("INSERT INTO records (domain_id,name,content,ispconfig_id,type,ttl,prio,change_date) values ('$row2[zone]','$row3[origin]','$file2','$row2[id]','$row2[type]','$row2[ttl]','$row2[aux]','1260446221');");
	}
76 77 78 79 80 81 82
}

mysql_select_db("powerdns");
$sql4 = mysql_query("SELECT records.id,records.content,records.type,domains.name FROM records,domains where records.domain_id=domains.id and records.content NOT LIKE '%.%' and (records.type='CNAME' or records.type='NS' or records.type='MX') order by domain_id asc;");

while($row4 = mysql_fetch_array($sql4))
{
83
	mysql_query("UPDATE records SET content = '$row4[content].$row4[name]' where id='$row4[id]';");
84 85 86
}


87
?>