backup mysql database by calling a batch file through php
I failed with the mysqldump method earlier. Now I'm trying to just call the batch file which will call the mysqldump. The php file which calls the 开发者_Go百科batch file, looks something like this:
<?php
shell_exec('c:\\abc.bat');
?>
And the batch file, which when I execute directly yields the 7kb sql file that I'm expecting:
mysqldump --opt -u root -ppassword onstor > c:\onstordb.sql
But when I use php to execute it, I get a 0 Kb file, I wonder why. I tried both shell_exec and exec. Is there something wrong with the parameters?
http://www.php.net/manual/en/function.exec.php#85930
It reads:
I was having trouble using the PHP
exec
command to execute any batch file. Executing other commands (i.e., "dir") works fine). But if I executed a batch file, I receieved no output from theexec
command.The server setup I have consists of Windows Server 2003 server running IIS6 and PHP 5.2.3. On this server, I have:
- Granted execute permissions to the Internet User on
c:\windows\system32\cmd.exe
.- Granted Everyone->Full Control to the directory in which the batch file is written.
- Granted Everyone->Full Control on the entire
c:\cygwin\bin
directory and its contents.- Granted the Internet User "log on as batch" permissions.
- Specified the full path to each file being executed.
- Tested these scripts running from the command line on the server and they work just fine.
- Ensured that
%systemroot%\system32
is in the system path.It turns out that even with all of the above in place on the server, I had to specify the full path to
cmd.exe
in theexec
call.When I used the call:
$output = exec("c:\\windows\\system32\\cmd.exe /c $batchFileToRun");
then everything worked fine. In my situation,
$batchFileToRun
was the actual system path to the batch file (i.e., the result of a call torealpath()
).
精彩评论