-
-
Save ramonfincken/a972dfdc3115e4c7b79d4720a0461af7 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 Transip\Api\Library\TransipAPI; | |
use Transip\Api\Library\Entity\Domain\DnsEntry as DnsEntry; | |
use Badcow\DNS\Zone; | |
use Badcow\DNS\Rdata\Factory; | |
use Badcow\DNS\ResourceRecord; | |
use Badcow\DNS\AlignedBuilder; | |
$generateWhitelistOnlyTokens = true; | |
// Login details, fill those in with your TransIP username, the file containing | |
// your private key, and the domain you would like to export | |
$login = ""; | |
$privateKey = file_get_contents( '' ); | |
$domainName = ''; | |
$api = new TransipAPI( | |
$login, | |
$privateKey, | |
$generateWhitelistOnlyTokens | |
); | |
$dnsentries = $api->domainDns()->getByDomainName( $domainName ); | |
//print_r($dnsentries); | |
// Construct the zone file | |
$zone = new Zone($domainName . '.'); | |
$zone->setDefaultTtl(1); // 1 means automatic | |
foreach( $dnsentries as $dnsEntry) { | |
$rr = new ResourceRecord; | |
$rr->setName($dnsEntry->getName()); | |
switch($dnsEntry->getType()) { | |
case DnsEntry::TYPE_A: | |
$rr->setRdata(Factory::A($dnsEntry->getContent())); | |
break; | |
case DnsEntry::TYPE_AAAA: | |
$rr->setRdata(Factory::Aaaa($dnsEntry->getContent())); | |
break; | |
case DnsEntry::TYPE_CNAME: | |
$rr->setRdata(Factory::Cname($dnsEntry->getContent())); | |
break; | |
case DnsEntry::TYPE_MX: | |
list($pref,$cont) = explode(' ', $dnsEntry->getContent()); | |
$rr->setRdata(Factory::Mx($pref, $cont)); | |
break; | |
case DnsEntry::TYPE_TXT: | |
$rr->setRdata(Factory::Txt($dnsEntry->getContent())); | |
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" : "[email protected]: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
Please note that the TTL " $zone->setDefaultTtl(1); // 1 means automatic " will actually set it to one (1)
You might like to set it to 600 or 86400