Skip to content

Instantly share code, notes, and snippets.

@oki
Forked from subimage/sync.rb
Last active December 12, 2015 12:19
Show Gist options
  • Select an option

  • Save oki/4770920 to your computer and use it in GitHub Desktop.

Select an option

Save oki/4770920 to your computer and use it in GitHub Desktop.

Revisions

  1. oki revised this gist Feb 12, 2013. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions sync.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    namespace :sync do

    namespace :db do
    desc "Load production data into development database"
    task :database, :roles => :db, :only => { :primary => true } do
    task :sync, :roles => :db, :only => { :primary => true } do
    require 'yaml'

    # Gets db yml from server, because we don't store it on dev boxes!
    @@ -13,6 +12,9 @@
    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 }

    prod_config['production']['host'] ||= '127.0.0.1'

    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
    @@ -25,4 +27,4 @@
    `rm -f tmp/#{filename}`
    `rm -f tmp/prod_database.yml`
    end
    end
    end
  2. @subimage subimage created this gist Nov 2, 2012.
    28 changes: 28 additions & 0 deletions sync.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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