lib/eycap/recipes/database.rb in engineyard-eycap-0.4.2 vs lib/eycap/recipes/database.rb in engineyard-eycap-0.4.3

- old
+ new

@@ -8,13 +8,25 @@ set :backup_file, "#{shared_path}/db_backups/#{environment_database}-snapshot-#{backup_time}.sql" end desc "Clone Production Database to Staging Database." task :clone_prod_to_staging, :roles => :db, :only => { :primary => true } do + + # This task currently runs only on traditional EY offerings. + # You need to have both a production and staging environment defined in + # your deploy.rb file. + backup_name on_rollback { run "rm -f #{backup_file}" } run("cat #{shared_path}/config/database.yml") { |channel, stream, data| @environment_info = YAML.load(data)[rails_env] } + + # If the production db hostname has a trailing -master string, substitute + # it with -replica. + # Elsif the production db hostname doesn't have a trailing -master, just + # append -replica. + # Else you're using PostgreSQL and we'll dump it that way. + if @environment_info['adapter'] == 'mysql' && production_dbhost.scan('-master') == true run "mysqldump --add-drop-table -u #{dbuser} -h #{production_dbhost.gsub('-master', '-replica')} #{production_database} -p > #{backup_file}" do |ch, stream, out| ch.send_data "#{dbpass}\n" if out=~ /^Enter password:/ end run "mysql -u #{dbuser} -p -h #{staging_dbhost} #{staging_database} < #{backup_file}" do |ch, stream, out| @@ -40,16 +52,14 @@ desc "Backup your MySQL or PostgreSQL database to shared_path+/db_backups" task :dump, :roles => :db, :only => {:primary => true} do backup_name run("cat #{shared_path}/config/database.yml") { |channel, stream, data| @environment_info = YAML.load(data)[rails_env] } - if @environment_info['adapter'] == 'mysql' && @environment_info['host'] != 'localhost' # For traditional offering - run "mysqldump --add-drop-table -u #{dbuser} -h #{environment_dbhost.gsub('-master', '-replica')} -p #{environment_database} | bzip2 -c > #{backup_file}.bz2" do |ch, stream, out | + if @environment_info['adapter'] == 'mysql' + dbhost = @environment_info['host'] + dbhost = environment_dbhost.sub('-master', '') + '-replica' if dbhost != 'localhost' # added for Solo offering, which uses localhost + run "mysqldump --add-drop-table -u #{dbuser} -h #{dbhost} -p #{environment_database} | bzip2 -c > #{backup_file}.bz2" do |ch, stream, out | ch.send_data "#{dbpass}\n" if out=~ /^Enter password:/ - end - elsif @environment_info['adapter'] == 'mysql' && @environment_info['host'] == 'localhost' # For Solo offering - run "mysqldump --add-drop-table -u #{sql_user} -h localhost -p #{sql_pass} | bzip2 -c > #{backup_file}.bz2" do |ch, stream, out | - ch.send_data "#{sql_pass}\n" if out=~ /^Enter password:/ end else run "pg_dump -W -c -U #{dbuser} -h #{environment_dbhost} #{environment_database} | bzip2 -c > #{backup_file}.bz2" do |ch, stream, out | ch.send_data "#{dbpass}\n" if out=~ /^Password:/ end