<?
error_reporting(0);

include("../include/common.php");

session_start();

islogin();

$action=addslashes($_GET["action"]);

//备份结构
if($action=="struct")
{
	$file=ROOT."/backup/struct_".md5(time().mt_rand(100,500)).".txt";

	//echo $file;

	$dh=dir(ROOT."/backup/");
	
	//清理旧数据
	
	while($file1=$dh->read())
	{
	if(ereg("^struct[a-z0-9\_]{0,}\.txt$",$file1))
	{
		unlink(ROOT."/backup/".$file1);
	}
	}

	$fp=fopen($file,"w");
	
	$mysql->query("show tables");
	
	while($arr=$mysql->fetcharray())
	{
		$table[]=$arr;
	}
	foreach($table as $arr)
	{
		fwrite($fp,"DROP TABLE IF EXISTS ".$arr[0].";\n");
		$mysql->query("show create table ".$arr[0]);
		$sql=$mysql->fetcharray();
		fwrite($fp,"$sql[1];\n");
		//echo "$arr[0]备份完毕";
	}
	fclose($fp);
	
	$msg="数据库结构,已经备份完毕";
}


//优化
if($action=="youhua")
{
	$table=addslashes($_GET["table"]);
	
	
	$rs=$mysql->query("OPTIMIZE TABLE $table");
	
	if($rs)
	{
		$msg=$table."表优化完毕";
	}
	else
	{
		$msg="优化".$table."表时出现错误";
	}
}

//修复
if($action=="repair")
{
	$table=addslashes($_GET["table"]);
	
	$rs=$mysql->query("REPAIR TABLE $table");
	
	if($rs)
	{
		$msg=$table."表修复完毕";
	}
	else
	{
		$msg="修复".$table."表时出现错误";
	}
}

//全部优化
if($action=="allyouhua")
{
	$mysql->query("show tables");
	while($tb=$mysql->fetcharray())
	{
		$tb1[]=$tb[0];
	}
	foreach($tb1 as $value)
	{
		$mysql->query("OPTIMIZE TABLE $tb[0]");
		
		$msg.="<br>OPTIMIZE TABLE $value 优化完毕";
	}
}

if($action=="allrepair")
{
	$mysql->query("show tables");
	while($tb=$mysql->fetcharray())
	{
		$tb1[]=$tb[0];
	}
	foreach($tb1 as $value)
	{
		$mysql->query("REPAIR TABLE $tb[0]");
		
		$msg.="<br>REPAIR TABLE $value 修复完毕";
	}
}

//分卷存储

if($action=="backup")
{
	  $partsize=1024*1024;
	  
	  $sqldump='';
	  
	  $mysql->query("show tables");
	  
	  //获取table总数存入一个数组
	  
	  while($arr=$mysql->fetcharray())
	  {
		  $table[]=$arr;
	  }
	  //foreach table
	  
	  $arr="";
	  
	  foreach($table as $tab)
	  {
		  //获取表结构
		  $mysql->query("show create table ".$tab[0]);
		  
		  $arr=$mysql->fetcharray();
		  
		  $sqldump.='drop table if exists '.$tab[0].";\n".$arr[1].";\n";
		  
		  
		  //获取表的字段数
		  
		  $numfields=$mysql->numfields("select * from ".$tab[0]);
		  
		  //获取每个表的信息值
		  
		  $mysql->query("select * from ".$tab[0]);
		  
		 //遍历每张表的数据
		  
		  while($array=$mysql->fetcharray())
		 {
			  $comma="";
			  
			  $sqldump.='insert into '.$tab[0].' value (';
			
			  //遍历没列,如果表不为空则insert into
			
			  for($i=0;$i<$numfields;$i++)
			  {
				  
				  //判断字段类型,是否加单引号
				  
				  $type=$mysql->fieldtype("select * from $tab[0]",$i);
				  
				  if(in_array($type,array("string","text","datetime","blob")))
				  {
				  
					  $sqldump.=$comma."'".mysql_escape_string($array[$i])."'";
					  
					  $comma=",";
				  
				  }
				  else
				  {
					   $sqldump.=$comma.mysql_escape_string($array[$i]);
					  
					   $comma=",";
				  }
				  
			  }
			   
			  $sqldump.=");\n";
			  
			 //监测字符串长度判断是否要分卷
			  
			  	if(strlen($sqldump)>$partsize)
	 			{
					$arrsqldump[]=$sqldump;
					$sqldump="";
				}
				
				$sqldump.="\n";  
			
		  }
		 
		
			  
	}
		 if(is_array($arrsqldump))
		  {
			  array_push($arrsqldump,$sqldump);
			  //print_r($arrsqldump);
			  write_file($arrsqldump);
		  }
		 else
		 {
		 //echo $sqldump;
		 	write_file($sqldump);
		 }
		 
		 $msg="已经备份成功";
}

