How do I create & restore a mysql database dump with Chef?

0

Environment

Objective

I am trying to replicate/clone my production Wordpress server.

I'd to be able to create a dump of the remote database

mysqldump --databases my_database > dump.sql

and restore

mysql -u my_user -p -D my_database < dump.sql

I have tried this without success:

mysql_anybots_connection_info = {
  :host     => 'localhost',
  :username => 'my_user',
  :password => "my_password"
}

mysql_database "my_database" do
   connection mysql_connection_info
   sql "source /path/dump.sql;"
end

and this

mysql_database "my_database" do
  connection mysql_connection_info
  sql { ::File.open("/pul/dump.sql").read }
  action :query
end

Which worked even less?

Any suggestions?

zabumba

Posted 2014-07-29T20:31:38.923

Reputation: 249

1Actually the latter one should work. "works even less" is a very bad error description. What's the error message that chef emits? Or is there no error message, but your log is empty? Please paste your log output in doubt. – StephenKing – 2014-07-30T01:55:07.580

Hey StephenKing, You are right to ask for the logs, but don't be picky on the english. – zabumba – 2014-07-30T23:45:07.417

Oh, I didn't want to complain about the English. Sorry, if it sounds so. It's only that "does not work" style statements don't help us readers while helping you. – StephenKing – 2014-07-31T00:16:59.133

try adding -c to mysqldump I forgot why, but it works for me. – cybernard – 2014-08-01T03:11:15.487

Answers

0

I was frustrated by this for some time, but finally found a solution.

In my situation the problem was with "DELIMITER", which is a command only used by the mysql command line client. It's not part of the API, which is what the mysql gem is using.

SOURCE: https://github.com/opscode-cookbooks/database/issues/75

To restore from the dump I use this function in the recipe:

execute 'restore-databases' do
  command "mysql -u root -p#{node['mysql']['server_root_password']} -D databaseName < /path/to_dump.sql"
end

JoeyG

Posted 2014-07-29T20:31:38.923

Reputation: 1