Skip to content

Instantly share code, notes, and snippets.

@ramonfincken
Forked from jbransen/a-transip-zone-export.php
Last active April 3, 2024 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramonfincken/a972dfdc3115e4c7b79d4720a0461af7 to your computer and use it in GitHub Desktop.
Save ramonfincken/a972dfdc3115e4c7b79d4720a0461af7 to your computer and use it in GitHub Desktop.
Exporting a DNS Zone file from the TransIP API
<?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);
{
"require" : {
"transip/transip-api-php" : "dev-master",
"badcow/dns": "^1.0"
},
"repositories": [
{
"type" : "vcs",
"url" : "git@github.com:transip/transip-api-php.git"
}
]
}
@ramonfincken
Copy link
Author

ramonfincken commented Mar 23, 2021

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

@matthijs166
Copy link

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