├── .gitignore ├── Gemfile ├── LICENSE ├── README.md └── cloudflare-backup.rb /.gitignore: -------------------------------------------------------------------------------- 1 | Gemfile.lock 2 | backup 3 | .bundle 4 | vendor 5 | id_rsa 6 | id_rsa.pub 7 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | gem 'cloudflare', '>=2.0' 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Dave Vasilevsky 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | CloudFlare DNS backup tool 2 | ========================== 3 | 4 | This tool allows you to backup your [CloudFlare DNS](https://www.cloudflare.com/dns) settings. It will back them up locally, and optionally push any changes to a git repository. 5 | 6 | Install the dependencies with [bundler](http://bundler.io/): ```bundle install```. 7 | 8 | Then to use it, you'll need the email address and API token from your CloudFlare account: 9 | ```bundle exec ./cloudflare-backup.rb EMAIL TOKEN OUTPUT_DIRECTORY``` 10 | 11 | You can run this from a cron job to keep up-to-date. 12 | 13 | If you want to track your changes, make the OUTPUT_DIRECTORY a git repository. Set up a git remote where changes should be pushed, and create an ```id_rsa``` in this directory so that the backup tool has access to your remote. 14 | -------------------------------------------------------------------------------- /cloudflare-backup.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/ruby 2 | require 'cloudflare' 3 | 4 | require 'json' 5 | require 'pathname' 6 | require 'set' 7 | require 'open3' 8 | require 'tempfile' 9 | 10 | # Get all records, even if > 180 in number 11 | def records(cf, zone) 12 | records = [] 13 | loop do 14 | resp = cf.rec_load_all(zone, records.size) 15 | recset = resp['response']['recs'] 16 | records.concat(recset['objs']) 17 | break unless recset['has_more'] 18 | end 19 | records 20 | end 21 | 22 | def write_json(dir, filename, obj) 23 | path = dir.join(filename + '.json') 24 | IO.write(path.to_s, JSON.pretty_generate(obj)) 25 | return path 26 | end 27 | 28 | def backup(cf, dir, key) 29 | # Fetch everything 30 | zones = cf.zone_load_multi 31 | recs = {} 32 | zones['response']['zones']['objs'].each do |zone| 33 | name = zone['zone_name'] 34 | recs[name] = records(cf, name) 35 | end 36 | 37 | # Put it where it belongs 38 | dir = Pathname.new(dir) 39 | dir.mkpath 40 | written = Set.new 41 | written << write_json(dir, 'zones', zones) 42 | recs.each { |z, rs| written << write_json(dir, z + '.zone', rs) } 43 | 44 | # Remove old files 45 | dir.find do |file| 46 | next if dir == file 47 | if file.basename.to_s == '.git' 48 | Find.prune 49 | next 50 | end 51 | file.unlink unless written.include? file 52 | end 53 | 54 | # Auto-commit 55 | auto_commit(dir, key) if dir.join('.git').exist? 56 | end 57 | 58 | def auto_commit(dir, key) 59 | # Setup GIT_SSH appropriately 60 | old_ssh = ENV['GIT_SSH'] 61 | ssh_wrapper = Tempfile.new('git-ssh', :mode => 0700) 62 | ssh_wrapper.chmod(0700) 63 | ssh_wrapper.puts(<<-EOF) 64 | #!/bin/sh 65 | exec ssh -i #{key.realpath.to_s} "$@" 66 | EOF 67 | ssh_wrapper.close 68 | ENV['GIT_SSH'] = ssh_wrapper.path 69 | 70 | begin 71 | Dir.chdir(dir.to_s) do 72 | # Add new/changed files 73 | added = IO.popen(%w[git ls-files --exclude-standard -o -m -z]) \ 74 | .each_line("\0").map { |n| n.chomp("\0") } 75 | system('git', 'add', *added) unless added.empty? 76 | 77 | unless system(*%w[git diff --cached --quiet]) 78 | system(*%w[git commit --quiet -am], "Autocommit at " + Time.now.to_s) 79 | system(*%w[git push --quiet]) 80 | end 81 | end 82 | ensure 83 | ENV['GIT_SSH'] = old_ssh 84 | ssh_wrapper.unlink 85 | end 86 | end 87 | 88 | email, token, dir = ARGV 89 | cf = CloudFlare::connection(token, email) 90 | key = Pathname.new(__FILE__).parent.join('id_rsa') 91 | backup(cf, dir, key) 92 | --------------------------------------------------------------------------------