require File.expand_path(__FILE__ + '/../ey-flex') require 'logger' require 'forwardable' require 'stringio' require 'open4' require 'zlib' require 'ostruct' require 'fileutils' require 'fog' module Fog module AWS class Storage < ::Fog::Service module Utils def region_to_endpoint(region_name) case region_name when 'us-east-1' 's3.amazonaws.com' when 'us-west-2' 's3-us-west-2.amazonaws.com' when 'us-west-1' 's3-us-west-1.amazonaws.com' when 'eu-west-1' 's3-eu-west-1.amazonaws.com' when 'ap-southeast-1' 's3-ap-southeast-1.amazonaws.com' when 'ap-northeast-1' 's3-ap-northeast-1.amazonaws.com' when 'sa-east-1' 's3-sa-east-1.amazonaws.com' else raise ArgumentError, "Unknown region: #{region_name.inspect}" end end end class Mock alias_method :original_initialize, :initialize def initialize(options={}) options[:region] ||= 'us-east-1' options[:host] ||= region_to_endpoint(options[:region]) original_initialize(options) end end class Real alias_method :original_initialize, :initialize def initialize(options={}) options[:region] ||= 'us-east-1' options[:host] ||= region_to_endpoint(options[:region]) original_initialize(options) end end end end end module EY module Backup class << self attr_accessor :logger attr_accessor :tmp_dir end def self.run(argv = ARGV) options = CLI.run(argv) Runner.new(options).run end class Runner def initialize(options) @options = options end def run setup dispatch end # COMMANDS def new_backup Dumper.run(databases, @options[:split_size]) end def list BackupSet.list(databases) end def restore databases.each do |database| Loader.run(database, @options[:index]) end end def download databases.each do |database| Loader.download(database, @options[:index]) end end # END COMMANDS def databases @databases ||= database_names.map do |name| Database.new(@engine, EY::Backup.tmp_dir, @options[:keep], @backend, @options[:environment], name) end end def database_names if @options[:db].nil? || @options[:db].empty? @options[:databases] else [@options[:db]] end end def setup setup_logger setup_tmp_dir setup_backend setup_engine end def setup_logger if @options[:quiet] EY::Backup.logger ||= Logger.quiet else EY::Backup.logger ||= Logger.new end end def setup_tmp_dir if @options[:tmp_dir] EY::Backup.tmp_dir = @options[:tmp_dir] else EY::Backup.tmp_dir = "/mnt/tmp" end end def setup_backend @backend = Backend.new(@options[:aws_secret_id], @options[:aws_secret_key], @options[:region], @options[:backup_bucket]) end def setup_engine engine_class = Engine.lookup(@options[:engine]) if ! @options.key?(:dbhost) or @options[:dbhost] == nil or @options[:dbhost] == "" @options[:dbhost] = 'localhost' end @engine = engine_class.new(@options[:dbuser], @options[:dbpass], @options[:dbhost], @options[:key_id]) end def dispatch send(@options[:command]) end end end end lib_dir = File.expand_path(__FILE__ + '/../ey_backup') require lib_dir + '/spawner' require lib_dir + '/logger' require lib_dir + '/base' require lib_dir + '/backup_set' require lib_dir + '/cli' require lib_dir + '/database' require lib_dir + '/dumper' require lib_dir + '/engine' require lib_dir + '/loader' require lib_dir + '/backend' require lib_dir + '/engines/mysql_engine' require lib_dir + '/engines/postgresql_engine' require lib_dir + '/processors/gzipper' require lib_dir + '/processors/splitter' require lib_dir + '/processors/gpg_encryptor'