четверг, 8 декабря 2011 г.

UPDATED: Загрузка mysql из production mysql в development

слегка модифицирован http://www.snippetstash.com/public/9


# Fetches the production database on the server contents into the development
 # database
 #
 # Assumes you have dbuser, dbhost, dbpassword, and application defined somewhere in your
 # task. Modify as needed - database.yml is used for importing data, just not for exporting.
 #
 # Only supports MySQL.
 desc "Load production data into development database"
 task :fetch_remote_db, :roles => :db, :only => { :primary => true } do
   require 'yaml'

   database = YAML::load_file('config/database.yml')

   filename = "dump.#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.sql"

   on_rollback do
     delete "/tmp/#{filename}"
     delete "/tmp/#{filename}.gz"
   end

   cmd = "mysqldump -u #{database['production']['username']} --password=#{database['production']['password']} #{database['production']['database']} > /tmp/#{filename}"
   puts "Dumping remote database"
   run(cmd) do |channel, stream, data|
     puts data
   end

   # compress the file on the server
   puts "Compressing remote data"
   run "gzip -9 /tmp/#{filename}"
   puts "Fetching remote data"
   get "/tmp/#{filename}.gz", "dump.sql.gz"

   # build the import command
   # no --password= needed if password is nil.
   if database['development']['password'].nil?
     cmd = "/usr/local/mysql/bin/mysql -u #{database['development']['username']} #{database['development']['database']} < dump.sql"
   else
     cmd = "/usr/local/mysql/bin/mysql -u #{database['development']['username']} --password=#{database['development']['password']} #{database['development']['database']} < dump.sql"
   end

   # unzip the file. Can't use exec() for some reason so backticks will do
   puts "Uncompressing dump"
   `gzip -d dump.sql.gz`
   puts "Executing : #{cmd}"
   `#{cmd}`
   puts "Cleaning up"
   `rm -f dump.sql`

   puts "Be sure to run rake db:migrate to ensure your database schema is up to date!"
 end


UPDATED: еще один старый recipe для capistrano


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

  database = YAML::load_file('config/database.yml')

  filename = "dump.#{Time.now.strftime '%Y-%m-%d_%H:%M:%S'}.sql"
  on_rollback { delete "/tmp/#{filename}" }

  run "mysqldump -u #{database['production']['username']} --password=#{database['production']['password']} #{database['production']['database']} > /tmp/#{filename}" do |channel, stream, data|
    puts data
  end
  get "/tmp/#{filename}", filename
#  exec "/tmp/#{filename}"
  exec "mysql -u #{database['development']['username']} --password=#{database['development']['password']} #{database['development']['database']} < #{filename}; rm -f #{filename}"
end

Комментариев нет:

Отправить комментарий