-
-
Save rantler/3815b467aeff9ebcd97c 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
# encoding: utf-8 | |
namespace :db do | |
desc "Import DB Data" | |
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=qa s3=true | |
## or no s3 | |
## example rake db:import:production to=staging | |
## or default to developtment with no s3 | |
## rake db:import:production | |
## this rake task will import production database 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" if ENV["to"].blank? | |
s3_sync_hash = { | |
development: "somethin-dev", # this supposed to be "something-dev" ??? | |
staging: "something-stg", | |
production: "something-prod", | |
}.with_indifferent_access | |
allowed_dbs_to_update = %w[development staging] | |
## variables to set ## | |
unless allowed_dbs_to_update.include?(ENV["to"]) | |
# This should probably be: | |
# raise ArgumentError.new("please set to= to one of the following #{allowed_dbs_to_update}") | |
puts "please set to= to one of the following #{allowed_dbs_to_update}" | |
next | |
end | |
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" | |
# drop and create the #{to_config[:database]} | |
client = Mysql2::Client.new(:host => to_config[:host], :username => to_config[:username], :password => to_config[:password]) | |
client.query("DROP DATABASE IF EXISTS #{to_config[:database]}") | |
client.query("CREATE DATABASE #{to_config[:database]}") | |
client.close | |
puts "Dropped and re-created #{to_config[:database]}" | |
# inject dropped sql into #{to_config[:database]} | |
if to_config[:password].blank? | |
system("mysql -u#{to_config[:username]} -h #{to_config[:host]} #{to_config[:database]} < tmp/#{to_config[:database]}.sql") | |
else | |
system("mysql -u#{to_config[:username]} -p#{to_config[:password]} -h #{to_config[:host]} #{to_config[:database]} < tmp/#{to_config[:database]}.sql") | |
end | |
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