0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsで本番サバーからDemoサバーにデータをコピーする

Posted at

Railsで本番サバーからDemoサバーにデータをコピーする (rake task)

今Railsプロジェクトを開発している、その時は本番サバーはドンドン新しいデータが登録している。そして同じデータをテストしたい時難しいと思います。したのコート使ってTerminalからデータをコピーができる。

# lib/tasks/db_pull.rake
namespace :db do
  desc 'Pull production db to development'
  task :pull => [:dump, :restore]

  task :dump do
    dumpfile = "#{Rails.root}/tmp/latest.dump"
    production = Rails.application.config.database_configuration['production']
    puts 'mysqldump on production database...'
    system "ssh user@server.tld 'mysqldump -u #{production['username']} --password=#{production['password']} -h #{production['host']} --add-drop-table --skip-lock-tables --verbose #{production['database']}' > #{dumpfile}"
    puts 'Done!'
  end

  task :restore do
    dev = Rails.application.config.database_configuration['development']
    abort 'Live db is not mysql' unless dev['adapter'] =~ /mysql/
    abort 'Missing live db config' if dev.blank?
    dumpfile = "#{Rails.root}/tmp/latest.dump"
    puts 'importing production database to development database...'
    system "mysql -h #{dev['host']} -u root #{dev['database']} < #{dumpfile}"
    puts 'Done!'
  end
end
rake db:pull

Reference: https://martinschurig.com/posts/2015/02/pulling-production-database-to-local-machine-rails-task/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?