将AWS Router53 DNS 导出文件转换成兼容 BIND的区域文件并导入到Azure DNS

在某些场景中,需要将AWS上的DNS迁移到其他服务商,如Azure,Dodaddy,Cloudflare等,AWS导出的DNS区域文件为Json格式,需要将其将换成兼容 BIND 的区域文件格式(RFC1035)。

AWS DNS区域文件格式:

{
"ResourceRecordSets": [
{
"Name": "mooing.cc.",
"Type": "NS",
"TTL": 3600,
"ResourceRecords": [
{
"Value": "ns-590.awsdns-09.net."
},
{
"Value": "ns-1393.awsdns-46.org."
},
{
"Value": "ns-339.awsdns-42.com."
},
{
"Value": "ns-1767.awsdns-28.co.uk."
}
]
},
{
"Name": "test.mooing.cc.",
"Type": "A",
"TTL": 300,
"ResourceRecords": [
{
"Value": "1.1.1.1"
}
]
},
{
"Name": "test2.mooing.cc.",
"Type": "CNAME",
"TTL": 300,
"ResourceRecords": [
{
"Value": "www.baidu.com."
}
]
}
}

兼容 BIND 的区域文件格式(RFC1035):

test.mooing.cc. 300 IN  A 1.1.1.1
test2.mooing.cc. 300 IN CNAME www.baidu.com.

编写了一个脚本,供大家参考:

需要注意某些记录(如例子中的NS记录),可能对应多个值,所以需要用foreach循环进行遍历。

$FolderLocation="D:MooingDomains"  #设置AWS导出的Domain文件夹
$ZoneLocation="D:MooingZone" #设置转换后的文件夹
$Domains=Get-ChildItem $FolderLocation
foreach ($domain in $Domains)
{
cd $FolderLocation
$DomainName=Get-Content $FolderLocation$domain -Raw | ConvertFrom-Json
$Records=$DomainName.ResourceRecordSets

foreach ($Record in $Records)
{

if ($Record.ResourceRecords.Count -eq 1)
{
$Record.Name + " " + $Record.ttl + " IN " + $Record.type + " " + $Record.ResourceRecords.Value.replace("","") |Out-File $ZoneLocation$domain.Zone.txt -Append utf8
}
else
{
$i=0
while($i -lt $Record.ResourceRecords.Count) {
$Record.Name + " " + $Record.ttl + " IN " + $Record.type + " " + $Record.ResourceRecords.Value[$i].replace("","") |Out-File $ZoneLocation$domain.Zone.txt -Append utf8
$i++
}
}
}
}

转换完成之后,即可导入支持Bind格式的服务商,Azure导入方式请参考:

发表评论

相关文章