{"id":191,"date":"2009-03-02T01:00:00","date_gmt":"2009-03-02T07:00:00","guid":{"rendered":"http:\/\/gregsowell.com\/?p=191"},"modified":"2009-03-02T08:47:50","modified_gmt":"2009-03-02T14:47:50","slug":"failover-dns-change-script","status":"publish","type":"post","link":"https:\/\/gregsowell.com\/?p=191","title":{"rendered":"Failover DNS Change Script"},"content":{"rendered":"<p>So, as promised, here is my DNS change script.  This is written in php for a BIND DNS server.  Right now it is very simplistic; it is made to change a single A record, but can be adapted to do multiples at once.<\/p>\n<p>This is designed to be used in conjunction with <a title=\"webmin\" href=\"http:\/\/gregsowell.com\/?p=209\" target=\"_blank\">Webmin<\/a>.  Setup a remote monitor in the &#8220;System and Server Status&#8221; section under &#8220;other&#8221;.<\/p>\n<p>Webmin will monitor your web server and ensure they are up and returning a webpage that matches a regex statement.  If it does go down it runs our php script with the command line parameter of &#8220;down&#8221;.  The script will then change the A record in the bind file to the secondary server&#8217;s IP and then restart the bind service.  Once the server comes back up, the script runs again with &#8220;up&#8221; on the command line.  The up command sets the a record back to the main server.  This is meant to be run on the DNS server itself.  This could be adapted to issue the rndc command to reload the single zone if you so wanted.<\/p>\n<p>You will want to set the TTL on the A record down to like 60 seconds.<\/p>\n<p>Click the link below for Webmin setup and the PHP script!<br \/>\n<!--more--><br \/>\nWebmin Setup:<\/p>\n<p>Add type Remote HTTP Service<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-195\" title=\"Webmin Sys Status\" src=\"http:\/\/gregsowell.com\/wp-content\/uploads\/2009\/02\/webmin1.jpg\" alt=\"Webmin Sys Status\" width=\"679\" height=\"80\" srcset=\"https:\/\/gregsowell.com\/wp-content\/uploads\/2009\/02\/webmin1.jpg 679w, https:\/\/gregsowell.com\/wp-content\/uploads\/2009\/02\/webmin1-300x35.jpg 300w\" sizes=\"auto, (max-width: 679px) 100vw, 679px\" \/><\/p>\n<p>Then configure as below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-196\" title=\"Bind Script Webmin Config\" src=\"http:\/\/gregsowell.com\/wp-content\/uploads\/2009\/02\/webmin2.jpg\" alt=\"Bind Script Webmin Config\" width=\"964\" height=\"544\" srcset=\"https:\/\/gregsowell.com\/wp-content\/uploads\/2009\/02\/webmin2.jpg 964w, https:\/\/gregsowell.com\/wp-content\/uploads\/2009\/02\/webmin2-300x169.jpg 300w\" sizes=\"auto, (max-width: 964px) 100vw, 964px\" \/><\/p>\n<p>PHP Script.  I&#8217;m just putting it in the \/root folder.  Make sure to set it to executable.<br \/>\n<code lang=\"php\"><br \/>\n<?php\n\/\/ \n\/\/servers\n$Main = \"1.1.1.1\";\n$Backup = \"2.2.2.2\";\n\/\/path to named folder\n$Path = '\/var\/named\/chroot\/var\/named\/';\n\/\/domains to check through for the IP replacement\n$Domains = 'whatever1.com';\n\/\/A record to look for\n$Arecord = 'www.whatever1.com';\n\n\/\/check for a command line parameter\nif ($argc >= 2) {<br \/>\n\tif ($argv[1] == 'up') {<br \/>\n\t\t\/\/set the A record back to main<br \/>\n\t\techo \"up \\n\";<br \/>\n\t\t$FindIT = $Backup;<br \/>\n\t\t$SetIT = $Main;<br \/>\n\t} elseif ($argv[1] == 'down') {<br \/>\n\t\t\/\/set the A record to backup<br \/>\n\t\techo \"down \\n\";<br \/>\n\t\t$FindIT = $Main;<br \/>\n\t\t$SetIT = $Backup;<br \/>\n\t}<\/p>\n<p>\t$lineNum = 0;<br \/>\n\techo $Path . $Domains . '.hosts' . \"\\n\";<br \/>\n\t\/\/open the domain file<br \/>\n\t$lines = file($Path . $Domains . '.hosts');<br \/>\n\t\/\/open the file for editing<br \/>\n\t$fp=fopen($Path . $Domains . '.hosts',\"w+\");<br \/>\n\tforeach ($lines as $line_num => $line) {<br \/>\n\t\t$lineNum = $lineNum + 1;<br \/>\n\t\t\/\/increment sequence #<br \/>\n\t\tif ($lineNum == 3) {<br \/>\n\t\t\t$line = $line + 1;<br \/>\n\t\t\t$line = \"\t\t\t\" . $line . \"\\n\";<br \/>\n\t\t}<br \/>\n\t\t$pos = 0;<br \/>\n\t\t\/\/loop through each line checking if it matches $FindIT exactly with the<br \/>\n\t\t\/\/last values at the end of the line.  If found, change it to $SetIT<br \/>\n\t\t$pos = strpos('test' . $line, $FindIT);<br \/>\n\t\tif ($pos > 0) {<br \/>\n\t\t\t\/\/we found the line<br \/>\n\t\t\t$line = str_replace($FindIT, $SetIT, $line);<br \/>\n\t\t}<br \/>\n\t\t\/\/write each line as we loop through<br \/>\n\t\tfwrite($fp,$line);<br \/>\n\t}<\/p>\n<p>\t\/\/restart bind service<br \/>\n\texec('\/etc\/init.d\/named restart');<br \/>\n}<br \/>\n?>\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So, as promised, here is my DNS change script. This is written in php for a BIND DNS server. Right now it is very simplistic; it is made to change a single A record, but can be adapted to do multiples at once. This is designed to be used in conjunction with Webmin. Setup a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,7],"tags":[],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-linux","category-programmingscripting"],"_links":{"self":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=191"}],"version-history":[{"count":23,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":312,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/191\/revisions\/312"}],"wp:attachment":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}