Last active
February 22, 2017 20:19
-
-
Save davidchua/4242942 to your computer and use it in GitHub Desktop.
quick and dirty ruby script to copy dns from zerigo to amazon aws route 53 (route53). allows users to migrate ALL zones from zerigo over to Route53
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
#!/usr/local/rvm/rubies/ruby-1.9.2-p180/bin/ruby | |
require 'rubygems' | |
require 'zerigo_dns' | |
require 'route53' | |
Zerigo::DNS::Base.user = '[email protected]' | |
Zerigo::DNS::Base.api_key = 'yourkeyhere' | |
# iterate through all domain names | |
domains = Zerigo::DNS::Zone.find(:all) | |
domains.each do |z| | |
puts "#{z.domain} (id: #{z.id})" | |
hosts = Zerigo::DNS::Host.find(:all, :params=>{ :zone_id => z.id, :per_page=>100, :page=>1 }) | |
host_map = {} | |
hosts.each do |host| | |
host.hostname = '' if host.hostname.nil? | |
host_map[host.hostname] = {} unless host_map[host.hostname].present? | |
host_map[host.hostname][host.host_type] = [] unless host_map[host.hostname][host.host_type].present? | |
host_map[host.hostname][host.host_type] << { :fqdn => host.fqdn, :priority => host.priority, :hostname => host.hostname, :data => host.data } | |
end | |
puts "Connecting to Route 53" | |
aws = Route53::Connection.new('access_key_id', 'secret_key') | |
puts "Creating zone on Route 53" | |
new_zone = Route53::Zone.new("#{z.domain}.", nil, aws) | |
response = new_zone.create_zone | |
exit 1 if response.error? | |
while response.pending? | |
sleep 1 | |
end | |
puts "Zone created on Route 53" | |
host_map.each do |hostname, records| | |
puts " Hostname: #{hostname.blank? ? 'domain apex' : hostname}" | |
records.each do |record_type, values| | |
puts " Record type: #{record_type}" | |
case record_type | |
when "A", "CNAME" | |
values.each do |record| | |
new_record = Route53::DNSRecord.new("#{ record[:fqdn] }.", record_type, "600", [ "#{ record[:data] }#{ '.' if record_type != 'A' }" ], new_zone) | |
resp = new_record.create | |
end | |
when "MX" | |
fqdn = values.first[:fqdn] | |
new_mx_record = Route53::DNSRecord.new("#{fqdn}.", "MX", "600", values.collect{ |record| "#{ record[:priority] } #{ record[:data] }." }, new_zone) | |
resp = new_mx_record.create | |
when "TXT", "SPF" | |
fqdn = values.first[:fqdn] | |
new_txt_record = Route53::DNSRecord.new("#{fqdn}.", record_type, "600", values.collect{ |record| "\"#{ record[:data] }\"" }, new_zone) | |
resp = new_txt_record.create | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment