Forum Moderators: coopster

Message Too Old, No Replies

mysqldump creates a new empty file

         

mattch80

8:00 pm on May 7, 2010 (gmt 0)

10+ Year Member



When I try this it creates an empty file:

$filename = DB_NAME.date("Y-m-d-H-i-s").".sql";
$command = "mysqldump --opt -h ".DB_SERVER." -u ".DB_USER." -p ".DB_PASS." ".DB_NAME." > $filename";
system($command);

jatar_k

8:35 pm on May 7, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you tried it with the proper values via the commandline?
are you sure those vars are populated?

mattch80

10:14 pm on May 7, 2010 (gmt 0)

10+ Year Member



I re-checked the variables. I can get it working from command line (running on my imac) using the full paths:

Matts-iMac:~ Matt$ /Applications/XAMPP/xamppfiles/bin/mysqldump --user=root --password=___ dbname > /Users/Matt/Sites/___/backup/backup.sql

But adding the full paths in my php script didn't help.

rocknbil

7:11 pm on May 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add some error checking.


$filename = DB_NAME.date("Y-m-d-H-i-s").".sql";
$command = "mysqldump --opt -h ".DB_SERVER." -u ".DB_USER." -p ".DB_PASS." ".DB_NAME." > $filename";
system($command,$status);
if ($status==FALSE) { echo "some error occurred in executing the command. The command was $command"; }

mattch80

7:34 pm on May 8, 2010 (gmt 0)

10+ Year Member



rocknbil, I tried that and it didn't return false.

Matthew1980

8:30 pm on May 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there mattch80,

Have you made sure that the actual $command var you are building is being populated as if it were being done via the cmd line? Because if it does work via the cmd line, then its down to the way the the var is being built.

Personally I would do this too, though it may have no impact, but as you are adding it... :-

$command = "mysqldump --opt -h ".DB_SERVER." -u ".DB_USER." -p ".DB_PASS." ".DB_NAME." > ".$filename;

Good luck!

Cheers,
MRb

mattch80

10:38 pm on May 8, 2010 (gmt 0)

10+ Year Member



I finally got it to work in php with this as the command:

$command = "/Applications/XAMPP/xamppfiles/bin/mysqldump --opt --host=".DB_SERVER." --user=".DB_USER." --pass=".DB_PASS." ".DB_NAME." > ".$filename;

Does anyone know why this works when the single dash version doesn't? (--host= instead of -h)

rocknbil

12:32 am on May 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well there are **two** differences there. :-) You have the full path too, it could have been that, or it could have been it's not understanding the -h switch for whatever reason. The mysqldump man page would be where to look. So you know what's happening,

system("blah > $filename");

Is trying to exec "blah" but however that works out, > will always create and dump output from blah into $filename. No output from blah . . . empty file.

Did $status return anything at all? Not that it matters . . . usually you will get some sort of return code in other languages.

mattch80

3:28 am on May 9, 2010 (gmt 0)

10+ Year Member



I did some more testing. The -p and not having the full path to mysqldump seemed to cause the problem (not sure why yet). When it's not working $status returns a 2 or 127, and when it's working a 0.

rocknbil

7:35 pm on May 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



when it's working a 0.


I wasn't surewith PHP, but I had a bit of incorrect info, $status will never be FALSE, it's system() that returns FALSE, not the command within it. $status is the code from executing the actual command. This verifies you can do

system($command,$status);
if ($status > 0) { die("Command $command died with status $status"); }

What $status represents is a bit complicated and OS dependent. In perl we use an 8 bit shift or divide by 127 to get at the child error code, or more simply, print out the STDERR filehandle . . . . not sure how you'd do that in PHP.

All of which is probably unnecessary now that it is working . . . just look for $status==0 or error.

coopster

1:32 pm on May 11, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If the mysqldump command is not in the system path definition/scope then you will always need the full path to the executable command. Period.

The other issue I see here is that the short version of the password switch requires no space between the switch and the argument:
mysqldump -h MyHostName -pMyPassWord


mysqldump [dev.mysql.com]