一个比较好用的php备份mysql数据库 不指定

jed , 2007-8-27 17:30 , 代码编程 , 评论(2) , 阅读(5081) , Via 本站原创 | |

<?
  include "inc/admincheckLogin.php";
  include "../inc/config.php";
  $PWARRAY=mysql_fetch_array(mysql_query("SELECT * FROM shm_siteadmin WHERE user='".$power_user."'"));
  $name=$PWARRAY[name];$right=$PWARRAY[adright];$state=$PWARRAY[state];
  function get_image_size($pix_path,$w,$h){
  $size=getimagesize($pix_path);
  $ow=$size[1];
  $oh=$size[0];
  $w=($oh>$h and $ow<=$w and $oh>$ow)?($ow*$h/$oh):$w;
  $h=($oh<=$h and $ow>$w and $oh<$ow)?($oh*$w/$ow):$h;
  $w=($oh>$h and $ow>$w and $oh>=$ow)?($ow*$h/$oh):$w;
  $h=($oh>$h and $ow>$w and $oh<=$ow)?($oh*$w/$ow):$h;
  $b_size[1]=$w;
  $b_size[0]=$h;
  if($oh<=$h and $ow<=$w){
     $b_size[1]=$ow;
    $b_size[0]=$oh;
  }
  return $b_size;
}
DEFINE ("DEBUG", 0);
?>
<?
function create_table_sql_string($tablename){
  $sql_string = "CREATE TABLE $tablename";
  $result = mysql_query("DESCRIBE $tablename");
  if (DEBUG) echo "field_info\n\n";
  $field_string = "";
  while ($field_info = mysql_fetch_array($result)) {
    $field_name = $field_info[0];
    $field_type = $field_info[1];
    $field_not_null = ($field_info[2] == "YES") ? "" : " NOT NULL";
    $field_default = ($field_info[4] == NULL) ? "" : sprintf(" default '%s'", $field_info[4]);;
    $field_auto_increment = ($field_info[5] == NULL) ? "" : sprintf(" %s", $field_info[5]);
    
    $field_string = sprintf("%s, %s %s%s%s%s", $field_string, $field_name, $field_type, $field_not_null, $field_auto_increment, $field_default);
  }
  
  $result = mysql_query("SHOW INDEX FROM $tablename");
  if (DEBUG) echo "\nindex_info\n\n";
  $index_string = "";
  while ($index_info = mysql_fetch_array($result)) {
    
    $index_name = $index_info[2];
    $index_unique = $index_info[1];
    $index_field_name = $index_info[4];
    $index_type = $index_info[10];
    
    if ($index_name == "PRIMARY") $index_name = "PRIMARY KEY";
    if ($index_unique == "1" && $index_type != "FULLTEXT") $index_name = sprintf("KEY %s", $index_name);
    if ($index_unique == "0" && $index_name != "PRIMARY KEY") $index_name = sprintf("UNIQUE KEY %s", $index_name);
    if ($index_type == "FULLTEXT") $index_name = sprintf("FULLTEXT KEY %s", $index_name);
    $index_string = sprintf("%s, %s (%s)", $index_string, $index_name, $index_field_name);
  }
  
  $result = mysql_query("SHOW TABLE STATUS");
  if (DEBUG) echo "\nstatus_info\n\n";
  while ($status_info = mysql_fetch_array($result)) {
    for ($i = 0; $i < count($status_info); $i++) {
      if (DEBUG) echo "$i: $status_info[$i]\n";
      if ($status_info[0] == $tablename) $table_type = sprintf("TYPE=%s", $status_info[1]);
    }
  }
  $field_string = substr($field_string, 2);
  $field_string = sprintf("%s%s", $field_string, $index_string);
  $field_string = sprintf("(%s)", $field_string);
  $sql_string = sprintf("%s %s %s", $sql_string, $field_string, $table_type);
  return $sql_string.";";
}

