Last active
August 29, 2015 14:05
-
-
Save davingee/2fa7be904d5182177adb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :db do | |
desc "Import production data to ENV['to'] database" | |
namespace :import do | |
desc "Import production db to whatever 'ENV['to']' db is set to and s3 sync" | |
task :production => :environment do | |
## example rake db:import:production to=staging s3=true | |
## or no s3 | |
## example rake db:import:production to=staging | |
## or default to developtment with no s3 | |
## rake db:import:production | |
## this will import production databse to the development and sync s3 bucket | |
## will have to http://coolestguidesontheplanet.com/connect-amazon-s3-bucket-command-line-os-x/ to use the s3 sync | |
## variables to set ## | |
ENV["to"] ||= "development" | |
s3_sync_hash = { | |
development: "dev.yourname.com", | |
qa: "qa.yourname.com", | |
staging: "staging.yourname.com", | |
production: "prod.yourname.com", | |
}.with_indifferent_access | |
allowed_dbs_to_update = %w[development qa staging] # only these can be synced from production | |
## variables to set ## | |
raise_statement = "please set to= to one of the following #{allowed_dbs_to_update} or don't include it to sync your development DB" | |
raise ArgumentError.new(raise_statement) unless allowed_dbs_to_update.include?(ENV["to"]) | |
# assign production and ENV["to"] config variables from database.yml file | |
yaml_file = YAML::load(File.open("#{Rails.root}/config/database.yml")) | |
production_config = yaml_file[ "production" ].with_indifferent_access | |
to_config = yaml_file[ ENV['to'] ].with_indifferent_access | |
# dump production db to dev_data/#{to_config[:database]}.sql | |
system("mysqldump -u #{production_config[:username]} --password=#{production_config[:password]} -h #{production_config[:host]} #{production_config[:database]} --single-transaction > tmp/#{to_config[:database]}.sql") | |
puts "Wrote #{production_config[:database]} db to tmp/#{to_config[:database]}.sql" | |
client = Mysql2::Client.new(:host => to_config[:host], :username => to_config[:username], :password => to_config[:password]) | |
client.query("CREATE DATABASE #{to_config[:database]}") rescue nil | |
client.close | |
puts "created #{to_config[:database]} if it didn't exist" | |
# inject dropped sql into #{to_config[:database]} | |
password = to_config[:password].blank? ? "" : "-p#{to_config[:password]}" | |
system("mysql -u#{to_config[:username]} #{password} -h #{to_config[:host]} #{to_config[:database]} < tmp/#{to_config[:database]}.sql") | |
puts "Injected tmp/#{to_config[:database]}.sql into #{to_config[:database]}" | |
# remove #{to_config[:database]}.sql | |
system("rm tmp/#{to_config[:database]}.sql") | |
puts "removed tmp/#{to_config[:database]}.sql" | |
# sync production s3 to ENV['to'] if ENV["s3"] == "true" | |
if ENV["s3"] == "true" | |
system("s3cmd sync --delete-removed --acl-public s3://#{s3_sync_hash["production"]} s3://#{s3_sync_hash[ENV["to"]]} ") | |
puts "synced s3 bucket #{s3_sync_hash["production"]} with #{s3_sync_hash[ENV["to"]]}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment