www.gusucode.com > 搜一次CMS电影程序 PHP 1.5源码程序 > admin/tools/admin_back.php

    <?php
/*
'**************************************************************************************************
' 软件名称: 搜一次 Content Management System
' 版本编号: Version 1.X
' 官方网站: http://www.syccms.com
' 官方论坛:http://bbs.syccms.com
' 版权所有: 搜一次开发团队    无痕(QQ:512591)
' 法律顾问: 杭州市元茂律师事务所 严飞律师
' 郑重声明:
'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息;
'    2、搜一次网络事业部保留此软件的法律追究权利
'**************************************************************************************************
*/
include "../../include/conn.php";
include "../../include/function.php";
include "../admin_version.php";
include "../admin_loginstate.php";
set_time_limit(0);
@ini_set('memory_limit', '-1');
?>
<?php
$action=SafeRequest("action","get");
if($action=="seodata"){
	echo("<div style=' width:100%; background: #F9FBFD; border-top:4px solid #DEEFFA; border-bottom:1px solid #DEEFFA;color:#008EBD; font-size:10pt; height:30px; line-height:30px;font-weight:bold; margin:0 auto;'>&nbsp;数据表优化中...</div>");
	$tabels=RequestBox("tabels","post");
	$tabelarr=explode(",",$tabels);
	for($i=0;$i<count($tabelarr);$i++){
		$sql="OPTIMIZE TABLE `$tabelarr[$i]`";
		  if($db->query($sql)){
			   echo "&nbsp;&nbsp;<font style=\"font-size:10pt;\">数据表:<font color=red>$tabelarr[$i]</font>优化完毕...</font><br/>";  
			  }
		 else{
				echo "&nbsp;&nbsp;<font style=\"font-size:10pt;\">数据表:<font color=red>$tabelarr[$i]</font>优化失败...</font><br/>";  
			 }	  		
		}
	unset($tabelarr);	
	die("&nbsp;&nbsp;<font style=\"font-size:10pt;\"><b>恭喜你,数据表已经全部优化完毕...</b></font>"); 
}
elseif($action=="repair"){
	echo("<div style=' width:100%; background: #F9FBFD; border-top:4px solid #DEEFFA; border-bottom:1px solid #DEEFFA;color:#008EBD; font-size:10pt; height:30px; line-height:30px;font-weight:bold; margin:0 auto;'>&nbsp;数据表修复中...</div>");
	$tabels=RequestBox("tabels","post");
	$tabelarr=explode(",",$tabels);
	for($i=0;$i<count($tabelarr);$i++){
       $sql="REPAIR TABLE `$tabelarr[$i]`";
		if($db->query($sql)){
			 echo "&nbsp;<font style=\"font-size:10pt;\">数据表:<font color=red>$tabelarr[$i]</font>修复完毕...</font><br/>";  
			 }
	    else{
			echo "&nbsp;<font style=\"font-size:10pt;\">数据表:<font color=red>$tabelarr[$i]</font>修复失败...</font><br/>";  
			 }	  
		
		}
	  unset($tabelarr[$i]);
	 die("&nbsp;<font style=\"font-size:10pt;\"><b>恭喜你,数据表已经全部修复完毕...</b></font>"); 	
	}
elseif($action=="backup"){
	echo("<div style=' width:99%; background: #F9FBFD; border-top:4px solid #DEEFFA; border-bottom:1px solid #DEEFFA;color:#008EBD; font-size:10pt; height:30px; line-height:30px;font-weight:bold; margin:0 auto;'>&nbsp;正在备份数据...</div>");
	//删除旧文件
	deloldbk();
	$size=SafeRequest("sizes","post");
	$result=$db->list_tables(S_Sqldbname);
	if($result){
	  $bkfile = "../backup/tables_struct_".substr(md5(time().mt_rand(1000,5000)),0,16).".txt";
	  $fp = fopen($bkfile,"w");
	   while($row= mysql_fetch_row($result))   
	   {   
             fwrite($fp,"DROP TABLE IF EXISTS `$row[0]`;\r\n\r\n");
			 $sql="SHOW CREATE TABLE `$row[0]`";
			  $tableStruct=$db->getAll($sql);
			  if($tableStruct){
				  //去除AUTO_INCREMENT
				  $tableStruct[0]['Create Table']= eregi_replace("AUTO_INCREMENT=([0-9]{1,})[ \r\n\t]{1,}","",$tableStruct[0]['Create Table']);
				   fwrite($fp,''.$tableStruct[0]['Create Table'].";\r\n\r\n");
				   echo "&nbsp;<font style=\"font-size:10pt;\">数据表:<font color=red>$row[0]</font>结构备份成功...</font><br/>";  
				  }
			 else{
				  	echo "&nbsp;<font style=\"font-size:10pt;\">数据表:<font color=red>$row[0]</font>结构备份失败...</font><br/>";  
				 }	  
			  
			  
          } 
		  fclose($fp);
		  echo "&nbsp;<font style=\"font-size:10pt;\"><b>数据表结构已全部备份完毕...</b></font>";
		  die("&nbsp;&nbsp;<img src=../images/loading2.gif><font style=\"font-size:10pt;\"><b>正在开始备份数据,请稍等...</b></font><script language='javascript'>setTimeout('ReadGo();',".(S_StopTime*1000).");function ReadGo(){location.href='?action=bkdata&size=".$size."';}</script>");
		  mysql_free_result($result);  
		}	
	
	}
