简体   繁体   中英

Running mysqldump from a PHP script doesn't work but works on SSH

I'm trying to back a database via a PHP script using this one simple line:

passthru("/usr/bin/mysqldump --opt --host=localhost --user=\"myuser\" --password=\"mypass\" db_name > /var/www/vhosts/mydomain.com/httpdocs/tools/dbbackup-2011-12-17.sql");

I get no errors or warnings or anything. but if I copy that string and execute it exactly as is on the server via SSH it works perfectly.

/usr/bin/mysqldump --opt --host=localhost --user="myuser" --password="mypass" db_name > /var/www/vhosts/mydomain.com/httpdocs/tools/dbbackup-2011-12-17.sql

I tried using exec() and system() instead of passthru() but same results. This is really strange and I can't focure out what could be the problem. Any ideas?

An educated guess

If I can make an educated guess it's because the user that runs the php-script (ie. user the httpd is run as) doesn't have permission to write to /var/www/vhosts/mydomain.com/httpdocs/tools/ .

Though the user you are using to execute the command has.


STDERR and STDOUT

To see if there is anything printed to STDERR that is relevant to the problem, use the below snippet!

$tubes = array(
  0 => array("pipe", "r"),
  // print contents on STDOUT to file
  1 => array("file", "/var/www/vhosts/mydomain.com/httpdocs/tools/dbbackup-2011-12-17.sql", "w"),
  2 => array("pipe", "w")
);

$p_handle = proc_open (
  "/usr/bin/mysqldump --opt --host=localhost --user=\"myuser\" --password=\"mypass\" db_name",
  $tubes, $pipes
);

if (is_resource ($p_handle)) {
    fclose ($pipes[0]);

    $stderr_data = stream_get_contents ($pipes[2]); fclose($pipes[2]);

    $proc_ret    = proc_close ($p_handle);

    echo "--------- STDERR:\n$stderr_data\n";
    echo "------------ RET: $proc_ret\n";
} else {
  die ("Unable to execute external resource, aborting!");
}

Check the log files!

Have you checked the error_log associated with your httpd ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM