3

I'm playing around with a backup script for mysql. A variation of this used to work, but I haven't looked at it since php4. It's returning an empty file. The weird thing is that if I go to the command line and use the EXACT same command, I get the file I'm expecting.

I've poked around the internet and can't find anything... thoughts?

Bad code?

$db_host='localhost';
$db_user='root';
$db_pass='root';
$db_name='gakkou';
$dir='backups';
$file_list=scandir($dir);
if(count($file_list)>10) unlink($dir.'/'.$file_list[2]); //delete old file
$prefix=date("YmdHi").'_';
$command='mysqldump -u'.$db_user.' --password="'.$db_pass.'" --databases '.$db_name.' | gzip > '.$dir.'/'.$prefix.'_backup.sql.gzip';
exec($command,$output,$return_val);

This works perfectly: mysqldump -uroot -proot -hlocalhost gakkou > /webdocs/gakkou/backups/mysql_backup.sql and it is exactly the same as the command the php file executes (except for the file name).

EDIT: updated with working code for anyone interested. This turned out to be two separate issues. Using MAMP, I needed to specify the path /Applications/MAMP/Library/bin/mysqldump. Then on the production server the crazy password gummed up the works.

6
  • 4
    is mysqldump in the path of whatever shell PHP's using? try a fullblown /usr/bin/mysqldump or whatever the absolute path is on your system. Plus, if it's failing, exec($command, $output, $return_val); var_dump($output, $return_val) will probably have some debug output useful for tracking down the problem. Commented Mar 2, 2014 at 4:07
  • it's maybe a permission problem. Commented Mar 2, 2014 at 4:17
  • Thanks, Marc. Absolute path seems to have done the trick on my development machine, but it still fails on the real deal. array(4) { [0]=> string(44) "Usage: mysqldump [OPTIONS] database [tables]" [1]=> string(65) "OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]" [2]=> string(52) "OR mysqldump [OPTIONS] --all-databases [OPTIONS]" [3]=> string(38) "For more options, use mysqldump --help" } int(127) Commented Mar 2, 2014 at 4:33
  • The above seems to imply that the command I'm executing is bad, but when I echo it out I get what I'd expect mysqldump -uUSER -pPASSWORD -hlocalhost DB_NAME > backups/23-20140301_gakkou_backup.sql Commented Mar 2, 2014 at 5:02
  • 2
    OK. FINALLY got it figured out. My super stupid password was messing with mysqldump because it had an "&" in it. Didn't have to change the password. I just enclosed the password in parentheses: --password="'.$db_pass.'" Now it works as expected. So many hours wasted... @MarcB, thank you so much for your help. Didn't know how to return the errors and that was definitely the biggest roadblock. Commented Mar 2, 2014 at 15:38

1 Answer 1

5

OK. FINALLY got it figured out. My super stupid password was messing with mysqldump because it had an "&" in it. Didn't have to change the password. I just enclosed the password in parentheses: --password="'.$db_pass.'" Now it works as expected. So many hours wasted... @MarcB, thank you so much for your help. Didn't know how to return the errors and that was definitely the biggest roadblock.

Sign up to request clarification or add additional context in comments.

2 Comments

@shi, comment is now answer! :)
You saved my day :) :). was struggling with this for so long thinking that it is an error in the syntax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.