2


I am having the command line to backup the db and wnat to run through the browser.How can I do it.Because when I execute the below code with the inputs it wont give me the output.Why?How can I achieve the db backup if it is not work out.

$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile"; 
print system($command);

3 Answers 3

2

It sounds like what you want is passthru()

According to the documentation, it will dump the output of the command directly back to the browser.

$ret_val = null;
header("Content-type: application/gzip");
passthru("mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip", $ret_code);
# the extra equals sign below makes sure $ret_val isn't null
if($ret_val !== 0) 
   echo "Failboat";

That being said, this sounds like a really brittle way to do backups. You'd be better off saving the file to disk first and downloading seperately. IF you really want to get hacky with it though, try using tee to write the file to disk while streaming it back to the browser (warning, that's actually an even worse idea)

Sign up to request clarification or add additional context in comments.

3 Comments

I hope his backup page is really well secured, I wouldn't like some random people to get access to my database dumps directly...
@RageZ: agreed. This is probably one of those times where what was asked for isn't really what's wanted
It also return the empty zip.I didnt get the result
1

I would say one of this functions system or exec. If you need the ouput exec would be better

system($command)

Just for your reference the function signature from the doc

string exec ( string $command [, array &$output [, int &$return_var ]] )

and a quick snippet

exec($command, $output, $ret_var);
echo "return code: {$ret_var}" . PHP_EOL;
echo "output ------" . PHP_EOL;
foreach($output as $line){
    print $line . PHP_EOL;
}

10 Comments

I tried that But I cant get the result.I updated my coding.It wont print anything
@vinothkumar: what do you mean by cannot get the result?
The expected output is the zip file of database.That I didnt get.Thats why i put I cant(I didnt get the expected result).
@vinothkumar: did you check the return value if it is not 0. I suspect your webserver doesn't have permissions to write the zip file.
But when I export through phpmyadmin it gives the zip file.
|
0

Eigther use mysqladmin (english version) or you may use an ajax call (over javascript) in one of your own pages.

2 Comments

Is there any way to make it simple.Not want to install and use all the files from mysqladmin
No, there is no partial version. Myasqladmin is a free GUI for all possible DB-issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.