function create_data_sql_string($tablename)
{
  $sql_string = "";
  $result = mysql_query("SELECT * FROM $tablename");
  while ($row = mysql_fetch_row($result)) {
     $data_string = "";
    for ($i = 0; $i < mysql_num_fields($result); $i++) {
      $data_string = sprintf("%s, '%s'", $data_string, mysql_escape_string($row[$i]));
    }
    $data_string = substr($data_string, 2);
    $data_string = sprintf("VALUES (%s)", $data_string).";";
    $sql_string = sprintf("%sINSERT INTO %s %s\n", $sql_string, $tablename, $data_string);
  }
  return $sql_string;
}
?>
<?php
if(isset($act) and $act=="bakup"){  
 $dir=date("Y_m_d");
 if(!file_exists("data/".$dir))  $r=mkdir("data/".$dir,0700);
 for($i=0;$i<12;$i++){
    $table = $GLOBALS["ch".$i];
    if($table!=""){
        if($bukcon=="all"){
           $datable = create_table_sql_string($table);
            $data = create_data_sql_string($table);
       $txt=$datable."\n".$data;
      }
      if($bukcon=="record") $txt = create_data_sql_string($table);
      if($bukcon=="struct") $txt = create_table_sql_string($table);
        $filename=$table."_".$bukcon.date('_H_m').'_'.'nb315'.'.sql';
         $fp=fopen("data/".$dir."/".$filename,"w+");
         if(fputs($fp,$txt))$out="数据表".$table."已成功备份在 data/$dir/目录下--文件名是$filename"."\\n\\n".$out;
      }
   }
   echo "<script>alert('$out');</script>";
}
?>
<?
  function check_dir($dir){
   $temp=opendir($dir);
   while($tempfile=readdir($temp)){
      if(($tempfile!=".")&&($tempfile!="..")&&($tempfile!="")){
         return $tempfile;
    }
    }
    closedir($temp);
    return 0;
  }
 
  if(isset($button) and $button=="删除"){
    for($i=0;$i<$filelength;$i++){
      $list= $GLOBALS["f".$i];
      if($list!=""){
       if(!is_dir($dir.$list)) unlink($dir.$list);
         else{
           if(check_dir($dir.$list)) echo "<script>alert('文件夹 ".$dir.$list." 不是空的,不能删除!');</script>";
          else rmdir($dir.$list);
         }
      }
   }
  }
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=gbk">
<STYLE type=text/css>A {
  COLOR: #003366; TEXT-DECORATION: none
}
A:hover {
  TEXT-DECORATION: underline
}
BODY {
  FONT-SIZE: 12px; SCROLLBAR-ARROW-COLOR: #698cc3; SCROLLBAR-BASE-COLOR: #f8f8f8; BACKGROUND-COLOR: #9eb6d8
}
TABLE {
  FONT: 12px Tahoma, Verdana; COLOR: #000000
}
INPUT {
  FONT: 11px Tahoma, Verdana; COLOR: #000000; BACKGROUND-COLOR: #f8f8f8
}
SELECT {
  FONT: 11px Tahoma, Verdana; COLOR: #000000; BACKGROUND-COLOR: #f8f8f8
}
TEXTAREA {
  FONT: 11px Tahoma, Verdana; COLOR: #000000; BACKGROUND-COLOR: #f8f8f8
}
FORM {
  PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px
}
SELECT {
  FONT: 11px Arial, Tahoma; COLOR: #000000; BACKGROUND-COLOR: #f8f8f8
}
.nav {
  FONT: bold 12px Tahoma, Verdana; COLOR: #000000
}
.nav A {
  COLOR: #000000
}
.header {
  FONT: bold 11px Tahoma, Verdana; COLOR: #ffffff; BACKGROUND-COLOR: #698cc3
}
.header A {
  COLOR: #ffffff
}
.category {
  FONT: 11px Arial, Tahoma; COLOR: #000000; BACKGROUND-COLOR: #efefef
}
.tableborder {
  BORDER-RIGHT: #698cc3 1px solid; BORDER-TOP: #698cc3 1px solid; BACKGROUND: #d6e0ef; BORDER-LEFT: #698cc3 1px solid; BORDER-BOTTOM: #698cc3 1px solid
}
.singleborder {
  PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: 0px; PADDING-BOTTOM: 0px; LINE-HEIGHT: 1px; PADDING-TOP: 0px; BACKGROUND-COLOR: #f8f8f8
}
.smalltxt {
  FONT: 11px Arial, Tahoma
}
.outertxt {
  FONT: 12px Tahoma, Verdana; COLOR: #000000
}
.outertxt A {
  COLOR: #000000
}
.bold {
  FONT-WEIGHT: bold
}
.altbg1 {
  BACKGROUND: #f8f8f8
}
.altbg2 {
  BACKGROUND: #ffffff
}
.maintable {
  WIDTH: 98%; BACKGROUND-COLOR: #ffffff
}
</STYLE>

