歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> 更多Linux >> 關於如何備份數據庫(Mysql)的簡易程序

關於如何備份數據庫(Mysql)的簡易程序

日期:2017/2/27 14:23:46   编辑:更多Linux
  在公司(網絡)的服務器上有很多網站,我上傳網站時對其他瓦干也有同樣的權限。所以就想把同事的網站蕩下來學習一下。有了源代碼,沒有數據庫什麼也運行不了啊。服務器的數據庫是安網站開的,每一個網站把*.sql發給服務器管理員,由管理員導入到數據庫中。但是數據庫賬號密碼不能登陸只能執行sql語句。所以我就想起來做一個簡易的,類似於PHPmyadmin的數據庫導出功能。這樣我就可以輕松的獲得所有網站的數據庫了。    用這個程序可以實現簡單的數據導出(生成的腳本可直接在phpmyadmin執行)。進一步的功能還可以慢慢擴展。當然不要用來盜用別人機密的冬冬了。    其中解析的數據類型還不全,只是集中簡單的常用類型。如果那位仁兄,發現了其他不適用的類型,請告訴我mailto:[email protected]。     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:[email protected]  //*****************************************************************  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  //功能:返回指定數據庫中表的結構,用於創建指定表的SQL語句  //參數:$link 是數據庫連接  // $database 數據庫名  // $table_name表名  //時間:2004年3月23日  //作者:野馬  //QQ:46163020  //Email:[email protected]  //*****************************************************************  function get_table_code($link,$database,$table_name)  {  mysql_select_db($database, $link);  $result = mysql_query("select * from ".$table_name, $link);  $return_str = "CREATE TABLE `".$table_name."` (\n\t";  $fields_num = (int)mysql_num_fields($result);    $fun_my['string'] = "varchar";  $fun_my['datetime'] = "datetime";  $fun_my['blob'] = "text";  $fun_my['real'] = "float";  echo "<br>";  for($i=0; $i < $fields_num; $i++)  {  $return_str .= "`".mysql_field_name($result, $i)."` ";    $type = mysql_field_type($result, $i);  if(isset($fun_my[$type]))  {  //解析數據類型  if($fun_my[$type]=="datetime" $fun_my[$type]=="text")  {  $return_str .= $fun_my[$type]." ";  }else{  $return_str .= $fun_my[$type]."(".mysql_field_len($result, $i).") ";  }//end if  }else{  $return_str .= $type."(".mys




Copyright © Linux教程網 All Rights Reserved