PHP备份MYSQL数据库
2012-08-09 09:19:03 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾惧綊鏌熼梻瀵割槮缁惧墽鎳撻—鍐偓锝庝簻椤掋垺銇勯幇顖毿撻柟渚垮妼椤粓宕卞Δ鈧獮濠勭磽閸屾艾鈧懓顫濋妸鈺佺疅缂佸顑欓崥瀣煕椤愵偅绶氱紓鍐╂礋濮婂宕掑▎鎴М濠电姭鍋撻梺顒€绉甸幆鐐哄箹濞n剙濡肩紒鎰殜閺屸€愁吋鎼粹€茬敖婵炴垶鎸哥粔鐢稿Φ閸曨垰鍐€妞ゆ劦婢€濞岊亪姊虹紒妯诲蔼闁稿海鏁诲濠氭晲婢跺﹤宓嗛梺缁樺姈缁佹挳宕戦幘璇叉嵍妞ゆ挻绋戞禍鐐叏濡厧浜鹃悗姘炬嫹

核心提示:<?phprequire(dirname(__FILE__).'/config.php');//备份if(post_get('act', 1)=='bak'){$file_name = $_POST['file'];$bak_connection =
<?php require(dirname(__FILE__).'/config.php'); //备份 if(post_get('act', 1)=='bak'){ $file_name = $_POST['file']; $bak_connection = db::factory('mysql'); $query = 'show tables'; $tables_arr = $bak_connection->get_array($query); $file_content = ''; //遍历所有表 foreach($tables_arr as $value){ $table = $value['Tables_in_'.$current_conf['DBNAME']]; if(!empty($table)){ $query = 'show create table '.$table; $file_content .= "DROP TABLE IF EXISTS `$table`;\r\n"; //表结构 $table_destruct = $bak_connection->get_array($query); $file_content .= str_replace("\n", '', str_replace("\r\n", "\n", $table_destruct[0]['Create Table'])) . ";\r\n\r\n"; //$msg = $table.'表结构备份完毕'; //表数据 $query = 'SELECT * FROM '.$table; $table_data = $bak_connection->get_array($query); foreach($table_data as $key => $value){ $file_content .= "INSERT INTO `$table` VALUES('"; $value_arr = array_values($value); $file_content .= implode("','" , $value_arr); $file_content .= "');\r\n\r\n"; } //$msg .= $table.'表数据备份完毕'; } } $file_content = iconv('GBK' , 'UTF-8' , $file_content); /* $fp = fopen($path , 'w') or die('文件不可写,或不存在'); fputs($fp , $file_content); fclose($fp); unset($fp); */ header("Content-type: text/plain"); header("Accept-Ranges: bytes"); header("Content-Disposition: attachment; filename=".$file_name); header("Cache-Control: must-revalidate, post-check=0, pre-check=0" ); header("Pragma: no-cache" ); header("Expires: 0" ); exit($file_content); } //还原 if(post_get('act', 1)=='restore'){ $sql_file = $_POST['file']; $bak_connection = db::factory('mysql'); $fp = fopen($sql_file , 'r'); while(!feof($fp)){ $line = fgets($fp); if(trim($line)!='' || trim($line)!=null){ $temp_line = iconv('utf-8' , 'gbk' , $line); $result = $bak_connection->execute($temp_line); if($result){ redirect('back.php' , '还原成功'); } redirect('back.php' , '还原失败'); } } }
config.php这个文件是我自己的,主要是用来提供生成MYSQL连接的。
$bak_connection = db::factory('mysql');
更多精彩
赞助商链接