Created
August 3, 2021 17:46
-
-
Save SeanSith/8e91ea3c9750ea13e61078321a0f7885 to your computer and use it in GitHub Desktop.
Backup AWS SimpleDB
This file contains hidden or 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/bin/env ruby | |
require 'bundler/inline' | |
gemfile do | |
source "https://rubygems.org" | |
gem "fog-aws" | |
gem "yajl-ruby" | |
end | |
# Forked from https://gist.github.com/wr0ngway/2140938 | |
# 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 'yajl' | |
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