A Rake Task to Grab Data From Your Production Site
Posted by Craig Ambrose on April 17, 2007 at 10:17 PM
When you’re fixing bugs that occur in a production rails application, the first step is often to reproduce them locally in development mode. Every now and then, I find that I want to make my local development site run with the same data as my live production site, so here’s a little rake task to make it easier.
desc "Make local develoment site look like the live site"
task :sync do
host = 'craigambrose.com'
path = '/var/www/apps/craigambrose/current' db_config = YAML.load_file('config/database.yml') system "ssh #{host} \"mysqldump -u #{db_config['production']["username"]} -p#{db_config['production']["password"] } -Q —add-drop-table -O add-locks=FALSE -O lock-tables=FALSE —ignore-table=#{db_config['production']["database"]}.sessions #{db_config['production']["database"]} > ~/dump.sql\""
system "rsync -az —progress #{host}:~/dump.sql ./db/production_data.sql"
system "mysql -u #{db_config['development']["username"]} -p#{db_config['development']["password"]} #{db_config['development']["database"]} < ./db/production_data.sql"
system "rsync -az —progress #{host}:#{path}/public/system/ ./public/system"
end
Installation
Rake tasks can go in any file ending in .rake inside your lib/tasks directory (within a rails application). You might put this in a file specific to your application, like craigambrose.rake. You can also namespace rake tasks by putting them inside a blog like:
namespace :craigambrose do
# your tasks go here
end
With my task, you’ll need to set the value of two local variables, host and path. Or, perhaps you can think of a way to read those from your deploy.rb?
Usage
If you’ve namespaced it like I have, then it’ll be something like:
rake craigambrose:sync
Note that this uses a lot of shell commands, and basically assumes that you are developing on a unix box of some sort. You can delete the rsync for /public/system if you don’t store any files there.
