Ruby & Rails

Hendrik Mans - March 19, 2013

rake db:pull

I have a custom Rake task called db:pull that copies’s production database to my local development machine. Here’s what it looks like:

 1# lib/tasks/db.rake
 2namespace :db do
 3  desc "Pull production database into the local development environment."
 4  task :pull do
 5    puts cmd = "ssh sloblog@slobdb mysqldump -v sloblog_production | mysql sloblog_development"
 6    system cmd
 7    puts "Done."
 8  end

As you can see, I simply ssh to my database machine, pull the contents of my production database through mysqldump, and pipe it all into my local development database.

First and foremost, I use this to quickly get some real data into my development database to test new code against. I’m a big fan of factories (my primary weapon of choice for this being FactoryGirl), and I usually set up my db/seeds.rb so it will generate production-like data, but all the Lorem Ipsum in the world is no good compared to real data created by real users.

But there’s a couple of additional uses. For example, just before I deploy a new feature that involves database migrations, I run rake db:pull followed by rake db:migrate to test my migrations against current production data.

Last but not least, this is a nice, pain-free way of quickly pulling a backup of the production database. Of course, I’m doing daily backups of the production database anyway, but sometimes I need to fiddle around in the production database, and quickly running rake db:pull makes sure I have a very recent copy in case I mess up.


Developer dude from Hamburg, Germany. Among other things, runs Not good with descriptions. Don’t follow me on Twitter. ★

Loading comments...

Please sign in to post a comment.