elseif($action=="bkdata"){
	echo("<div style=' width:100%; background: #F9FBFD; border-top:4px solid #DEEFFA; border-bottom:1px solid #DEEFFA;color:#008EBD; font-size:10pt; height:30px; line-height:30px;font-weight:bold; margin:0 auto;'>&nbsp;正在备份数据...</div>");

	$size=SafeRequest("size","get");
	if(!IsNum($size)) $size=1024;
	$bakStr='';
	$tabelresult=$db->list_tables(S_Sqldbname);
	if($tabelresult){
		
	   while($tabelrow= mysql_fetch_row($tabelresult))   
	   { 
	   $intable = "INSERT INTO `$tabelrow[0]` VALUES(";
										 
	    $fieldresult=$db->list_fields(S_Sqldbname,$tabelrow[0]);
		$i=0;
		if($fieldresult){
			//分析表里的字段信息
	  		 while($fieldrow=mysql_fetch_field($fieldresult)){
		 		 		$fs[$i] = trim($fieldrow->name);
						$i++;
				}
				$fsd=$i-1;
				
				////读取表的内容
				$sql="select * from `$tabelrow[0]`";
				$result=$db->getAll($sql);
				
				
				for($j=0;$j<count($result);$j++){
					$line = $intable;
					for($k=0;$k<=$fsd;$k++){
						if($k < $fsd){
							$line.="'".mysql_escape_string($result[$j][$fs[$k]])."',";
							}
						else{
							$line.="'".mysql_escape_string($result[$j][$fs[$k]])."');\r\n";
							}							
						}
						$bakStr.=$line;						
							if(strlen($bakStr)>$size*1024){
								$bkfile = "../backup/tables_datas_".substr(md5(time().mt_rand(1000,5000)),0,16).".txt";
								$fp = fopen($bkfile,"w");
								fwrite($fp,$bakStr);
								fclose($fp);
								echo $bkfile."<br/>";
								$bakStr='';
								}
						}
				
			}	
	  	
		}
		if(!empty($bakStr)&&strlen($bakStr)<$size*1024){
			  $bkfile = "../backup/tables_datas_".substr(md5(time().mt_rand(1000,5000)),0,16).".txt";
			  $fp = fopen($bkfile,"w");
			  fwrite($fp,$bakStr);
			  echo "&nbsp;<font style=\"font-size:10pt;\">".$bkfile."</font><br/>";
			  die("&nbsp;<font style=\"font-size:10pt;\"><b>恭喜你,数据库备份成功...</b></font>");
			  fclose($fp);							
			}		
		}

	}	
elseif($action=="restore"){
	echo("<div style=' width:100%; background: #F9FBFD; border-top:4px solid #DEEFFA; border-bottom:1px solid #DEEFFA;color:#008EBD; font-size:10pt; height:30px; line-height:30px;font-weight:bold; margin:0 auto;'>&nbsp;开始还原数据...</div>");

	$id=SafeRequest("bkid","post");
	if($id!=1){
		echo("&nbsp;<font color=red></b>对不起,没有备份文件,请先备份数据</b></font>");
		}
	else{
		$d='../backup';
		if (is_dir($d)) {
			$dh=opendir($d);//打开目录
			//列出目录中的所有文件并去掉 . 和 ..
			 while (false !== ( $file = readdir ($dh))) {
				if($file!="." && $file!="..") { 
					$fullpath=$d."/".$file;
					//还原表结构
					if(!is_dir($fullpath)&& stristr($fullpath,"tables_struct_")) {
						$tabel_stru=file_get_contents($fullpath);
						$tabelarr=explode(";",$tabel_stru);
							for($i=0;$i<count($tabelarr)-1;$i++){
							  $db->query($tabelarr[$i]);					  							 
							  }
							 echo "<br/>&nbsp;&nbsp;<font color=#00FF00 style=\"font-size:10pt;\">数据表结构已还原成功...</font>";
							 die("&nbsp;<img src=../images/loading2.gif><font style=\"font-size:10pt;\">正在开始恢复数据,请稍等...</font><script language='javascript'>setTimeout('ReadGo();',".(S_StopTime*1000).");function ReadGo(){location.href='?action=restoredata';}</script>");
						 } 
					}
				}
			 closedir($dh);
			}
		}	
		
	}
