Take Backup of MySQL database using PHP
Hello Friends !!
Generally we take backup of our MySQL database using import/Export option in phpMyAdmin.
But Suppose, you have to take backup every day or every hour, this technique is time consuming. It takes more time.
There is one way to take backup which take less time. We can take backup using PHP code. When you run this code, This code create one backup file automatically which ia easily import in database when you want.
I write that PHP code below. Simply you have to copy this code and paste in you PHP file.
When you run this script, it crates backup file saved at where your this PHP file stored.
Look at this code :
<?php
backup_tables(‘hostaddress’,'dbusername’,'dbpassword’,'dbname’);
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = ‘*’)
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == ‘*’)
{
$tables = array();
$result = mysql_query(‘SHOW TABLES’);
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(‘,’,$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query(‘SELECT * FROM ‘.$table);
$num_fields = mysql_num_fields($result);
$row2 = mysql_fetch_row(mysql_query(‘SHOW CREATE TABLE ‘.$table));
$return.= “\n\n”.$row2[1].”;\n\n”;
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= ‘INSERT INTO ‘.$table.’ VALUES(‘;
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace(“\n”,”\\n”,$row[$j]);
if (isset($row[$j])) { $return.= ‘”‘.$row[$j].’”‘ ; } else { $return.= ‘”"‘; }
if ($j<($num_fields-1)) { $return.= ‘,’; }
}
$return.= “);\n”;
}
}
$return.=”\n\n\n”;
}
//save file
$handle = fopen(‘db-backup-’.time().’-’.(md5(implode(‘,’,$tables))).’.sql’,'w+’);
fwrite($handle,$return);
fclose($handle);
}
?>
I think this post is become very useful to you..
If you like this or any query then please comment on this.
Thanks…
-
Jason
-
Jason
-
admin
-
admin
-
http://www.mingerso.com/ Matth
-
http://www.mingerso.com Matth
-
admin
-
admin
-
http://www.mingerso.com/ Matth
-
http://www.mingerso.com Matth
-
admin
-
admin
-
http://www.faqpal.com/blog/2009/03/21/top-tutorials-week-ending-032109/ Top tutorials week ending 03/21/09 | FAQPAL Blog
-
Chamith Malindasiri
-
Chamith Malindasiri
-
archit
-
Malar selvi
-
Ashokkadali K
-
Didwaniasumit
-
Anonymous
-
Didwaniasumit
-
Didwaniasumit
-
Panjabshelke




