Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a capistrano task to download latest database_backup.tgz and import locally?

I'm trying to do something with a Capistrano task that is similar to the heroku db:pull functionality if you are familiar with it.

I have a remote server. On that server I have a bunch of backups in the /path/db_backups/ folder. And in that folder there's a backup of the database everyday.

All I want to do is

  1. Download the latest backup on the client machine.
  2. Untar it.
  3. Import it into local mysql db.

Anyone know of a good way to handle this? Is there a gem I am unaware of? Is there a script you have handy?

like image 688
Daniel Fischer Avatar asked Nov 21 '25 13:11

Daniel Fischer


1 Answers

I'm not sure if there is a gem for that. I usually copy/pastle this task on capistrano (config/deploy.rb) to pull a compressed database from the server and store it on my development environment

namespace :utils do
  desc 'pull the DB from the server'
  task :pull_db, :roles => :db, :only => { :primary => true } do

    website = "http://www.my_website.com"
    filename = "#{application}.dump.#{Time.now.to_f}.sql"
    filename_bz2 = "#{filename}.bz2"
    remote_file = "#{current_path}/public/#{filename_bz2}"


    text = capture "cat #{deploy_to}/current/config/database.yml"
    yaml = YAML::load(text)

    on_rollback { run "rm #{remote_file}" }
    run "mysqldump -h#{yaml[rails_env]['host']} -u #{yaml[rails_env]['username']} -p #{yaml[rails_env]['database']} | bzip2 -c > #{remote_file}" do |ch, stream, out|
      ch.send_data "#{yaml[rails_env]['password']}\n" if out =~ /^Enter password:/
    end

    local_text = run_locally("cat config/database.yml")
    local_yaml = YAML::load(local_text)

    run_locally("wget #{website}/#{filename_bz2}")
    run_locally("bzip2 -d #{filename_bz2}")

    run_locally("bundle exec rake db:drop")
    run_locally("bundle exec rake db:create")

    if local_yaml['development']['password'] && !local_yaml['development']['password'].blank?
      run_locally("mysql -h#{local_yaml['development']['host']} -u#{local_yaml['development']['username']} -p#{local_yaml['development']['password']} #{local_yaml['development']['database']} < #{filename}")
    else
      run_locally("mysql -h#{local_yaml['development']['host']} -u#{local_yaml['development']['username']} #{local_yaml['development']['database']} < #{filename}")
    end

    run_locally("rm #{filename}")
    run "rm #{remote_file}"
  end
end
like image 136
rorra Avatar answered Nov 23 '25 04:11

rorra