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?

3 Answers 3

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)
}
2
  • 3
    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. Commented Feb 20, 2013 at 15:07
  • Hi. You have syntax error on line 6 (missing bracket).
    – Tegos
    Commented Sep 26, 2017 at 7: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 ]] )
                                                   ^^^^^^^^^^^^^^^^
2
  • 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 stackoverflow.com/a/10828773 oriented me to system().
    – Iam Zesh
    Commented Feb 20, 2013 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. Commented Feb 20, 2013 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)
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.