Solved

Use System Function to  execute a mysqldump

Posted on 2008-10-30
9
330 Views
Last Modified: 2013-12-13
I am trying to create a mysqldump in PHP:

----------
$command = "mysqldump -h localhost --user=me --password=mypass --no-create-info --opt --where=\"Id<12\" MyDatabase MyTable |sed -e \"s|INSERT INTO|REPLACE INTO|\" > ".$path."replacedump.sql";
system($command,$error);
-------------

The dump file is not being executed.

I can take the string for $command and put in straight to the command line and it works. Then I thought maybe it was the system() function, so I create system("ls",$error); in PHP and it worked, I got the file list in my browser.

Is there a reason why system() is not working with mysqldump. I should have permission to run it because I can do it on the command line. Does PHP read permissions differently? Or is there something wrong with my mysqldump?

I appreciate any help!
0
Comment
Question by:SaltyDawg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 1

Author Comment

by:SaltyDawg
ID: 22841444
I've also used exec() and passthru() and none worked.
0
 
LVL 26

Expert Comment

by:Umesh
ID: 22841631
This may help you..

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_23025392.html

Also, can you tell me whether safe_mode is enabled in php.ini?

Thanks,
Umesh
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 22841860
Hello Umesh
thanks again for your help

safe_mode and safe_mode_gid are off
and
safe_mode_exec_dir and safe_mode_include_dir have no value
and
sql.safe_mode is off


my error always returns 1
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 26

Expert Comment

by:Umesh
ID: 22842288
Can you please try this way... lets start one-by-one.. first will see whether it dumps or writes any error...

Once you execute below code.. then from command line check whether the file /tmp/mydump.sql is created...


<?php
 
$command = "mysqldump -h localhost --user=root --password=123 --no-create-info --opt --where=\"Id<12\" MyDatabase MyTable 2>&1 > /tmp/mydump.sql";
system($command,$error);
 
?>

Open in new window

0
 
LVL 26

Expert Comment

by:Umesh
ID: 22842411
Just checked below code and it works for me..

PHP details..

$ php -v
PHP 5.0.4 (cli) (built: Mar 17 2006 15:37:24)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.4-dev, Copyright (c) 1998-2004 Zend Technologies


Pls change required info from below...script.. hostname, user,pass, DBname, table etc
<?php
 
$command = "mysqldump -h xx.xx.xx.xx --user=me --password=secret --no-create-info --opt --where=\"id <12\" myDB myTable |sed -e \"s|INSERT INTO|REPLACE INTO|\" 2>&1 > /tmp/mydump.sql";
system($command,$error);
 
?>

Open in new window

0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 22843139
Yes it worked for me to. Apparently from PHP that's only directory I can write to. I can't even write to my my Home dir. I'm guessing from PHP I'm not logged in as myself. Is there a command I can run to log in?
0
 
LVL 26

Accepted Solution

by:
Umesh earned 500 total points
ID: 22843271
Command...not sure will check and let you know...

BTW, you can do onething create a directory under your home or anywahere you want and chmod it to 777

chmod -R 777
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 22843420
Yea I figured out the chmod, thanks
0
 
LVL 1

Author Closing Comment

by:SaltyDawg
ID: 31511676
Thanks again.

But keep an eye out for me again I may have one more thing to do regarding scp to another host with php
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question