<?php
	session_start(); 
	require_once('inc/conn.php');
	require_once('inc/conn_var.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	require_once('inc/check.php');
	$opendb=new mydb();
	require_once('inc/char_set.php');

	echo "<meta http-equiv='Content-type' content='text/html; charset=utf-8'>";
	echo "<link href=\"css/admin.css\" rel=\"stylesheet\" type=\"text/css\" />";
	
	$conn=@mysql_connect($host,$username,$password) or die ("can't connect DBS");
	$result= mysql_query("SHOW TABLES FROM $dbname");
	$dumpstr="";
	$echoinfo="";
	$j=0;
	while($table_rows=mysql_fetch_row($result))
	{
		$tablename=$table_rows[0];
		$dumpstradd="";
		$fields=mysql_list_fields($dbname,$tablename,$conn);
		$columns=mysql_num_fields($fields);
		$insertstr="insert into `".$tablename."` (";
		$dumpstr.="create table `".$tablename."` (";
		$echoinfo.="备份 ".$tablename." ok!<br/>";
		for ($i = 0; $i < $columns; $i++) 
		{
			$fieldname=mysql_field_name($fields, $i);
			$flag=str_replace("not_null","NOT NULL",mysql_field_flags($fields, $i));	//change not null
			if(strpos($flag,"primary_key"))
			{
				$flag=str_replace("primary_key","",$flag); //clear the primary_key
				$dumpstradd=",PRIMARY KEY  (`".$fieldname."`)";	// set PRIMARY KEY 
			}
			$fieldtype=mysql_field_type($fields, $i);
			$fieldtype=str_replace("string","varchar",$fieldtype);	//change not null
			//echo "==".$fieldtype."==";
			if($fieldtype=="date")
			{
				$fieldtype2=$fieldtype;
				$flag=str_replace("binary","",$flag); 
			}
			else
			{
				if($fieldtype=="blob")
				{
					$fieldtype2="text";
					$flag=str_replace("blob","",$flag); //clear the primary_key
				}
				else
				{
					$fieldtype2=$fieldtype."(".mysql_field_len($fields, $i).")";
				}
			}
			$dumpstr.="`".$fieldname."` ".$fieldtype2." ".$flag. ",";
			$insertstr.="`".mysql_field_name($fields, $i)."`,";
			
		}
		$dumpstr=substr($dumpstr,0,(strlen($dumpstr)-1));	//delete last ,
		$insertstr=substr($insertstr,0,(strlen($insertstr)-1));	//delete last ,

		//$insertstr.="\r\n<br>";
		$dumpstr.=$dumpstradd." ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;\r\n<br>$insertstr";
		$dumpdata_sql="select * from $tablename";
		$dump_query=$opendb->query($dumpdata_sql);
		$insertvalue=" value";
		$l=0;
		
		while($dump_row=mysql_fetch_row($dump_query))
		{
			$insertvalue.=" (";
			$nums=count($dump_row);
			for($k=0;$k<$nums;$k++)
			{
				switch($dump_row[$k])
				{
					
					case "":
					$insertvalue.=" NULL ,";
					break;
					
					default:
					if(is_string($dump_row[$k]))
					{
						$replacevalue=str_replace("'","\'",$dump_row[$k]);	// replace ' with \'
						$insertvalue.="'".$replacevalue."',";
					}
					else
					{$insertvalue.=$dump_row[$k].",";}
				}
			}
			$insertvalue=substr($insertvalue,0,(strlen($insertvalue)-1));	//delete last ,
			$insertvalue.="),\r\n<br>";
			$l++;
		}
		$insertvalue=substr($insertvalue,0,(strlen($insertvalue)-7)).";\r\n<br>";	//delete last ,
		if(strpos($insertvalue,"alu;")&&$l==0)
		{$insertvalue=";\r\n<br>";}
		$dumpstr.=")\r\n<br>$insertvalue";
		//dumpdata="select * from $tablename into outfile 'aa$j.sql'";
		//dump_query=$opendb->query($dumpdata);
		$j++;
	}
	$dumpstr=str_replace("<br>","",$dumpstr); //clear <br>
	$dumplen=strlen($dumpstr);
	$filename=$backupSQLFile;
	writegeneralfile($filename,$dumpstr);  //write the backup sql file
	echo $echoinfo;
	echo "<br/>备份数据库内容".$dumplen."字节。";

	mysql_free_result($result);
	
//	$resule=shell_exec("mysqldump -opt test > backup-test.sql");
//	if($resule)
//	{echo "数据库备份成功";}
?>
