Skip to content

Instantly share code, notes, and snippets.

@jbransen
Last active September 10, 2023 14:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jbransen/eeca81fac46e1fc29f235277b41bf5d0 to your computer and use it in GitHub Desktop.
Save jbransen/eeca81fac46e1fc29f235277b41bf5d0 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 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);
{
"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

Deprecation warning: require.Badcow/DNS is invalid, it should not contain uppercase characters. Please use badcow/dns instead. Make sure you fix this as Composer 2.0 will error.

@ramonfincken
Copy link

ramonfincken commented Mar 23, 2021

Changed to make it work, thanks for the setup!

https://gist.github.com/ramonfincken/a972dfdc3115e4c7b79d4720a0461af7

@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