Mangina wrote:
Hello all.
Can anyone see why this snippet of code isnt dumping the databases using
mysqldump ?
All the credentials are correct (changed for the purpose of this post).
I think I found a couple of things. See below. =)
$host = "localhost";
$user = "root";
$pass = "password";
$database = "database";
$dbname = array ("1","2","3","4","5");
$savepath = "c:/inetpub/sqlbackup";
$date = date("mdy-hia");
$link = mysql_connect($host, $user, $pass) or die("Could not connect");
mysql_select_db($database) or die("Could not select database");
Above, you're specifying the database names... so unless you're running
a query to get the db names, the two lines above aren't necessary. If
you want to dump all the databases, it'd be easier to use the
"--all-databases" flag with mysqldump than it would be to ask the db for
all the names first.
for ($i=0;$i<sizeof($dbname);$i++)
{
$filename = "$savepath/$dbname[$i]-$date.sql";
passthru("c:/mysql/bin/mysqldump --opt -h$host -u$user -p$pass $dbname[$i]
$filename");
}
mysql_free_result($result);
mysql_close($link);
Same basic comment as above. Those two lines probably aren't necessary.
I'm assuming that you're on a windows machine with IIS. =) Why else
would one have a "C:" in their path name... and want to put stuff in
"inetpub?. ;)
Your mileage may vary... but the following worked for me. Please note
that this *won't work* if safe mode is on in your php.ini file. Also,
my machine (Apache/XP) choked on the forward slashes in the path to
mysqldump, so I put in escaped backslashes. T'would be a different path
on *nix.
<?php
$host = "localhost";
$user = "user";
$pass = "password";
$dbnames = array ("db1","db2");
$savepath = "c:/inetpub/sqlbackup";
$date = date("mdy-hia");
foreach($dbnames as $dbname)
{
$filename = $savepath.$dbname."-".$date.".sql";
$execCmd = "c:\\mysql\\bin\\mysqldump.exe -h$host -u$user -p$pass
$dbname >> $filename";
exec($execCmd);
}
?>
Regards,
- Dan
http://blog.dantripp.com/