当前位置:Gxlcms > 数据库问题 > php备份mysql数据库

php备份mysql数据库

时间:2021-07-01 10:21:17 帮助过:2人阅读

/*程序功能:mysql数据库备份功能*/ ini_set(‘max_execution_time‘,‘0‘); ini_set(‘memory_limit‘,‘1024M‘);//加上这两行,可以备份大数据,昨晚测了一下公司的一个600M的表,很快就可以备份成功 $code_type = ‘gbk‘; header("Content-type:text/html;charset=$code_type"); header("Content-disposition: filename=backup.sql");//所保存的文件名 header("Content-type: application/octetstream"); header("Pragma: no-cache"); header("Expires: 0"); $conn = mysql_connect("localhost","root","root"); $db = mysql_select_db($dbname,$conn); mysql_query("set names $code_type"); $dbname="test";//数据库名 $flag = ‘1‘;//1:全部备份,2:只备份表结构,3:只备份数据 echo back_dataBase($dbname,$conn,$flag);//备份数据库 //开始备份 function back_database($dbname,$conn,$flag=‘1‘){ $crlf="\r\n"; $str = ‘‘; $now = date(‘Y年m月d日H点i分s秒‘,time()); $str .= "$crlf--$crlf-- 数据库: `$dbname`$crlf--$crlf-- 导出日期: `$now`$crlf--$crlf"; $str .= "$crlf--$crlf-- 作者: ****$crlf--$crlf-- QQ: 1019822077$crlf--$crlf"; $str .= "-- blog: http://hi.baidu.com/woaidelphi/blog$crlf--$crlf"; $str .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";$crlf"; $tables = mysql_list_tables($dbname,$conn); $num_tables = mysql_numrows($tables);//表的总数 $i = 0; while($i<$num_tables){//循环所有的表 $table = mysql_tablename($tables,$i); //备份表结构 if($flag==‘1‘ or $flag==‘2‘){ $query = mysql_query("SHOW CREATE TABLE $table"); $row = mysql_fetch_row($query); $str .= "-- --------------------------------------------------------$crlf$crlf"; $str .= "--$crlf-- 表的结构 `$table` $crlf--$crlf"; $str .= $row[1].";$crlf--$crlf"; } //备份表内容 if($flag==‘1‘ or $flag==‘3‘){ $str .= "--$crlf-- 导出表中的数据 `$table` $crlf--$crlf"; $str .= get_table_content($dbname,$table); $str .= "$crlf$crlf"; } $i++; } return $str; } //得到表中的记录sql function get_table_content($dbname,$table){ $crlf = "\r\n"; $schema_create = ""; $temp = ""; $result = mysql_db_query($dbname,"SELECT * FROM $table"); $i = 0; while($row = mysql_fetch_row($result)){ $schema_insert = "INSERT INTO $table VALUES ("; for($j=0; $j<mysql_num_fields($result);$j++){ if(!isset($row[$j])) $schema_insert .= " NULL,"; elseif($row[$j] != "") $schema_insert .= " ‘".addslashes($row[$j])."‘,"; else $schema_insert .= " ‘‘,"; } $schema_insert = ereg_replace(",$", "", $schema_insert); $schema_insert .= ");$crlf"; $temp = $temp.$schema_insert ; $i++; } return $temp; }

 

php备份mysql数据库

标签:

人气教程排行