Skip to content

Instantly share code, notes, and snippets.

@wr0ngway
Created March 20, 2012 20:25
Show Gist options
  • Save wr0ngway/2140938 to your computer and use it in GitHub Desktop.
Save wr0ngway/2140938 to your computer and use it in GitHub Desktop.
script to backup all simpledb tables from a given aws account
#!/usr/bin/env ruby
# Add your gems here
#
gemfile_contents = <<-EOF
source "http://rubygems.org"
gem "fog"
gem "yajl-ruby"
EOF
require 'open-uri'
eval open('https://raw.github.com/gist/1770601/gist-bundler.rb').read, binding
# Your code goes here
#
# script to backup all sdb tables for a given aws account
# in the interests of memory conservation, it writes multiple json
# maps to each json file, rather than combining into one before writing
if ARGV.size != 2
puts "usage: #{File.basename $0} access_key access_secret"
exit 1
end
require 'fileutils'
access_key = ARGV[0]
access_secret = ARGV[1]
sdb = ::Fog::AWS::SimpleDB.new(:aws_access_key_id => access_key,
:aws_secret_access_key => access_secret,
:host => 'sdb.amazonaws.com')
def find_all(sdb, table, opts={})
query = "select *"
query << " from `#{table}`"
query << " limit " + (opts[:limit] ? opts[:limit].to_s : "200")
query_opts = {}
query_opts["NextToken"] = opts[:offset].to_s if opts[:offset]
response = sdb.select(query, query_opts)
data = response.body['Items']
return data, response.body['NextToken']
end
FileUtils.mkdir_p("sdb_backup")
domains = sdb.list_domains.body['Domains']
domains.each do |domain|
print "Dumping #{domain}"
encoder = Yajl::Encoder.new
open("sdb_backup/#{domain}.json", "w") do |f|
print '.'
data, next_token = find_all(sdb, domain)
encoder.encode(data, f)
while next_token
print '.'
data, next_token = find_all(sdb, domain, :offset => next_token)
encoder.encode(data, f)
end
print "\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment