Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
namespace :db do
  desc "Load production data into development database"
  task :load_production_data, :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"
    server_dump_file = "#{current_path}/tmp/#{filename}"
    on_rollback { delete server_dump_file }
    run "mysqldump -u #{prod_config['production']['username']} --password=#{prod_config['production']['password']} #{prod_config['production']['database']} > #{server_dump_file}" do |channel, stream, data|
      puts data
    end

    # Compress file for quicker transfer
    run "gzip #{server_dump_file}"
    get "#{server_dump_file}.gz", "tmp/#{filename}.gz"

    puts "Uncompressing local db dump file"
    `gunzip tmp/#{filename}.gz`
    puts "Loading locally..."
    `mysql -u #{local_config['development']['username']} --password=#{local_config['development']['password']} #{local_config['development']['database']} < tmp/#{filename}`
    puts "Cleaning up temp files"
    `rm -f tmp/#{filename}`
    `rm -f tmp/prod_database.yml`
  end
end