<SCRIPT language=JavaScript src="admincp(2).files/common.js"></SCRIPT>

<SCRIPT language=JavaScript>
function checkalloption(form, value) {
  for(var i = 0; i < form.elements.length; i++) {
    var e = form.elements[i];
    if(e.value == value && e.type == 'radio' && e.disabled != true) {
      e.checked = true;
    }
  }
}

function zoomtextarea(objname, zoom) {
  zoomsize = zoom ? 10 : -10;
  obj = findobj(objname);
  if(obj.rows + zoomsize > 0 && obj.cols + zoomsize * 3 > 0) {
    obj.rows += zoomsize;
    obj.cols += zoomsize * 3;
  }
}

function redirect(url) {
  window.location.replace(url);
}
</SCRIPT>
  <script language="JavaScript">
function CheckAll(form) {
  for (var i=0;i<form.elements.length;i++) {
    var e = form.elements[i];
    if (e.name != 'chkall' && e.type=='checkbox')
      e.checked = form.chkall.checked;
  }
}

function check(){
  var bSelected = false ;
  for ( i = 0 ; i < document.form1.elements.length ; i ++ ){
   if ( document.form1.elements[i].type == "checkbox"){
    if ( document.form1.elements[i].checked ){
      bSelected = true ;
      break;
    }
    }
  }
  if ( ! bSelected ){
    alert ( "请选择一个数据表" ) ;
    document.form1.chkall.focus();
    return false;
  }
}
</script>

