Created
September 8, 2011 05:03
-
-
Save georges/1202665 to your computer and use it in GitHub Desktop.
Capistrano task for db cloning
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 | |
task :backup_name, :roles => :db, :only => { :primary => true } do | |
now = Time.now | |
run "mkdir -p db_backups" | |
backup_time = [now.year,now.month,now.day,now.hour,now.min,now.sec].join('-') | |
set :backup_file, "db_backups/#{application}-snapshot-#{backup_time}.sql" | |
end | |
desc 'Load remote production database into local development environment' | |
task :clone_to_local, :roles => :db, :only => { :primary => true } do | |
conf = YAML.load_file("config/database.yml")['production'] | |
mysql_command = [ | |
"mysqldump --add-drop-table", | |
("-u #{conf['username']}" if conf['username']), | |
("-p#{conf['password']}" if conf['password']), | |
("-h #{conf['host']}" if conf['host']), | |
conf['database'] | |
].compact.join(' ') | |
backup_name | |
command = "#{mysql_command} | bzip2 -c > #{backup_file}.bz2" | |
run command | |
system 'mkdir -p tmp' unless File.directory?('tmp') | |
local_file = "tmp/#{application}.sql.bz2" | |
logger.info 'downloading remote backup file ...' | |
get "#{backup_file}.bz2", local_file | |
conf = YAML.load_file("config/database.yml")['development'] | |
mysql_command = [ | |
"mysql", | |
("-u #{conf['username']}" if conf['username']), | |
("-p#{conf['password']}" if conf['password']), | |
("-h #{conf['host']}" if conf['host']), | |
conf['database'] | |
].compact.join(' ') | |
command = "bzcat #{local_file} | #{mysql_command}" | |
logger.info 'loading database from remote backup ...' | |
logger.debug command | |
system command | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment