Sha256: af2f9f093bf14597d756e3ea4dfb00312861575353851467d31db220e2c9c05c

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

module Cloner::MongoDB
  extend ActiveSupport::Concern

  def mongodb_conf
    @conf ||= begin
      YAML.load_file(Rails.root.join('config', 'mongoid.yml'))[Rails.env]['sessions']['default']
    end
  end

  def mongodb_to
    mongodb_conf['database']
  end

  def mongodb_r_conf
    @r_conf ||= begin
      Net::SSH.start(ssh_host, ssh_user, ssh_opts) do |ssh|
        ret = ssh_exec!(ssh, "cat #{remote_app_path}/config/mongoid.yml")
        check_ssh_err(ret)
        YAML.load(ret[0])[env_from]['sessions']['default']
      end
    end
  end

  def mongodb_local_auth
    if mongodb_conf['password'].nil?
      ""
    else
      "-u #{mongodb_conf['username']} -p #{mongodb_conf['password']}"
    end
  end

  def mongodb_dump_remote
    puts "backup remote DB via ssh"
    Net::SSH.start(ssh_host, ssh_user, ssh_opts) do |ssh|
      ssh.exec!("rm -R #{remote_dump_path}")
      ret = ssh_exec!(ssh, "mkdir -p #{remote_dump_path}")
      check_ssh_err(ret)
      dump = "mongodump -u #{mongodb_r_conf['username']} -p #{mongodb_r_conf['password']} -d #{mongodb_r_conf['database']} --authenticationDatabase #{mongodb_r_conf['database']} -o #{remote_dump_path}"
      puts dump if verbose?
      ret = ssh_exec!(ssh, dump)
      check_ssh_err(ret)
    end
  end

  def mongodb_dump_restore
    puts "restoring DB"
    restore = "mongorestore --drop -d #{mongodb_to} #{mongodb_local_auth} #{mongodb_path}"
    puts restore if verbose?
    pipe = IO.popen(restore)
    while (line = pipe.gets)
      print line if verbose?
    end
    ret = $?.to_i
    if ret != 0 
      puts "Error: local command exited with #{ret}"
    end
  end

  def mongodb_path
    Rails.root.join("tmp", "dump", mongodb_to).to_s
  end

  def mongodb_dump_copy
    FileUtils.mkdir_p(mongodb_path)
    rsync("#{remote_dump_path}/#{mongodb_r_conf['database']}", mongodb_path)
  end

  def clone_mongodb
    mongodb_dump_remote()
    mongodb_dump_copy()
    mongodb_dump_restore()
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cloner-0.3.1 lib/cloner/mongodb.rb
cloner-0.3.0 lib/cloner/mongodb.rb