Jump to content

mysqldump, exec() and escapeshellcmd()


Toot4fun

Recommended Posts

I'm trying to create a PHP script that will use mysqldump to backup my databases.  I have the following code:

 

$strCommand = "mysqldump --host=$strHostName --user=$strUserName --password=$strPassword $strDBName | gzip > $strBackupFile";
$strOutput = exec(escapeshellcmd($strCommand) . " 2>&1",$arrOutput,$intReturnValue);
if($intReturnValue != 0){
	etc...

 

However, I get an error that says "mysqldump: Couldn't find table: "|"".  I have to assume it's because of the escaping (\), but if I run it without the escapeshellcmd(), if I have an error, the error is written directly to the screen, not stored in $arrOutput or $intReturnValue.  If I use escapeshellcmd(), $intReturnValue is populated as is my output array $arrOutput.

 

What gives??  Hopefully I have supplied enough information.

 

Thanks!!

Link to comment
Share on other sites

I don't belive mysqldump outputs anything usefull to stdout. Nothing that could be piped to gzip anyway. And in fact, if you look at the man page...

 

mysqldump [options] db_name [tables]

 

The optional argument after the db_name tables. Hence, its getting confused and looking for a table called |

 

You'll need to do this in a few steps.

Link to comment
Share on other sites

If it wasn't for the fact that

 

$strCommand = "mysqldump --host=$strHostName --user=$strUserName --password=$strPassword $strDBName | gzip > $strBackupFile";
$strOutput = system($strCommand, $intReturnValue);

 

dumps and zips the output, I would agree with you.  But if I run the same command using system() as shown above, I end up with a gzip file.  The problem is that if I force an error (bad password), the error is written directly to the screen - $strOutput is empty and $intReturnValue is equal to 0.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.