Simply to export database in mysql and stored in your particular path. See the following code and run in your system with your db details. After run successfully set the file in cron job. so you get the db backup daily or weekly or monthly to store automatically and take from particular path. cheers :)
<?php
$db_host="localhost"; //your host name
$db_user="root"; //your db user name
$db_pass="root"; //your db password
$db_name="test_db"; //your db name
$tables="*"; // all tables to select as *
db_backup($db_host,$db_user,$db_pass,$db_name,$tables);
function db_backup($db_host,$db_user,$db_pass,$db_name,$tables = '*')
{
$con= mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name,$con);
//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);
$return.= 'DROP TABLE IF EXISTS '.$table.';';
$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
$filename='db-backup-'.time().'.sql';
$handle = fopen($filename,'w+');
fwrite($handle,$return);
fclose($handle);
}
?>
<?php
$db_host="localhost"; //your host name
$db_user="root"; //your db user name
$db_pass="root"; //your db password
$db_name="test_db"; //your db name
$tables="*"; // all tables to select as *
db_backup($db_host,$db_user,$db_pass,$db_name,$tables);
function db_backup($db_host,$db_user,$db_pass,$db_name,$tables = '*')
{
$con= mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db_name,$con);
//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);
$return.= 'DROP TABLE IF EXISTS '.$table.';';
$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
$filename='db-backup-'.time().'.sql';
$handle = fopen($filename,'w+');
fwrite($handle,$return);
fclose($handle);
}
?>
No comments:
Post a Comment