PDA

View Full Version : کد بکاپ از دیتا بیس



mehdiv
دوشنبه 02 اردیبهشت 1392, 17:08 عصر
با سلام خدمت تمامی دوستان
من این کد رو دارم برای بکاپ گیری از دیتا بیس ب فرمت یو تی اف 8
قبلا کار میکرد اما الان کار نمیکنه میشه یه نگا بندازید ببیند مشکلش چیه


<?php

$db_host = localhost;
$db_name = ;
$db_user =;
$db_pass = ;


// Output the MIME header
header("Expires: Mon, 12 Dec 2001 08:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Pragma: no-cache");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Content-Type: text/x-utf-8");
header("Content-Type: text/x-delimtext");
header("Content-Transfer-Encoding: binary");
// Output the file
echo "# DB Backup Script\r\n";
echo "#\r\n";
echo "# Dump of tables for ".$db_name."\r\n";
echo "# --------------------------------------------------------\r\n";
echo "# DATE : " . gmdate("d-m-Y H:i:s", time()) . " GMT\r\n\r\n";

echo "USE ".$db_name.";\r\n";

$tables_query = "SHOW TABLES FROM `".$db_name."`";
$db = mysql_pconnect($db_host, $db_user, $db_pass);
mysql_select_db($db_name, $db);
$result=mysql_query("set names 'utf8' ",$db);
$result = mysql_query($tables_query, $db);

if(!$result)
{
die("Failed to get list of tables!");
}

while ($row = mysql_fetch_array($result)){
$table_name = $row[mysql_field_name($result, 0)];

// dump table structure
$create_table_query = "SHOW CREATE TABLE `".$table_name."`";
$result2 = mysql_query($create_table_query, $db);
if(!$result2)
{
die("Failed to get table structure for ".$table_name."!");
}
if ($row2 = mysql_fetch_array($result2)){
echo "\r\n\r\n#";
echo "\r\n# Table structure for table '".$table_name."'";
echo "\r\n#\r\n\r\n";

echo "DROP TABLE IF EXISTS `".$table_name."`;\r\n";
echo preg_replace (array("'\n'"), array("\r\n"), $row2['Create Table']).";\r\n";
}

// dump table data
echo "\r\n\r\n\r\n#";
echo "\r\n# Dumping data for table '".$table_name."'";
echo "\r\n#\r\n\r\n";
$select_query = "SELECT * FROM `".$table_name."`";
$result2 = mysql_query($select_query, $db);
if(!$result2)
{
die("Failed to get table data for ".$table_name."!");
}
if ($row2 = mysql_fetch_array($result2)){
$field_names = array();
// Grab the list of field names.
$num_fields = mysql_num_fields($result2);
$table_list = '(`';
for ($j = 0; $j < $num_fields; $j++)
{
$field_names[$j] = mysql_field_name($result2, $j);
$table_list .= (($j > 0) ? '`, `' : '') . $field_names[$j];

}
$table_list .= '`)';

do
{
// Start building the SQL statement.
$schema_insert = "INSERT INTO `".$table_name."` ". $table_list." VALUES(";

// Loop through the rows and fill in data for each column
for ($j = 0; $j < $num_fields; $j++)
{
$schema_insert .= ($j > 0) ? ', ' : '';

if(!isset($row2[$field_names[$j]]))
{
$schema_insert .= 'NULL';
}
elseif ($row2[$field_names[$j]] != '')
{
$schema_insert .= "\"" . mysql_real_escape_string($row2[$field_names[$j]]) . "\"";
}
else
{
$schema_insert .= "\"\"";
}
}

$schema_insert .= ");\r\n";

// Go ahead and send the insert statement
echo $schema_insert;

}
while ($row2 = mysql_fetch_array($result2));
}


}


########### end of process

while ($rowtest= mysql_fetch_array($result)){
$table_name = $row[mysql_field_name($result, 0)];

// dump table structure
$create_table_query = "SHOW CREATE TABLE `".$table_name."`";
$result2 = mysql_query($create_table_query, $db);
echo $result2;
if(!$result2)
{
die("Failed to get table structure for ".$table_name."!");
}
if ($rowtest = mysql_fetch_array($result2)){
echo "\r\n\r\n#";
echo "\r\n# Table structure for table '".$table_name."'";
echo "\r\n#\r\n\r\n";

echo "DROP TABLE IF EXISTS `".$table_name."`;\r\n";
echo preg_replace (array("'\n'"), array("\r\n"), $rowtest['Create Table']).";\r\n";
}

}
?>

و اگه این نشد اگه کسی یه کد داره که دیتابیس رو dump میکنه با فرمت utf-8 لطفا بزاره

mehdiv
دوشنبه 02 اردیبهشت 1392, 18:27 عصر
کسی نبود به من کمک کنه؟