14

I want to create a backup from a database, but I get only a blank file.

include('config.php');

$command = "mysqldump --opt -h ".$_host." -u ".$_user." -p ".$_pass." ".$_db." > test.sql";
exec($command);

echo "<br />".$command;

test.sql is created where the .php file is located.

Edit:

Note! I'm using XAMPP WINDOWS !

Solution:

Because I'm using a Windows Web Server (XAMPP), I needed to specify the path:

$command = 'd:\xampp\mysql\bin\mysqldump --opt -u '.$_user.' -p'.$_pass.' '.$_db.' > test.sql';
  1. I removed the space between the -p and the pass. It looks like: -pMYPASSWORD
  2. Replaced " with '

I think if you are using a Linux based web server, you don't have to specify the path for mysqldump.

Cheers! :-)

2
  • 4
    I think you should remove the space between the -p and the password. Commented Aug 17, 2012 at 10:46
  • 2
    Have you tried echo-ing out the command (to make sure all the variables are passed as expected) or testing it manually with the right input? Commented Aug 17, 2012 at 10:51

8 Answers 8

4

Try this one:

$command = 'd:\xampp\mysql\bin\mysqldump --opt -u '.$_user.' -p'.$_pass.' '.$_db.' > test.sql 2>&1';

-its about permission issue.

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

1 Comment

The 2>&1 at the end helped me at least get an error instead of an empty file.
4

These are the parameters

-uROOT -pPASSWORD --databases DB --result-file=FILE.SQL

1 Comment

for -u, you can have a space.
0

If you look at the manual for exec the output goes to an array that is the second argument. That may be the source of the problem.

Comments

0

You should remove the space between the -p and the password.

--opt isn't needed with recent versions of mysql.

Other than that, your syntax is correct so if it doesn't work with the -p fix, you should check the parameters and the produced command.

Comments

0

Take the variables out of the quotes and remove --opt.

Also make sure that you are having unique file names

$backupfile = $dbname . date("Y-m-d-H-i-s") . '.sql';

$command = "D:\xampp\mysql\bin\mysqldump -u $_user -p$_pass $_db > $backupfile";

system($command);

Comments

0

For those on Macs

I was battling this issue the entire evening today. Silly mistake: one needs to chmod the directory that you are dumping the file to.

I ran this which fixed the issue:

chmod -R 777 your_dump_directory/

Comments

0

Try this:

$DBUSER="user";
$DBPASSWD="password";
$DATABASE="DBname";

$filename = "backup-" . date("d-m-Y") . ".sql";

$command = '"C:\Program Files\MySQL\MySQL Server 5.5\bin\mysqldump.exe" '.$DATABASE ." -u".$DBUSER ." -p".$DBPASSWD." > your_web_site/$filename";
passthru($command);
  • Change the route to the direction of the mysqldump.exe application of your computer.
  • Respect the " " inside the command.

Then force the download of the file:

if (file_exists("your_web_site/".$filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
readfile("your_web_site/".$filename);
exit;
}

Edit: You have to give permissions to the folder where you keep copies.

Comments

0

Please make sure set PATH for MySQL bin folder for WAMP.

Comments

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.