<META content="MSHTML 6.00.2900.3020" name=GENERATOR></HEAD>
<BODY text=#000000 topMargin=10 #9EB6D8 background-color:><BR>
<TABLE class=tableborder cellSpacing=1 cellPadding=4 width="85%" align=center>
 <TBODY>
 <TR class=header>
   <TD>友情提示</TD></TR>
 <TR bgColor=#f8f8f8>
   <TD><BR>
     <UL>
       <LI><font class="midwhite">警告:本系统将对数据表结构和数据记录完全备份,但不对数据库之外的任何文件进行备份,请自行备份文件</font>。</LI></UL>
     <UL>
       <LI>备份文件自动保存在服务器data目录下,请自行保存在本地电脑上,以防止意外。</LI>
     </UL>
     <UL>
       <LI>选择“数据结构+数据记录”,将保存数据库所有内容,选择“数据结构”或者“数据记录”,将只保存选择的部分。</LI></UL>
     <UL>
       <LI>请定期备份!</LI></UL></TD></TR></TBODY></TABLE><BR><BR>
    <? if(!isset($need) or $need!="viewfile" or !isset($dir) or $dir==""){ ?>
<TABLE class=tableborder cellSpacing=1 cellPadding=4 width="85%" align=center>
<tr>
 <td width="84%" valign="top">
          <input type="button" name="Submit2" value="选择备份数据库" onClick="window.location='sysdata.php'">&nbsp;&nbsp;
          <input type="button" name="Submit22" value="查看备份文件" onClick="window.location='?need=viewfile&dir=data/'">
 </td>
 </tr>
      <tr>
         <td width="84%" valign="top"><form name="form1" method="post" action="" onsubmit="return check()">
          <table width="100%" border="1" cellspacing="0" cellpadding="4">
            <tr>
              <td  align="right" class=header> 全选
                  <input type="checkbox" name="chkall" value="on" onclick="CheckAll(this.form)" title="点击可全选本页的所有选项">
              </td>
              <td class=header>数据库名</td>
              <td class=header>数据库用途</td>
            </tr>
            <tr>
              <td width="18%" bgcolor="#FFFFFF" align="right"><input type="checkbox" name="ch0" value="shm_advertisement"></td>
              <td width="36%" bgcolor="#FFFFFF" class="midback">shm_advertisement</td>
              <td width="46%" bgcolor="#FFFFFF" class="smallback">存放客户广告</td>
            </tr>
            <tr>
              <td bgcolor="#FFFFFF"  align="right"><input type="checkbox" name="ch1" value="shm_advice"></td>
              <td bgcolor="#FFFFFF" class="midback" >shm_advice</td>
              <td bgcolor="#FFFFFF" class="smallback" >存放客户意见</td>
            </tr>
            <tr>
              <td bgcolor="#FFFFFF"   align="right"><input type="checkbox" name="ch2" value="shm_bulletin"></td>
              <td bgcolor="#FFFFFF" class="midback" >shm_bulletin</td>
              <td bgcolor="#FFFFFF" class="smallback" >存放网站公告</td>
            </tr>
            <tr>
              <td bgcolor="#FFFFFF"  align="right"><input type="checkbox" name="ch3" value="shm_certify"></td>
              <td bgcolor="#FFFFFF" class="midback" >shm_certify</td>
              <td bgcolor="#FFFFFF" class="smallback" >会员认证</td>
            </tr>
            <tr>
              <td height="27"  bgcolor="#FFFFFF" align="right"><input type="checkbox" name="ch4" value="forum_message">
              </td>
              <td  bgcolor="#FFFFFF" class="midback">forum_message</td>
              <td  bgcolor="#FFFFFF" class="smallback">文章评论</td>
            </tr>
            <tr>
              <td bgcolor="#FFFFFF"  align="right"><input type="checkbox" name="ch5" value="shm_friendship"></td>
              <td bgcolor="#FFFFFF" class="midback" >shm_friendship</td>
              <td bgcolor="#FFFFFF" class="smallback" >会员间关系如商友</td>
            </tr>
            <tr>
              <td bgcolor="#FFFFFF"  align="right"><input type="checkbox" name="ch6" value="shm_message"></td>
              <td bgcolor="#FFFFFF" class="midback" >shm_message</td>
              <td bgcolor="#FFFFFF" class="smallback" >会员留言</td>
            </tr>
            <tr>
              <td bgcolor="#FFFFFF"  align="right"><input type="checkbox" name="ch7" value="shm_productlist"></td>
              <td bgcolor="#FFFFFF" class="midback" >shm_productlist</td>
              <td bgcolor="#FFFFFF" class="smallback" >供求信息</td>
            </tr>
            <tr>
              <td bgcolor="#FFFFFF"   align="right"><input type="checkbox" name="ch8" value="shm_siteadmin"></td>
              <td bgcolor="#FFFFFF" class="midback" >shm_siteadmin</td>
              <td bgcolor="#FFFFFF" class="smallback" >网站管理员</td>
            </tr>
            <tr>
              <td bgcolor="#FFFFFF"  align="right"><input type="checkbox" name="ch9" value="txt_bizinfo"></td>
              <td bgcolor="#FFFFFF" class="midback" >txt_bizinfo</td>
              <td bgcolor="#FFFFFF" class="smallback" >行业资讯,商务指南</td>
            </tr>
            <tr>
              <td bgcolor="#FFFFFF" align="right" ><input type="checkbox" name="ch10" value="shm_userlist"></td>
              <td bgcolor="#FFFFFF" class="midback" >shm_userlist</td>
              <td bgcolor="#FFFFFF" class="smallback" >会员列表</td>
            </tr>
            <tr>
              <td bgcolor="#FFFFFF" align="right" ><input type="checkbox" name="ch11" value="shm_vacation"></td>
              <td bgcolor="#FFFFFF" class="midback" >shm_vacation</td>
              <td bgcolor="#FFFFFF" class="smallback" >行业名称</td>
            </tr>
            <tr>
              <td  bgcolor="#FFFFFF" class=midback align=right>选择备份内容</td>
          <td colspan="2" bgcolor="#FFFFFF">
        <select name="bukcon" style="background-color: #FF0000;border: #3366CC;color: #ffffff;font-weight: bold;">
          <option selected value="all">数据库结构+数据库记录</option>
               <option value="record">数据库记录</option>
        <option value="struct">数据库结构</option>
        </select>
        </td>
         </tr>
            <tr>
              <td colspan="3" bgcolor="#FFFFFF"><input type="hidden" name="act" value="bakup">
                  <input name="Submit" type="submit" class="deletebutton" value="立即对选定数据表进行备份">
              </td>
            </tr>
        </table>
        </form></td>
      </tr>
</table><BR><BR>
   <? }else{ ?>
<TABLE class=tableborder cellSpacing=1 cellPadding=4 width="85%" align=center>
      <tr>
        <td height="26" valign="top"><input type="button" name="Submit23" value="选择备份数据库" onClick="window.location='sysdata.php'">&nbsp;&nbsp;
     <input type="button" name="Submit222" value="查看备份文件" onClick="window.location='?need=viewfile&dir=data/'">
     </td>
      </tr>
      <tr>
        <td height="26" class=header>当前目录:<? echo $dir ?></td>
      </tr>
      <tr>
        <td valign="top">
          <form name="form2" method="post" action="">
          <table width="100%" border="1" cellspacing="0" cellpadding="2">
            <tr>
              <td width="8%"><input type="checkbox" name="chkall" value="on" onclick="CheckAll(this.form)" >全选
              </td>
              <td width="92%" class=midback>文件或文件夹名</td>
            </tr>
            <?
     $opendir=opendir($dir);
      $num=0;
      while($file_name=readdir($opendir)){
              if(($file_name!=".") and ($file_name!="..")){
           $temp= "<tr><td ><input type=checkbox name=f$num value=$file_name></td>";
           if(!is_dir($dir.$file_name))  $temp=$temp."<td> <a href=opentxt.php?fid=$dir$file_name target=blank><img src=images/listfile.gif border=0>$file_name</a></td></tr>";
           else $temp=$temp."<td> <a href=?need=viewfile&dir=$dir$file_name/><img src=images/fold.gif border=0>$file_name</a></td></tr>";
           $num=$num+1;
          echo $temp;
        }
      }
    closedir($opendir);
    clearstatcache();
    if($num==0) echo "<tr> <td colspan=2 class=smallred>此目录是空的,没有文件</td></tr>";
     ?>
            <tr>
              <td colspan="2">
           <?  if($PWARRAY[adright]==1){ ?>
         <input type=hidden name=filelength value=<? echo $num ?>>
         <input name="button" type="submit" class="deletebutton" value="删除" onclick="return confirm('确定删除此操作?');">
                <? }else{ ?>
         <input name="button" type="submit" disabled class="deletebutton" value="删除" onclick="return confirm('确定删除此记录?');">
         <? } ?>
         <? if($dir=="data/"){ ?>
         <input type="submit" name="button" value="返回" disabled>
         <? }else{ ?>
         <input type="button" name="button" value="返回" onclick="window.location='?need=viewfile&dir=data/'">
         <? } ?>
         </td>
            </tr>
          </table>
        </form>
         </td>
      </tr>
    </table>
   <? } ?>
   <BR><BR>
<HR width="80%" color=#698cc3 noShade SIZE=0>

<CENTER><FONT
style="FONT-SIZE: 11px; FONT-FAMILY: Tahoma, Verdana, Arial">Powered by <A
style="COLOR: #000000" href="http://www.foodmate.com/"
target=_blank><B>foodmate.com</B></A> &nbsp;&copy; 2001-2008, <B>foodmate.com
Inc.</B></FONT> </CENTER></BODY></HTML>

2008-12-1 15:37
我日,整理好了再发不行啊
2008-4-22 12:38
靠   代码不全的都发
jed 回复于 2008-4-23 10:28
呵呵,核心有不就行了,链接数据库不用再教了吧!
分页: 1/1 第一页 1 最后页
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]