Guest User

Untitled

a guest
Sep 13th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * Reads a powerDNS mysql database and generates commands to load the data into route53 via cli53
  5. * User: mrunkel
  6. * Date: 3/25/14
  7. * Time: 2:50 PM
  8. */
  9.  
  10. $shortOptions = "u::"; //optional mysql username
  11. $shortOptions .= "p::"; // optional mysql password
  12. $shortOptions .= "d::"; // optional database name
  13. $shortOptions .= "h::"; // optional hostname
  14. $shortOptions .= "z:"; // required zone to transfer.
  15. $options = getopt($shortOptions);
  16.  
  17. if (array_key_exists('u',$options)) {
  18. $user = $options['u'];
  19. } else {
  20. $user = 'root';
  21. }
  22.  
  23. if (array_key_exists('p',$options)) {
  24. $password = $options['p'];
  25. } else {
  26. $password = 'root';
  27. }
  28.  
  29. if (array_key_exists('h',$options)) {
  30. $hostname = $options['h'];
  31. } else {
  32. $hostname = 'localhost';
  33. }
  34.  
  35. if (array_key_exists('d',$options)) {
  36. $database = $options['d'];
  37. } else {
  38. $database = 'pdns';
  39. }
  40.  
  41. if (array_key_exists('z',$options)) {
  42. $zone = $options['z'];
  43. } else {
  44. die ("Need to specify at least a zone to dump via -z");
  45. }
  46.  
  47. $db = new PDO("mysql:host=".$hostname.";dbname=".$database, $user, $password,
  48. array(PDO::ATTR_EMULATE_PREPARES => false, // newish version of MySQL
  49. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); // throw exceptions on error
  50.  
  51. date_default_timezone_set("America/Los_Angeles");
  52.  
  53. function getDomainId ($domain) {
  54. global $db;
  55.  
  56. $stmt = $db->prepare("SELECT id FROM domains WHERE name=?");
  57. $stmt->execute(array($domain));
  58. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  59.  
  60. if ($stmt->rowCount() <= 0) {
  61. return false;
  62. } else {
  63. return $row['id'];
  64. }
  65. }
  66.  
  67. if ($id = getDomainId($zone)) {
  68. $stmt = $db->prepare("SELECT name, TYPE, content, ttl, prio FROM records WHERE domain_id = ?");
  69. $stmt->execute(array($id));
  70. if ($stmt->rowCount() <= 0) {
  71. die ("No records found for that domain.");
  72. }
  73. $mxList = array();
  74. $srvList = array();
  75. $nsList = array();
  76. // usage: cli53 rrcreate [-h] [-x TTL] [-w WEIGHT] [-i IDENTIFIER]
  77. // [--region REGION] [-r] [--wait] [--dump]
  78. // zone rr {A,AAAA,CNAME,SOA,NS,MX,PTR,SPF,SRV,TXT,ALIAS}
  79. // values [values ...]
  80. echo "#!/bin/bash" . PHP_EOL;
  81. echo 'echo "Purging old records."' . PHP_EOL;
  82. echo "cli53 rrpurge --confirm " . $zone . PHP_EOL;
  83. while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  84. // add the period to the end of the record name
  85. $name = $row['name'];
  86. if ($name == "") {
  87. $name = "''";
  88. } else {
  89. $name .= ".";
  90. }
  91. $row['name'] = $name;
  92.  
  93. switch ($row['TYPE']) {
  94. case 'SOA':
  95. // no need to move the SOA, it already exists at amazon
  96. break;
  97. case 'MX':
  98. // save up all the MX records and make the Amazon combo MX records later
  99. $mxList[$name][] = $row;
  100. break;
  101. case 'SRV':
  102. // save up all the SRV records and make the Amazon combo SRV records later
  103. $srvList[$name][] = $row;
  104. break;
  105. case 'NS':
  106. if ($name != $zone) { // don't bother with our own NS records
  107. $nsList[$name][] = $row;
  108. }
  109. break;
  110. default:
  111. echo 'echo "creating ' . $row['TYPE'] . ' record for ' . $name . ' with content: ' . $row['content'] . '"' . PHP_EOL;
  112. echo "cli53 rrcreate -x " . $row['ttl'] . " " . $zone . " " . $name . " " . $row['TYPE'] . " " . $row['content'] . PHP_EOL;
  113. }
  114. }
  115. // Create MX entries
  116. foreach ($mxList as $mx) {
  117. $serverList = "";
  118. foreach ($mx as $row) {
  119. $serverList .= "'" . $row['prio'] . " " . $row['content'] . "' ";
  120. $name = $row['name'];
  121. $ttl = $row['ttl'];
  122. }
  123. echo 'echo "Creating MX Records."' . PHP_EOL;
  124. echo "cli53 rrcreate -x " . $ttl . " " . $zone . " " . $name . " MX " . $serverList . PHP_EOL;
  125. }
  126. foreach ($srvList as $srv) {
  127. $serverList = "";
  128. foreach ($srv as $row) {
  129. $serverList .= "'" . $row['content'] . "' ";
  130. $name = $row['name'];
  131. $ttl = $row['ttl'];
  132. }
  133. echo 'echo "Creating SRV Records."' . PHP_EOL;
  134. echo "cli53 rrcreate -x " . $ttl . " " . $zone . " " . $name . " SRV " . $serverList . PHP_EOL;
  135. }
  136. foreach ($nsList as $ns) {
  137. $serverList = "";
  138. foreach ($ns as $row) {
  139. $serverList .= "'" . $row['content'] . "' ";
  140. $name = $row['name'];
  141. $ttl = $row['ttl'];
  142. }
  143. echo 'echo "Creating NS Records."' . PHP_EOL;
  144. echo "cli53 rrcreate -x " . $ttl . " " . $zone . " " . $name . " NS " . $serverList . PHP_EOL;
  145. }
  146. } else {
  147. die ("domain not found!" . PHP_EOL);
  148. }
Add Comment
Please, Sign In to add comment