ruby 用于从给定的aws帐户备份所有simpledb表的脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 用于从给定的aws帐户备份所有simpledb表的脚本相关的知识,希望对你有一定的参考价值。

#!/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

以上是关于ruby 用于从给定的aws帐户备份所有simpledb表的脚本的主要内容,如果未能解决你的问题,请参考以下文章

ruby 用于删除所有云文件和容器的Ruby脚本 - 也就是关闭帐户。需要宝石'雾'和'哑剧类型'

AWS S3权限 - put-bucket-acl出错

从 AWS 组织导出所有 IAM 用户、角色和策略

按特定时间表重新填充帐户的实用程序

将dynamoDB表复制到另一个没有S3的aws帐户

Flask Mail 不适用于 Gmail 帐户和 AWS 实例