Last active
September 10, 2023 14:34
-
-
Save jbransen/eeca81fac46e1fc29f235277b41bf5d0 to your computer and use it in GitHub Desktop.
Exporting a DNS Zone file from the TransIP API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__ . '/vendor/autoload.php'; | |
use Badcow\DNS\Zone; | |
use Badcow\DNS\Rdata\Factory; | |
use Badcow\DNS\ResourceRecord; | |
use Badcow\DNS\AlignedBuilder; | |
// Login details, fill those in with your TransIP username, the file containing | |
// your private key, and the domain you would like to export | |
Transip_ApiSettings::$login = "<username>"; | |
Transip_ApiSettings::$privateKey = file_get_contents('<private_key_file>'); | |
$domainName = '<yourdomain.com>'; | |
// Get the domain info | |
$domain = Transip_DomainService::getInfo($domainName); | |
// Construct the zone file | |
$zone = new Zone($domainName . '.'); | |
$zone->setDefaultTtl(1); // 1 means automatic | |
foreach($domain->dnsEntries as $dnsEntry) { | |
$rr = new ResourceRecord; | |
$rr->setName($dnsEntry->name); | |
switch($dnsEntry->type) { | |
case Transip_DnsEntry::TYPE_A: | |
$rr->setRdata(Factory::A($dnsEntry->content)); | |
break; | |
case Transip_DnsEntry::TYPE_AAAA: | |
$rr->setRdata(Factory::Aaaa($dnsEntry->content)); | |
break; | |
case Transip_DnsEntry::TYPE_CNAME: | |
$rr->setRdata(Factory::Cname($dnsEntry->content)); | |
break; | |
case Transip_DnsEntry::TYPE_MX: | |
list($pref,$cont) = explode(' ', $dnsEntry->content); | |
$rr->setRdata(Factory::Mx($pref, $cont)); | |
break; | |
case Transip_DnsEntry::TYPE_TXT: | |
$rr->setRdata(Factory::Txt($dnsEntry->content)); | |
break; | |
default: | |
throw new \RuntimeException('Migrating ' . $dnsEntry->type . ' records is not implemented'); | |
} | |
$zone->addResourceRecord($rr); | |
} | |
echo AlignedBuilder::build($zone); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require" : { | |
"transip/transip-api-php" : "dev-master", | |
"Badcow/DNS": "^1.0" | |
}, | |
"repositories": [ | |
{ | |
"type" : "vcs", | |
"url" : "git@github.com:transip/transip-api-php.git" | |
} | |
] | |
} |
Also made a repo with all the necessary deps and docs:
https://github.com/matthijs166/transip-dns-zone-exporter
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changed to make it work, thanks for the setup!
https://gist.github.com/ramonfincken/a972dfdc3115e4c7b79d4720a0461af7