Skip to content

Instantly share code, notes, and snippets.

@yanyingwang
Last active February 2, 2016 03:09
Show Gist options
  • Save yanyingwang/e203b2b9c01f11f84141 to your computer and use it in GitHub Desktop.
Save yanyingwang/e203b2b9c01f11f84141 to your computer and use it in GitHub Desktop.
a simple rake task to restore development mysql db data with production's
# lib/tasks/db_pull.rake
# set below first on mysql server:
# mysql_config_editor set --login-path=local --host=localhost --user=root --password
#
#
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['pro']
ssh_user = "ubuntu"
ssh_host = production['host']
puts 'mysqldump on production database...'
system "ssh #{ssh_user}@#{ssh_host} 'mysqldump --login-path=local -i -c -q --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 #{dev['username']} --password=#{dev['password']} #{dev['database']} < #{dumpfile}"
puts 'Done!'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment