Skip to content

Instantly share code, notes, and snippets.

@subimage
Created November 2, 2012 14:17
Show Gist options
  • Save subimage/4001612 to your computer and use it in GitHub Desktop.
Save subimage/4001612 to your computer and use it in GitHub Desktop.
Capistrano recipe to sync database with production machine
namespace :sync do
desc "Load production data into development database"
task :database, :roles => :db, :only => { :primary => true } do
require 'yaml'
# Gets db yml from server, because we don't store it on dev boxes!
get "#{current_path}/config/database.yml", "tmp/prod_database.yml"
prod_config = YAML::load_file('tmp/prod_database.yml')
local_config = YAML::load_file('config/database.yml')
# Dump server sql
filename = "dump.#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.sql.gz"
server_dump_file = "#{current_path}/tmp/#{filename}"
on_rollback { delete server_dump_file }
run "mysqldump -h #{prod_config['production']['host']} -u #{prod_config['production']['username']} --password=#{prod_config['production']['password']} #{prod_config['production']['database']} | gzip > #{server_dump_file}" do |channel, stream, data|
puts data
end
get "#{server_dump_file}", "tmp/#{filename}"
puts "Uncompressing & loading locally..."
`gunzip < tmp/#{filename} | mysql -u #{local_config['development']['username']} --password=#{local_config['development']['password']} #{local_config['development']['database']}`
puts "Cleaning up temp files"
`rm -f tmp/#{filename}`
`rm -f tmp/prod_database.yml`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment