My system: Ubuntu 11.10, LAMP Stack.
Issue:
I run the following in terminal and it does the back up correctly.
mysqldump -u root dbBugTracker > BAK/dbw.sql
But I include it in my php code like the following and it does NOT work.
exec('/usr/bin/mysqldump -u root dbTracker > BAK/dbT.sql');
Tips:
I tried putting a second parameter in exec but nothing is shown except the word Array. I print it out but nothing in it.
The file dbw.sql is actually created as a result of the exec function but it is 0 bytes.
I tried with the full path and without for mysql and the same result is seen. i.e., 0 bytes.
The folder BAK is within my project folder and I even gave it 777 permissions.
Even tried different file names and databases but the result is the same.
I appreciate any inputs on this. Thank!
MORE INFO:
I added 2>&1 to the exec line and NOW the file contains some text but NOT the DB dump. This is an error and I have no idea how to deal with this :(
Here's the error
mysqldump: Got error: 1045: Access denied for user 'root'#'localhost' (using password: NO) when trying to connect
So this is what the output file (dbw.sql) now contains.
Once again, it works fine when I run the dump from terminal.
You're running that dump command as a different user while on the command line. You are running it as Apache (I assume) when using exec(). Try adding a password parameter to the exec command, or creating an php-specific user in your db with appropriate privileges.
UPDATE:: As I guessed, you are not able to use the root user while executing this dump using PHP. So, create a new user.
First, login to your database from the command line. If you are the root user, don't bother with using -u root:
mysql
Now that you're logged in, go ahead and create a new user for Apache to use:
GRANT ALL ON database_name.* TO yourapacheuser#localhost IDENTIFIED BY 'yourpassword';
Go ahead and logout of mysql:
exit
Next, let's re-work your original code a bit...
$db_user = 'newusername';
$db_pass = 'pass';
$command = "mysqldump --add-drop-table -u $db_user -p$db_pass database_name > backup.file.sql";
$output = `$command`;
echo "Your database has now been backed up.";
Now, to execute the file, run this from the command line:
php path/to/sqldumpfile.php
Hopefully you can adapt this pseudo-code. Best of luck!
How do you print it?
Debug it like this:
<?php
exec('/usr/bin/mysqldump -u root dbTracker > BAK/dbT.sql', $output);
var_dump($output);
First, you should get it working on the command line. Verify that this produces the desired results prior to using PHP's exec():
/usr/bin/mysqldump -u root -p YOUR_PASSWORD dbTracker > BAK/dbT.sql
If it DOES work, then it's an issue somewhere in your PHP config.
The first thing to check is safe_mode. Are you using safe_mode? What version of PHP are you running?
Another possibility may be that your PHP user does not have permission to use the mysqldump binary.
Related
Can't solve the problem.
Briefly: I need to execute command on remote server via ssh.
I have two PCs from where I do that job. Ubuntu server - no problem. But from FreeBSD - problem.
Target server is under Freebsd too.
Access is via ssh keys, user - root. In my client Freebsd I can do ssh root#x.x.x.x without any password, so we make conclusion that this part is ok. Next. I run php artisan tinker (for those who don't know what is this - this is terminal for executing php code). There I do:
exec("ssh root#x.x.x.x whoami");
And I get 'root'. Work is correct. But I do the same via
dd(exec("ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -v root#x.x.x.x 2>&1"));
and I get:
"Permission denied (publickey,keyboard-interactive)."
For that command:
dd(exec("ssh -v root#91.222.216.24 whoami 2>&1"));
I get:
"Host key verification failed."
As I understand the only way is to use password, but why key auth fails?
How to fix that?
I've tried many options (read many articles on stackoverflow) and in some combination of code I've found that it looks like when command executed via exec another user (www) is used, not root. And I got errors like:
Could not create directory '/nonexistent/.ssh'.
As I understand the problem connected to user and its permissions. And it looks like there is no any user at all when sshing via exec.
Upd.
Right in terminal on client PC
php -r "echo(exec('whoami'));"
gives. 'root'.
But the same inside of controller in Laravel while using php exec
dd(exec('whoami'));
gives 'www'.
For some reasons in php (exec command) different user is used.
And yes, in Ubuntu, where I have no problems, I get the same user in both cases (I've checked just now), that's why everything is working there. So,.. the question is How to create ssh keys for another user, which is 'www' or should I change somehow user for php?
I created this DB Copy tool for my company, and everything is completely functional locally. I rsync'ed it to our tool server and now it seems it is unable to execute the shell command I'm passing with shell_exec.
$command = "/usr/bin/mysqldump -v -u$dbUser -p$dbPass ";
$command .= "-h$sourceHost -P$sourcePort $sourceDB 2>../data/dump.log ";
$command .= "| /usr/bin/mysql -u$dbUser -p$dbPass ";
$command .= "-h$targetHost -P$targetPort $targetDB 2>../data/error.log";
$output = shell_exec($command);
I capture the stderr output of the mysql dump, because that is actually used by mysqldump to show progress (dumping this table, dumping this table etc) and I read from that file on my page to show progress.
Neither of the files specified are even being created. This makes me think there is some sort of permissions issue. However I can see that my directories all have the same permissions as others on this server for working tools. Doesn't rule it out, but I'm not sure what to assess further.
I tried changing shell_exec() to exec() and adding a variable to capture the output ( exec($command, $shellOutput) ) and $shellOutput was empty.
I echo'd the command to be run, after it is formed by the script, and was able to run that on the server with no issues.
I'm a bit at a loss here, as it's not even giving me any feedback to work with. Any ideas on what I can try?
To add context, this is a page that is being called by $.ajax to kick off this dump. After this shell command, there is an echo of what db was copied, and that is returning. So the page is being called correctly, it seems to only be the shell command itself that is simply not working.
Thanks in advance.
*Also worth noting that our directory structure is such that our www folder of our project is symlinked to the documentRoot of the server.
You can try system("ur command 2>&1");
This should work and if not output why it doesn't work.
Am sure its to do with your permissions
ls -ld directory
If thats the problem change mode chmod to your preference
Etan had it correct. It was something new to me that the files are created before anything it run (but it makes complete sense). I took the files out, and the command ran. I ended up having to give apache permission to write to the data directory in my project.
Still wrapping my head around linux permissions.
Thanks all.
I would like to create a php script to execute a shell command and return its output. The server requires a private key. When I first decided to test this out I created this:
<?php
$command = "ls";
$output = shell_exec($command);
echo "<pre>$output</pre>";
?>
That worked just fine. But when I changed $command to the command I really wanted to run:
$command = "/etc/init.d/mycontrollerd status /etc/mycontrollerconfig";
it gave me this output:
You need root privileges to run this script
My guess is I need to use sudo. Of course that will require putting the pem file somewhere on the server. Assuming I do that, what exactly should $command be? Should I use shell_exec(), exec(), system() or something else?
It does not matter which php function you use to start the script - what lacks is the authorization of your user account.
Either use sudo (preconfigure the web server user to run the exact command without password via visudo, and prefix the command with sudo) or set up a setuid script that executes the command on itself.
What you really need to do is set your web server to run as a specific user (other than 'nobody' for example), or give that user permissions to what you want to execute.
See also: PHP shell_exec() and sudo: must be setuid root
i am using this command to backup my full mysql database..
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.sql';
$command = "mysqldump -h$hostname -u$username -p$password $dbname > $backupFile";
system($command);
I am getting blank file.
And i am using XAMMP on windows.
I have already used exec() but also getting blank file.
And but on shell it has successfully done.
Whats wrong in this code.
The best thing you can do is check what is going wrong. You might even want to check outside of PHP to see what you are doing. So echo your $command, and look at it, see if it looks correct. Then use it on the commandline, see if you can get it to work.
Possible attention points:
Do you have any 'strange' characters (like &) in your password? You might need to escape them
Is the mysqldump command available? (is it in your PATH, or in the dir where you are running this from
Are you allowed to do any system/exec commands at all?
Is the user that runs your php code (apache?) allowed to do this command?
Is the user that runs this code allowed to write in this directory?
To test your current command, you might want to do this:
- replace your system command with an echo: echo $command;
- run the script and copy the command you see there.
- Open a terminal. (start->run->"cmd")
- goto the dir where your script is / runs.
- paste /type the command.
- check your result.
I do not know what happens when you do not have a password, but still supply the -p option. It might try and ask for a password anyway, as you've indicated you want to enter a password, but have not provided it. I do not know this for sure, that's why you might want to check it. (#wimvds confirms in the comment: if you supply a -p and no password, you'll get a "password: " dialog.)
In the commandline you can check what command you need to type to get the mysqldump to work. If that's ok, then make sure your script actually issues that command. Then test again with the script.
Update: Finally got this thing working but still not sure what the problem was. I am using a wamp server that I access through a networked folder.
The problem that still exists is that to execute the mysqldump I have to access the php file from the actual machine that is being used to host the WAMP server.
End of update
I am running a wamp server and trying to use mysqldump to backup a mysql database I have. The following is the PHP code I am using to run mysqldump.
exec("mysqldump backup -u$user -p$pass > $sql_file");
When I run the script the page just loads inifnately and the backup is not created.
A blank file is being created so I know something is happening.
Extra info:
* exec() is not disabled
* PHP is not running in safe mode
Any ideas??
Win XP, WAMP, MYSQL 5.0.51b
mysqldump is likely to exceed the maximal time php is supposed to run on your system. Try using the command in cmd or increase the max_execution_time in your php.ini .
Are you sure $pass is defined and doesn't have a space character at the start?
If it wasn't, mysqldump would be waiting for command line entry of the password.
I had the same thing happen a while back. A co-worker pointed me to the MySQL GUI tools and I have been making backups with that. The Query Browser that comes with it is nice, too.
MySQL GUI tools
It might help to look at the stderr output from mysqldump:
$cmd = "mysqldump backup -u$user -p$pass 2>&1 > $sql_file";
exec($cmd, $output, $return);
if ($return != 0) { //0 is ok
die('Error: ' . implode("\r\n", $output));
}
Also you should use escapeshellarg() if $user or $pass are user-supplied.
I've also struggled with using the mysqldump utility. I few things to check/try based on my experience:
Is your server set up to allow programs to run programs with an exec command? (My webhost's server won't let me.) Test with a different command.
Is the mysqldump utility installed? Check with whereis mysqldump.
Try adding the optimize argument --opt