Skip to content

Instantly share code, notes, and snippets.

@cleicar
Last active June 6, 2017 14:29
Show Gist options
  • Save cleicar/283e24032c4d02094571260864b4b0dc to your computer and use it in GitHub Desktop.
Save cleicar/283e24032c4d02094571260864b4b0dc to your computer and use it in GitHub Desktop.
Exportar e Restaurar databases via mongoidShell
namespace :backup do
[:development, :production].each do |env|
namespace env do
desc 'Faz o dump de uma base informada como parametro'
task :dump_database_collections => :environment do
Mongoid.load! File.join(Rails.root, "config/mongoid.yml"), env
database_name = ENV['db']
dump_dir = File.join(ENV['output'], database_name)
puts "Exportando database #{database_name} no diretório #{dump_dir}"
mongodump = Mongoid::Shell::Commands::Mongodump.new(db: database_name, out: dump_dir)
system mongodump.to_s
puts "Finalizado com sucesso!"
end
desc 'Restaura a base de dados informada como parametro'
task :restore_database_collections => :environment do
Mongoid.load! File.join(Rails.root, "config/mongoid.yml"), env
database_name = ENV['db']
restore_dir = ENV['path']
puts "Restaurando database: #{database_name}"
begin
session = Moped::Session.new(["localhost:27017"])
session.use database_name
mongorestore = Mongoid::Shell::Commands::Mongorestore.new({session: session, restore: restore_dir })
system mongorestore.to_s
ensure
session.disconnect
end
puts "Finalizado com sucesso!"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment