2

I want to backup the complete database before having a php script playing around with its tables so I thought about using mysqldump.

I tried to run the mysqldump command with shell_exec() and to check if it was successfull like this:

$command = 'mysqldump -u username -ppassword database > /path/database.sql';
$result = shell_exec($command);

if(!is_null($result)) {
  echo 'Error during backup';
else {
  echo 'Database saved';
  // rest of the php script to modify the database (create/drop tables etc)
}

But I noticed that $result is alwas null, even when I give a path where the file cannot be written (no existing directory, or something that should return a permission denied on the shell prompt).

What am I missing?

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Iam Zesh
  • 1,797
  • 2
  • 20
  • 42

3 Answers3

3

This worked for me:

$command = 'mysqldump -u username -ppassword database > /path/database.sql';
system($command, $output);

if($output != 0) {
  echo 'Error during backup';
else {
  echo 'Database saved';
  // rest of the php script to modify the database (create/drop tables etc)
}
Iam Zesh
  • 1,797
  • 2
  • 20
  • 42
  • 2
    It's worth noting that `system()`'s second argument is not the script output, it's the return value. Names aside, your code is fine but I thought I should clarify that to avoid confusion to future readers. – Álvaro González Feb 20 '13 at 15:07
  • Hi. You have syntax error on line 6 (missing bracket). – Tegos Sep 26 '17 at 07:55
1

From the manual:

Return Values
The output from the executed command or NULL if an error occurred

However, you are redirecting the script's output to a file:

mysqldump -u username -ppassword database > /path/database.sql
                                          ^^^^^^^^^^^^^^^^^^^^

It's possible (I don't really know) that mysqldump generates standard return codes (where 0 means OK and anything else means error). If that's the case, you should be using that mechanism, though you need a different PHP function:

string exec ( string $command [, array &$output [, int &$return_var ]] )
                                                   ^^^^^^^^^^^^^^^^
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Thanks for your answer. I didn't thought that redirecting the script's output could be the source of the problem. I tried with exec but $output was somehow inconsistent and not reliable and http://stackoverflow.com/a/10828773 oriented me to system(). – Iam Zesh Feb 20 '13 at 14:03
  • @IamZesh - Please note I highlighted `$return_var`, not `$output`. And of course whether that will work depends on whether mysqldump generates standard return values. I see from your answer that it does. – Álvaro González Feb 20 '13 at 15:04
1

You should remove all spaces between -u and -p and varialbes also use "exec()"

$command = "mysqldump -u$username -p$password database > /home/yourusername/public_html/database.sql";
exec($command,$result, $output);

if($output != 0) {
  echo 'Error during backup';
}else {
  echo 'Database saved';
  // rest of the php script to modify the database (create/drop tables etc)
}
Mohammad H.
  • 856
  • 9
  • 19