Created
April 9, 2014 08:27
-
-
Save suruja/10241312 to your computer and use it in GitHub Desktop.
Mongo dump and restore on Heroku
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'uri' | |
namespace :mongo do | |
desc "Dump the production database and restore to development and staging databases." | |
task :dump_and_restore => :environment do | |
dev = URI.parse(ENV["MONGO_URI"]) | |
prod = URI.parse(`heroku config:get MONGO_URI --remote production`) | |
prod_username, prod_password = prod.userinfo.split(':') | |
stag = URI.parse(`heroku config:get MONGO_URI --remote staging`) | |
stag_username, stag_password = prod.userinfo.split(':') | |
`mkdir -p db/backups` | |
out = `mongo #{prod.host}:#{prod.port}/#{prod_username} -u #{prod_username} -p #{prod_password} --eval 'printjson(db.getCollectionNames())'` | |
collections = out.scan(/\".+\"/).map { |s| s.gsub('"', '') }.reject{|c| c.match(/^system\./)} | |
collections.each do |c| | |
`mongodump --host #{prod.host}:#{prod.port} -d #{prod_username} -u #{prod_username} -p #{prod_password} -c #{c} -o db/backups/` | |
end | |
`mongorestore -h #{dev.host}:#{dev.port} --drop -d #{dev.path.gsub!(/^\//, '')} db/backups/#{prod_username}` | |
`mongorestore -h #{stag.host}:#{stag.port} --drop -d #{stag_username} db/backups/#{prod_username}` | |
`rm -r db/backups` | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment