Capistrano: Synchronize local mysql database to remote mysql database


/ Published in: Rails
Save to your folder(s)

This could be further abstracted to work with other db's. Also, it really should be using Ruby's Tempfile class instead of assuming the local machine is *NIX based, but it works :)


Copy this code and paste it in your HTML
  1. namespace :db do
  2. task :sync_to_local, :roles => :web do
  3. sql_out = "#{environment_database}.sql"
  4. run "mysqldump --user #{dbuser} --password=#{dbpass} #{environment_database} > #{sql_out}"
  5. system "sftp #{user}@myhost.com:#{sql_out} /tmp/#{sql_out}"
  6. run "rm #{sql_out}"
  7. system "mysql -u #{dbuser} --password=#{dbpass} mydatabase_development < /tmp/#{sql_out}"
  8. system "rm /tmp/#{sql_out}"
  9. end
  10. end
  11.  
  12. # invoked: cap production db:sync_to_local

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.