Skip to content

Instantly share code, notes, and snippets.

@sbounmy
Created March 23, 2017 15:35
Show Gist options
  • Save sbounmy/e81cdebcf3e840ebdf61fb8e8a5e126b to your computer and use it in GitHub Desktop.
Save sbounmy/e81cdebcf3e840ebdf61fb8e8a5e126b to your computer and use it in GitHub Desktop.
Rake task to import production database from heroku mongodb
namespace :db do
desc "Import from production env"
task import: :environment do
raise 'this should be run in dev mode !!' unless Rails.env == 'development'
uri = Mongo::URI.new(`heroku config:get MONGODB_URI`.chomp)
tmp = Dir::Tmpname.make_tmpname('/tmp/', 'hh-dump')
puts "Dumping #{uri.servers[0]} to #{tmp}..."
`mongodump -h #{uri.servers[0]} -d #{uri.database} -u #{uri.credentials[:user]} -p #{uri.credentials[:password]} -o #{tmp}`
dev = Mongoid.clients['default']
`mongorestore --host #{dev['hosts'][0]} --drop -d #{dev['database']} #{tmp}/#{uri.database}`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment