LoginSignup
16
14

More than 5 years have passed since last update.

mysqldumpでダンプするrakeタスク

Last updated at Posted at 2014-12-26
# ./lib/tasks/db.rake

require 'active_record'

namespace :db do
  desc "Dumps the database to db/dbname.dump"
  task dump_all: [:environment,:load_config] do
    cmd = nil 
    environment = Rails.env
    configuration = ActiveRecord::Base.configurations[environment]
    cmd = "mysqldump -u #{configuration['username']} -p#{configuration['password']} #{configuration['database']} > db/#{configuration['database']}.dump"
    puts cmd
    exec cmd 
  end   
end

使い方

rake db:dump_all # developmentのdbをdumpする
RAILS_ENV=production rake db:dump_all # productionのdbをdumpする

参考

上記のwith_configがよく分からなかったので、database.ymlの内容の参照方法は、active_record内のdatabase.rakeを参考にしました。

16
14
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
16
14