2

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 in /path/to/backups_dir/
  2. Untar it locally.
  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?

Daniel Fischer
  • 669
  • 1
  • 8
  • 11

1 Answers1

0

this is quite an old topic but here is how I handled it with Capistrano 3.

This will use your remote MySQL binary (for example, the one from your web server). You won't need a direct access to the MySQL server but MySQL binary must be installed on your remote server.

# ensure that the dump directory exists
%x{mkdir -p #{dump_dir}}

# run mysqldump command to backup the db from the remote server
args = "-u#{db_user} -p#{db_pass} -h#{db_host} #{db_name}"
dump = []

if fetch(:answer)== 'overwrite' then true else false end
# stream the output to local
output = capture(:mysqldump, args)
output.each_line do |line|
  dump << line
end

# write the streamed output to a file
File.open("#{dump_dir}/#{db_name}_#{stage}_#{date}.sql", 'w') do |file|
  file.puts(dump)
end

Then import to any MySQL database with %x

%x{mysql -u#{local_db_user} -p#{local_db_pass} -h#{local_db_host} #{local_db_name} < #{dump_dir}/#{db_name}_#{stage}_#{date}.sql}

Hope this will help someone else.

Tim
  • 101
  • 2