function write_file($sqldump)
{
	$backupdir=ROOT."/backup/";
	if(is_array($sqldump))
	{
		
	    //清除旧数据
		$bk=dir($backupdir);
		while($file=$bk->read())
		{
			if(ereg("^part_[0-9a-z\_]{0,}\.txt$",$file))
			{
				unlink($backupdir.$file);
			}
		}

		$i=1;
		//分卷存储
		foreach($sqldump as $value)
		{
			
			$filename=$backupdir."part_".$i."_".md5(time().mt_rand(3,100)).".txt";
			
			$fp=fopen($filename,"w");
			
			fwrite($fp,$value);
			
			fclose($fp);
			
			$i++;
		}
	}
	
	else
	{
		$bk=dir($backupdir);
		while($file=$bk->read())
		{
			if(ereg("^all_[0-9a-z]{0,}\.txt$",$file))
			{
				unlink($backupdir.$file);
			}
		}
		//不分卷
		$filename=$backupdir."all_".md5(time().mt_rand(3,100)).".txt";
		
		$fp=fopen($filename,"w");
		
		fwrite($fp,$sqldump);
		
		fclose($fp);
		
	}
}


//还原数据

if($action=="huanyuan")
{
	$backupdir=ROOT."backup/";
	$bk=dir($backupdir);
	while($file=$bk->read())
	{
		
		//判断是否有非分卷sql文件
		if(ereg("^all_[0-9a-z]{0,}\.txt$",$file))
		{
			$filename=$backupdir.$file;
		}
		//判断是否有分卷sql文件
		elseif(ereg("^part_[0-9a-z\_]{0,}\.txt$",$file))
		{
			$fenjuan[]=$backupdir.$file;
		}
		
	}
	
	//判断是否有还原文件,并决定用分卷还是整个
	if($filename || $fenjuan)
	{
		//非分卷还原
		
		if($filename && filesize($filename)<1024*1024)
		{
			$str="";
			$str=file_get_contents($filename);
			$arr=explode(";",$str);
			foreach($arr as $value)
			{
				$mysql->query($value);
				
				//echo $value;
			}
			
			$msg="还原完毕";
		}
		
		//分卷还原
		else
		{
			$biaoji="";
			for($i=0;$i<count($fenjuan);$i++)
			{
				//echo substr($fenjuan[$i],-38,1);
				for($j=$i+1;$j<count($fenjuan);$j++)
				{
					//获得文件路径并进行递增排序
					if(substr($fenjuan[$i],-38,1)>substr($fenjuan[$j],-38,1))
					{
						$biaoji=$fenjuan[$i];
						$fenjuan[$i]=$fenjuan[$j];
						$fenjuan[$j]=$biaoji;
					}
				}
			}
			//print_r($fenjuan);
			
			//遍历排序以后的文件数组并获取字符串
			foreach($fenjuan as $value1)
			{
				$str.=file_get_contents($value1);
			}
			
			//对字符串进行explode函数,执行sql语句
			
			$arr=explode(";",$str);
			
			foreach($arr as $value)
			{
				$mysql->query($value);
				
				//echo $value;
			}
			
			$msg="分卷还原完毕";
			
		}
	}
	else
	{
		die("没有可还原的文件");
	}
	
	
}

showmsg($msg,"backup.php","2");
?>