Skip to content

Instantly share code, notes, and snippets.

@georges
Created September 8, 2011 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georges/1202665 to your computer and use it in GitHub Desktop.
Save georges/1202665 to your computer and use it in GitHub Desktop.
Capistrano task for db cloning
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