backup mysql database by calling a batch file through php - 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 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 the exec 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
the exec 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 to
realpath()).

Related

PHP script fails when launched through crontab ("Directory nonexistent")

I have a PHP script creating a MySQL dump, working perfectly when launched from the browser. It calls mysqldump through the exec() function.
I scheduled this PHP script as root in the crontab (on an Ubuntu VPS). It fails with the following error message :
sh: 1: cannot create ./backups/db_20220522.sql: Directory nonexistent
I have set_include_path('/var/www/mysite.com/'); at the beginning of my PHP script. The script and the "backups" directory are in there.
What am I missing ?
Thanks a lot for your help
You need the specify the full path to your backup directory in your script .
Cron runs from a different directory.
eg
/home/username/domains/domain.com/public_html/backup

Unable to execute mysqldump from PHP script

I am trying to create periodic backups (poor man's cron) of my database using mysqldump with exec() function. I am using XAMPP/PHP7 on macOS.
$command = "$mysqldump_location -u$db_user -h$db_host -p$db_password $db_name > $backup_file_location";
exec($command);
When I run the PHP script, I get no SQL dump in the path mentioned in $backup_file_location but if I execute the same $command string on the terminal directly I get the desired SQL file in the desired location.
I am unable to understand what could be the problem here. Also open to suggestions on better ways to dump the entire DB.
Edit 1:
The value of $mysqldump_location is /Applications/XAMPP/xamppfiles/bin/mysqldump
The value of $backup_file_location is /Applications/XAMPP/xamppfiles/htdocs/app5/data/sqldumps/sql_data.sql
/app5/ is the folder in while I am developing my app.
Edit 2:
Possible duplicate suggestion does not apply since the issue here was not on how to dump SQL backups. The key issue here was that the backup using mysqldump was working through terminal, but not through PHP's exec() function.
The resolution of the issue, from above comments, was that the PHP request executes in XAMPP as a user that has limited privileges, and the mysqldump process inherits those privileges.
Checking the exit status of the process run by exec() confirmed that mysqldump exited with a nonzero exit status, indicating it failed for some reason.
Opening write privileges to 777 on the directory where the mysqldump process tries to write resolved the error.
It should also be adequate to figure out the specific uid & gid of Apache processes (check the User and Group config values in the Apache config file (e.g. xampp-home/apache/conf/httpd.conf) and make the output directory writeable by that uid or gid.

PHP exec() and command line switches

So I am using PHP exec() to run a batch file on my server:
echo exec("printCountries.bat");
The batch file opens Microsoft Access 2007 and runs a macro to print a form and then close Access:
START /MIN /B msaccess.exe "C:\inetpub\wwwroot\system\reports.accdb" /X printCountries
I can run the batch file from the command line successfully and the form prints to our network printer (this is for a company intranet) and closes Access. But when I run the PHP script, the batch file only runs the Access Process but does not perform the command line switches or open the database file.
I have configured PHP to have the right privileges on IIS, I am just stumped as to why the command line switches don't work and the batch file won't open the database.
Any ideas?
here is the echo output:
C:\inetpub\wwwroot\scale>START /MIN /B msaccess.exe "C:\inetpub\wwwroot\system\reports.accdb" /X printCountries
It looks like you have some access rights problem.
Did you try to run in PHP this echo exec("whoami");
What username you get?
Something like "nt authority\iusr" ?
You have to give to the default IIS user (i.e. IIS_IUSRS) full access to the path that your scripts lives.
In addition you can change (only TEMPORARILY huh?) the default application pool user from ApplicationPoolIdentity to LocalService or LocalSystem to test the results...
If I were you, I would created a new Application in IIS then I would created a new Application Pool and I would assigned it to this app. Then I created a new user that will have access to all the appropriate directories (web dir and where the bat is located).
If nothing of the above worked I would test my php application from my command line (locally) using the php command in my local command prompt.
I am neither good with PHP, nor with Access. However, when you invoke the batch file from PHP, please create an instance of cmd.exe and pass "/c printCountries.bat" to cmd.exe. In other words, the command run by PHP exec should look something like this (assuming PATH is set correctly):
cmd.exe /c printCountries.bat
Also, inside printContries.bat, I think you will need to wait until the access process terminates. So it should look like
START /WAIT /MIN /B msaccess.exe "C:\inetpub\wwwroot\system\reports.accdb" /X printCountries

fgets() doesn't work when executed through Command Prompt

I have been scratching my head over why my code didn't work for a couple of days now (since when I printed my variables out they were fine, but wouldn't record into the database) but now I realized that the issue is that PHP isn't reading the CSV file (basically what I needed to do was take a CSV file, and port it to a PostgreSQL database).
I now realize that the reason it doesn't work is because I am executing the code through the command line when trying to write to the database, but in the browser when testing.
This led me to realize that the file isn't being read when I execute the code through cmd. The CSV file is too big to execute in a browser and causes an internal server error so I have to execute it through cmd, and yet when I do, it doesn't read the file, so how would I go about fixing that?
Most likely you have file owner/permission problems. When you execute script in browser it is exectuted as user www-data (or whatever your user is). When you run from command line you run it as yourself, which is another user.
Either change file permissions or run your script from web user, for example
sudo -u www-data php yourscriptname.php

Running sqlcmd from php

I have a pretty complicated bunch of sql calls that I have stored to a .sql file, that I can't call using sqlsrv_query because there are a bunch of GOs.
From the (windows) command line I can run the sql file using
"path\to\sqlcmd" -U name -P pass -S loc -i "path\to\stuff.sql"
I can also write system( [all that] ) in a php file that I run from the command line using php -f.
However, if I call the same system( [allthat] ) from a php file that I call using ajax on a website, I get no feedback, the sql just doesn't run. I've checked sqlcmd, and it has permissions to allow all users to execute.
Any ideas?
Permissions issue. If you're running it from the command line, it's running under YOUR permissions. When you invoke it via a webserver, it's running with the webserver's permisisons. The webserver will need execute rights (and permission to REACH the sqlcmd) as well as read rights on the .sql file (and permission to reach it).
For that, you may use the shell_exec.

Categories