当前位置:数据库 > MySQL >>

关于如何备份数据库(Mysql)的简易程序

答案:在公司(网络)的服务器上有很多网站,我上传网站时对其他瓦干也有同样的权限。所以就想把同事的网站荡下来学习一下。有了源代码,没有数据库什么也运行不了啊。服务器的数据库是安网站开的,每一个网站把*.sql发给服务器管理员,由管理员导入到数据库中。但是数据库账号密码不能登陆只能执行sql语句。所以我就想起来做一个简易的,类似于phpmyadmin的数据库导出功能。这样我就可以轻松的获得所有网站的数据库了。

  用这个程序可以实现简单的数据导出(生成的脚本可直接在phpmyadmin执行)。进一步的功能还可以慢慢扩展。当然不要用来盗用别人机密的冬冬了。

  其中解析的数据类型还不全,只是集中简单的常用类型。如果那位仁兄,发现了其他不适用的类型,请告诉我mailto:woaini4651@yahoo.com.cn。

  


  index.php

  <html>

  <head>

  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">

  <title>数据库备份</title>

  <style type="text/css">

  .borderoff{ border-style:none; background-color:#F3F3F3}

  </style>

  </head>

  <body>

  <table width="100%" border="0" cellspacing="1" cellpadding="0" align="center">

  <tr valign="top">

  <td height="378"><br>

  <form action="cmd.php" method="post" name="backup" target="_blank" id="backup">

  <table width="500" border="0" cellspacing="1" cellpadding="4" align="center">

  <tr>

  <td colspan="2" align="center">数据库备份</td>

  </tr>

  <tr bgcolor="#F3F3F3">

  <td width="166" align="right"> 服务器名:</td>

  <td width="313"><input name="hostname" type="text" class="borderoff" value="localhost" size="35" maxlength="50">

  </td>

  </tr>

  <tr bgcolor="#F3F3F3">

  <td width="166" align="right"> 数据库名:</td>

  <td width="313"><input name="database" type="text" class="borderoff" value="yes_da" size="35" maxlength="50">

  </td>

  </tr>

  <tr bgcolor="#F3F3F3">

  <td width="166" align="right">账号:</td>

  <td width="313"><input name="username" type="text" class="borderoff" value="root" size="35" maxlength="50">

  </td>

  </tr>

  <tr bgcolor="#F3F3F3">

  <td width="166" align="right">密码:</td>

  <td width="313"><input name="password" type="text" class="borderoff" size="35" maxlength="50">

  </td>

  </tr>

  <tr bgcolor="#F3F3F3">

  <td width="166" align="right"> 完全备份:</td>

  <td width="313">

  <input type="radio" name="backup_type" value="full" checked>

  </td>

  </tr>

  <tr bgcolor="#F3F3F3">

  <td width="166" align="right">只备份结构:</td>

  <td width="313">

  <input type="radio" name="backup_type" value="structure">

  </td>

  </tr>

  <tr bgcolor="#F3F3F3">

  <td width="166" align="right">只备份数据:</td>

  <td width="313">

  <input type="radio" name="backup_type" value="data">

  </td>

  </tr>

  <tr bgcolor="#F3F3F3">

  <td width="166" align="right">采用压缩格式:</td>

  <td width="313">

  <input type="radio" name="gzipcompress" value="0" checked>

  否

  <input type="radio" name="gzipcompress" value="1">

  是(zip)</td>

  </tr>

  <tr>

  <td colspan="2" align="center">

  <input name="backupstart" type="submit" value="开始备份">

  </td>

  </tr>

  </table>

  </form>

  <p align="center"> <br>

  <br>

  </p></td>

  </tr>

  </table>

  </body>

  </html>

  cmd.php

  <?php

  if(isset($_POST['backupstart']))

  {

  $hostname = trim($_POST['hostname']);

  $database = trim($_POST['database']);

  $username = trim($_POST['username']);

  $password = trim($_POST['password']);

  $backtype = $_POST['backup_type'];

  $gz

  = $_POST['gzipcompress'];

  $link = @mysql_pconnect($hostname, $username, $password);

  if(!$link)

  {

  //连接数据库

  echo "数据库打开出错!";

  exit();

  }//end if

  $table_list = get_table_list($link,$database);

  if($table_name===false)

  {

  //检索数据库的表

  echo "数据库打开出错!!";

  exit();

  }//end if

  //echo "<pre>";

  //print_r($table_list);

  $table_code = "";

  while(list($key,$table_name)=each($table_list))

  {

  //执行对每一个表的数据返回

  if($backtype!="data")

  {

  //备份结构

  $table_code .= get_table_code($link,$database,$table_name);

  }//end if

  if($backtype!="structure")

  {

  //备份数据

  $table_code .= get_table_data($link,$database,$table_name);

  }//end if

  }//end while

  //echo $table_code;

  if($gz==0)

  {

  //输出

  $filename = $database.".sql";

  $handle = fopen($filename, "a+");

  $temp = fwrite($handle, $table_code);

  echo $temp;

  fclose($handle);

  //header("location:",$filename);

  echo "<script>location.replace('".$filename."');</script>";

  }elseif($gz==1){

  $filename = $database.".zip";

  $fp = gzopen($filename, "w9");

  gzwrite($fp,$table_code);

  gzclose($fp);

  //header("location:",$filename);

  echo "<script>location.replace('".$filename."');</script>";

  }//end if

  }else{

  echo "连接错误!!!";

  exit();

  }//end if

  //*****************************************************************

  //函数名:get_table_list

  //功能:返回指定数据库中的所有表名

  //参数:$link

  是数据库连接

  //

  $database

  数据库名

  //时间:2004年3月23日

  //作者:野马

  //QQ:46163020

  //Email:woaini4651@yahoo.com.cn

  //*****************************************************************

  function get_table_list($link,$database)

  {

  $result = @mysql_list_tables($database);

  if(!$result)

  {//判断打开是否出错

  return false;

  }//end if

  while($row = mysql_fetch_row($result))

  {

  $table_name[] = $row[0];

  }//end while

  mysql_free_result($result);

  return $table_name;

  }//end function

  //*****************************************************************

  //函数名:get_table_code

  //功能:返回指

上一个:Mysql日期和时间函数不求人的具体方法
下一个:如何才能使图形化管理MySQL更轻松(二)

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,