Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql backup using PHP

Status
Not open for further replies.

mrsbean

Technical User
Jul 14, 2004
203
US
Code:
<?php
include("common/dbConnection.php");
//I know I don't need to connect
//but the connection has the variables in it that I need
//below ... seemed like an easy way to get them in 
//without re-inventing ...

$backupFile = "backups/".$dbName . date("Y-m-d-H-i-s") . '.sql';
$command = "mysqldump --opt -h $dbHostname -u $dbUsername -p $dbPassword $dbName  > $backupFile";
system($command);

echo "DOWNLOAD: <a href=\"$backupFile\">$backupFile</a>";


?>

What I wind up with is an empty text file. What am I doing wrong?

MrsBean
 
You cannot have a space after both -u & -p. Your command should read -u$dbUsername -p$dbPassword.

--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
would recommend something like this, which forces a complete backup and also sends it down the tube to the browser in one dollop.
you may need to set a longer timeout in some cases.
Code:
<?
header('Content-type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-Disposition: attachment; filename="sqldump.sql"');
$command = "mysqldump --all-databases --lock-all-tables --host=$dbHostName --user=$dbUserName --password=$dbPassword --opt";
$handle = popen($command,"r");
fpassthru($handle);
?>
 
I've been using auto mysql backup. Its been working beautifully for me. Check it out here: It requires setting up a cron task, but if you have access to cPanel its super simple. You can save the backups to the server, or have them emailed to you. Let me know if you need help with it.
 
Correct me if I'm wrong, but the --opt means add the "if table exists, drop" clause.

Mark
 
and a whole bunch of other commonly selected options.
 
Thanks Everybody. All of your suggestions were most helpful.

MrsBean
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top