mysqldump and wamp - php

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

Related

PHP shell_exec() not executing -- no errors

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.

MySql table lock while using MySqlDump remotely?

I'm using a php script to backup my sql databases remotely that utilizes mysqldump. http://www.dagondesign.com/files/backup_dbs.txt
and I tried to add the the --lock-tables=false since I'm using MyISAM tables but still got an error.
exec( "$MYSQL_PATH/mysqldump --lock-tables=false $db_auth --opt $db 2>&1 >$BACKUP_TEMP/$db.sql", $output, $res);
error:
mysqldump: Couldn't execute 'show fields from `advisory_info`': Can't create/write to file 'E:\tmp\#sql_59c_0.MYD' (Errcode: 17) (1)
Someone told me this file was the lock file it self and I was able to find it in my Server that I wanted to backup.
So is this the lock file? And does it lock the database if you do remotely no matter if I put the variable --lock-tables=false? Or should it not be there since there are a lot of people working on the server and someone might have created it?
It's likely --lock-tables=false isn't doing what you think it's doing. Since you're passing --lock-tables, it's probably assuming you do want to lock the tables (even though this is the default), so it's locking them. In Linux, we don't prevent flags but appending something like =false or =0, but normally by having a --skip-X or --no-X.
You might want to try --skip-opt:
--skip-opt Disable --opt. Disables --add-drop-table, --add-locks,
--lock-tables, --set-charset, and --disable-keys.
Because --opt is enabled by default, you can --skip-opt then add back any flags you want.
On Windows 7 using Wamp, the option is --skip-lock-tables
Took from this answer

interactive prompts with proc_open() on psql query

I am trying to execute a few PostgreSQL DB commands from a web interface.
I use proc_open() to pipe to the Windows command prompt.
Because psql (and all other postgres command) do not accept the password as an option, I must send the password to the write stream.
The code below causes the browser to hang. Either the resource is not be created, or the password is not being piped properly. Any suggestions are welcome at this point.
$cmd = '""C:\\Program files\\PostgreSQL\\9.0\\bin\\psql.exe" --host localhost --port 5432 -U postgres --dbname $database_name --command "$query""';
$p=proc_open($cmd,
array(array("pipe","r"), array("pipe","w"), array("pipe","w")),
$pipes);
if (is_resource($p)){
fwrite($pipes[0],"mypassword");
fclose($pipes[0]);
proc_terminate($p);
proc_close($p);
}
[You'll notice the crazy double-double-quoting in the command -- this is apparently needed for windows for some reason.]
Work-arounds to this problem are welcome:
I previously tried using system() and exec() but gave up since they don't handle interactive prompt. Is there a better option in php for interactive?
pg_query() is the main command for interacting with the postgres DB, but pg_dump and pg_restore operations are not supported. Is there another way to backup and restore from binary postgres .backup files that can be accomplished with php?
Don't mess around with the password prompt, better put an entry into %APPDATA%\postgresql\pgpass.conf. Format is
hostname:port:database:username:password
Make sure to pick the %APPDATA% of the user running the webserver process.
If you're really set on interacting with the prompt, you could try the Expect library which people often use for such tasks... disclaimer: I've never used it on windows and have no idea how well it works there, or if it really is necessary. Maybe your fwrite is just missing a terminating newline.
As #c-ramseyer suggested, messing around with simulating an interactive prompt via proc_open() was a non-starter. PostgreSQL offers two methods to get around providing the password through the interactive prompt. Method (1) is to provide it as environment variables, as suggested by the other answer. Method (2) is to create a pgpass.conf file in the DB user's %appinfo% directiory. (To find that directory do echo %appinfo% from windows command prompt.) See postgresql for how to make this one-liner conf file. Neither of these methods worked for me, for reasons I still don't understand.
To solve the problem, I had to modify the ph_hda.conf file (PostgreSQL Client Authentication Configuration File) to disable authentication. That file is located in the postgresql/data directory. I commented out the 2 lines of default settings at the bottom and replaced them with
#TYPE DATABASE USER CIDR-ADDRESS METHOD
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
Now if I call postgres from php I include the --no-password option and the sucker works. Note that this solution is not secure, and only makes sense in my case because it is being used for an internal company application, with machines running offline. This method should not be used for a production site, your DB will get hacked. Here's the php code.
$commande_restore = '""'.$postgres_bin_directory.'pg_restore" --host 127.0.0.1 --port 5432 -U postgres --no-password -d '.$database_name.' '.$restore_file.'"';
$this->execute($commande_restore);
function execute($cmd, $env = null){
$proc=proc_open($cmd,array(0=>array('pipe','r'),1=>array('pipe','w'),2=>array('pipe','w')),$pipes, null, $env = null);
//fwrite($pipes[0],$stdin); //add to argument if you want to pass stdin
fclose($pipes[0]);
$stdout=stream_get_contents($pipes[1]); fclose($pipes[1]);
$stderr=stream_get_contents($pipes[2]); fclose($pipes[2]);
$return=proc_close($proc);
return array( 'stdout'=>$stdout, 'stderr'=>$stderr, 'return'=>$return );
}
It took me close to 2 weeks to solve this, so I hope it helps someone.

How to export full mysql database with php script...? Problem in using 'mysqldump'

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.

PHP MySQL Backup script on IIS Server

Bit of a newbie, so please excuse the lack of terminology. . . .
I have a PHP script to backup a MySQL database "dbjobs".
I've tried nearly everything I can but can't get it to work.
It works if I run the $command directly from the Command Prompt on the server, but everytime I try to run the PHP version, I get an HTTP 500 error.
<?php
$backupFile = "DBJobs_" . date("Y-m-d");
$command = "\"mysqldump.exe\" --opt -hlocalhost -uUser -pPasswword dbjobs > c:/backup.sql";
$result = system($command);
if ($command !== false) {
echo "<p>Backup file created!</p>";
}
else {
echo "<p>There was a problem!</p>";
}
?>
I have tried the exec() function instead of system() but still does the same.
Does anyone know where I am going wrong?
Thanks
It's very likely both a path issue (the web server doesn't know where the executable is) and a permission issue. Typically, IIS isn't able execute a shell (cmd.exe) and so isn't going to be able to run the executable, so it would have to have permissions to get a shell and to run the MySQL utility.
Your other alternative is to have the database do it for you with a select statement that writes to an output file.
A couple of options to try are:
full path to the mysqldump.exe in the $command line of code
You might try backticks $command instead of system (backticks key also has the tilda symbol when you shift)
Could be a permissions issue???
You have to grant read/execute permissions to the account under which IIS is running on C:\WINDOWS\System32\cmd.exe. Check this out.

Categories