Frage

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?

War es hilfreich?

Lösung 2

The following script should achieve that:

# Find out which file to copy and save its name in a local text file:
# ssh allows you to specify a command that should be executed on the remote
# machine instead of opening a terminal session on it. I use this to get
# a sorted (ls -t sorts by modification date) list of all backups. I then
# truncate this list to one entry using head -1 and save the file name in a
# local file (filename.txt).
# (12.34.56.78 is a placeholder for the ip/hostname of your server)
ssh 12.34.56.78 ls -t /path/to/backups/ | head -1 > filename.txt

# Copy the backup specified in filename.txt to the tmp dir on your local machine.
scp 12.34.56.78:/path/to/backups/`cat filename.txt` /tmp/db_backup.sql.tar

# Untar the backup archive.
cd /tmp && tar -xf db_backup.sql.tar

# Import into database of choice.
mysql -u your_username -p database_to_import_to < /tmp/db_backup.sql

(This assumes that you are on a UNIX system and have scp and tar installed...)

Andere Tipps

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
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top