elseif($action=="restoredata"){
	echo("<div style=' width:100%; background: #F9FBFD; border-top:4px solid #DEEFFA; border-bottom:1px solid #DEEFFA;color:#008EBD; font-size:10pt; height:30px; line-height:30px;font-weight:bold; margin:0 auto;'>&nbsp;开始还原数据...</div>");	
	$d='../backup';
	if (is_dir($d)) {
	  	$dh=opendir($d);//打开目录
  		//列出目录中的所有文件并去掉 . 和 ..
 		 while (false !== ( $file = readdir ($dh))) {
   			if($file!="." && $file!="..") { 
      			$fullpath=$d."/".$file;
      			if(!is_dir($fullpath) && stristr($fullpath,"tables_datas_") ) {
         		 	//数据列表
					$filearr[]=$fullpath;
     				 } 
				}
   		 	}
		for($i=0;$i<count($filearr);$i++){
			$tabel_datas=file_get_contents(trim($filearr[$i]));
			//$db->query($tabel_datas);
			 $dataarr=explode("\n",$tabel_datas);
			  for($j=0;$j<count($dataarr);$j++){
				  $db->query($dataarr[$j]);
				  }
				echo "&nbsp;<font style=\"font-size:10pt;\">已经还原<font color=red>".$j."</font>条数据...</font><br/>";	  
			  
			}
			die("&nbsp;<font style=\"font-size:10pt;\"><b>数据库已全部数据还原成功...</b></font>");	
   		 closedir($dh);
		}
	
	}
elseif($action=="singelseo"){
	$tabel=SafeRequest("tabel","get");
     $sql="OPTIMIZE TABLE `$tabel`";
		if($db->query($sql)){
			 die("&nbsp;<font style=\"font-size:10pt;\">数据表:<font color=red>$tabel</font>优化完毕...</font><br/><script>window.location='admin_back.php?types=condb'</script>");  
			 }
	
	}
elseif($action=="singelrepair"){
	$tabel=SafeRequest("tabel","get");
     $sql="REPAIR TABLE `$tabel`";
		if($db->query($sql)){
			 die("&nbsp;<font style=\"font-size:10pt;\">数据表:<font color=red>$tabel</font>修复完毕...</font><br/><script>window.location='admin_back.php?types=condb'</script>");  
			 }
	
	}	
	
function deloldbk(){
	$d='../backup';
	if (is_dir($d)) {
	  	$dh=opendir($d);//打开目录
  		//列出目录中的所有文件并去掉 . 和 ..
 		 while (false !== ( $file = readdir ($dh))) {
   			if($file!="." && $file!="..") { 
      			$fullpath=$d."/".$file;
      			if(!is_dir($fullpath) && $file!="log.txt") {
         		 	unlink($fullpath);//删除目录中的所有文件
     				 } 
				}
   		 	}
   		 closedir($dh);
		}
	}
	
function showbkfile(){
	$d='../backup';
	if (is_dir($d)) {
	  	$dh=opendir($d);//打开目录
  		//列出目录中的所有文件并去掉 . 和 ..
 		 while (false !== ( $file = readdir ($dh))) {
   			if($file!="." && $file!="..") { 
      			$fullpath=$d."/".$file;
      			if(!is_dir($fullpath) && stristr($fullpath,"tables_struct_")) {
         		 	echo '<input type="checkbox" class="checkbox" value="1" checked="checked" name="bkid">'.$fullpath;
     				 } 
				}
   		 	}
   		 closedir($dh);
		}	
	}	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>数据库备份</title>
<link href="../images/alerts.css" rel="stylesheet" type="text/css" media="screen" />
<link  href="../images/style.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script type="text/javascript" language="javascript">var tentpath='<?php echo S_SitePath?>';</script>
<script src="../js/jquery.js"></script>
<script src="../js/alerts.js"></script>
<script language="javascript">
$(document).ready(function(){
	$("#seodata").click(function(){
		window.form1.action="?action=seodata";
		form1.submit();
		//window.location='?action=seodata';						 
		})					   

	$("#repairdata").click(function(){
		window.form1.action="?action=repair";
		form1.submit();
		//window.location='?action=repair';						 
		})	

   })

