Created
July 8, 2009 19:42
-
-
Save crofty/143122 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
# Capistrano task for importing database from production | |
require 'yaml' | |
desc "Copy the remote production database to the local development database" | |
task :backup, :roles => :db, :only => { :primary => true } do | |
filename = "#{application}.dump.#{Time.now.to_i}.sql.bz2" | |
file = "/tmp/#{filename}" | |
on_rollback { delete file } | |
get "#{current_path}/config/database.yml", "tmp/prod_database.yml" | |
db = YAML::load(ERB.new(IO.read(File.join(File.dirname(__FILE__), '/../tmp/prod_database.yml'))).result)['production'] | |
run "mysqldump --add-drop-database -u #{db['username']} --password=#{db['password']} #{db['database']} | bzip2 -c > #{file}" do |ch, stream, out| | |
puts out | |
end | |
`mkdir -p #{File.dirname(__FILE__)}/../backups/` | |
get file, "backups/#{filename}" | |
run "rm #{file}" | |
File.delete(File.join(File.dirname(__FILE__), '/../tmp/prod_database.yml')) | |
end | |
desc "Copy the latest backup to the local development database" | |
task :import_backup do | |
filename = `ls -tr backups | tail -n 1`.chomp | |
if filename.empty? | |
logger.important "No backups found" | |
else | |
ddb = YAML::load(ERB.new(IO.read(File.join(File.dirname(__FILE__), 'database.yml'))).result)['development'] | |
logger.debug "Loading backups/#{filename} into local development database" | |
`bzip2 -cd backups/#{filename} | mysql -u #{ddb['username']} --password=#{ddb['password']} #{ddb['database']}` | |
logger.debug "command finished" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment