Rake mysql tasks

I have found that I should be quicker if I put the following task in all my small projects.

require ‘find’
namespace :mysql do
desc “Perform mysql dump on the RAILS_ENV environment”
task :dump => :environment do
db_config = ActiveRecord::Base.configurations[RAILS_ENV]
backup_file = “#{db_config[‘database’]}.sql”
cmd = “mysqldump -u #{db_config[‘username’]} -p#{db_config[‘password’]} -Q –add-drop-table -O add-locks=FALSE -O lock-tables=FALSE #{db_config[‘database’]} -r #{backup_file}”
puts cmd
sh cmd
end

desc “imports the dumped database in the mysql FILE”
task :import => :environment do |t, args|
version = ENV[“FILE”] ? ENV[“FILE”].to_i : nil
raise “FILE is required” unless version

db_config = ActiveRecord::Base.configurations[RAILS_ENV]
backup_file = ENV[“FILE”]
sh “mysql -u #{db_config[‘username’]} -p#{db_config[‘password’]} -e \”CREATE DATABASE IF NOT EXISTS #{db_config[‘database’]}\” ”
sh “mysql -u #{db_config[‘username’]} -p#{db_config[‘password’]} #{db_config[‘database’]} < #{backup_file}”
end

desc “runs the mysql console for the selected RAILS_ENV”
task :console => :environment do
db_config = ActiveRecord::Base.configurations[RAILS_ENV]
backup_file = “#{db_config[‘database’]}.sql”
cmd = “mysql -u #{db_config[‘username’]} -p#{db_config[‘password’]} #{db_config[‘database’]}”
sh cmd
end

end

The task dumps just make a mysql dump from the current environment. Hmm. The next step is to transfer it with scp to my host.

Grab from here the mysql console task