</script>
</head>

<body>
<div class="r_main">
	<div class="r_content">
		<div class="r_content_1">      
 <form method="post" action="?action=backup" name="form1">
  <?php
 	$types=SafeRequest("types","get");
	if($types=="condb"){
 ?> 
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" Class="tb_style" >
    <tr bgcolor="#FFFFFF">
      <td height="30" class="td_title" colspan="8">&nbsp;数据库管理</b></td>
    </tr>
    <tr>
       <td width="5%" height="30" align="center" class="td_border">选 择</td>
       <td width="15%" align="center" class="td_border">表 名</td>
       <td width="15%" align="center" class="td_border">记 录</td>
       <td width="15%" align="center" class="td_border">操 作</td>
       <td width="5%" height="30" align="center" class="td_border">选 择</td>
       <td width="15%" align="center" class="td_border">表 名</td>
       <td width="15%" align="center" class="td_border">记 录</td>
       <td width="15%" align="center" class="td_border">选 项</td>       
    </tr>
    <tr>
<?php
	$i=0;
	$result=$db->list_tables(S_Sqldbname);
	if($result){
		while($row=mysql_fetch_row($result)){
			$i=$i+1;
			echo "<td width=\"5%\" height=\"30\" align=\"center\" class=\"td_border\"><input type=\"checkbox\" class=\"checkbox\" checked=\"checked\" value=\"$row[0]\" name=\"tabels[]\"></td>
       <td width=\"15%\" align=\"center\" class=\"td_border\">$row[0]</td>
       <td width=\"15%\" align=\"center\" class=\"td_border\"><span id=\"num$row[0]\">读取中..<script>$.get('../js/ajaxfun.php?action=getrenum&tabel=".$row[0]."',function(R){
																																										$('#num".$row[0]."').html(R);																																																														})</script></td>
       <td width=\"15%\" align=\"center\" class=\"td_border\"><a onclick=\"window.location='?action=singelseo&tabel=$row[0]'\">优化</a>&nbsp;&nbsp;&nbsp;<a onclick=\"window.location='?action=singelrepair&tabel=$row[0]'\">修复</a></td>";
	   		if($i==2){
				echo "</tr>\r\n<tr>";
				$i=0;
				}	
			}
		}
	mysql_free_result($result);	
?>    
    </tr>
   <tr>
   	 <td colspan="8">
   	 &nbsp;  <input type="button" value="优化数据库" class="rb1" id="seodata">
   	   &nbsp; <input type="button" value="修复数据库" class="rb1" id="repairdata"></td></tr> 
</table>   
 <?php
	}
	else{
?>            
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" Class="tb_style" >
   <tr bgcolor="#FFFFFF">
      <td height="30" class="td_title" >&nbsp;数据库备份</td>
    </tr>
    <tr bgcolor="#FFFFFF">
      <td  height="30" class="td_border">&nbsp;&nbsp;当前数据库版本:<?php echo function_exists('mysql_close')?"<font color=#00FF00>√</font>".' / '.mysql_get_client_info() :"<font color=red>×</font>"?>&nbsp;      </td>
    </tr>
    <tr>
    	<td  height="30" class="td_border">
         &nbsp;&nbsp;数据分卷大小:
         <input type="text" size="5" name="sizes" value="1024">
         &nbsp;KB&nbsp;
        &nbsp;&nbsp; <input type="checkbox" value="1" checked="checked" class="checkbox">
         备份表结构信息
        &nbsp;&nbsp;&nbsp; <input name="submit" type="submit" class="rb1" value="开始备份数据库" /></td>
    </tr>
</table>

  </form>
<form method="post" action="?action=restore">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" Class="tb_style" >    
<tr bgcolor="#FFFFFF">
      <td height="30" class="td_title" >&nbsp;还原数据库</td>
    </tr>
    <tr>
    	<td  height="30" class="td_border">
          &nbsp;&nbsp;<?php echo showbkfile()?>
         </td>
    </tr>  
     <tr>
    	<td  height="30" class="td_border">
          &nbsp;&nbsp;<input type="submit" value="开始还原数据" class="rb1" id="btn_restore">
         </td>
    </tr>  
 </table>
 </form> 
 <?php
	}
?>  
     		</div>
        </div>
     </div>       
<?php include "../admin_bottom.php"; ?>
</body>
</html>