php備份與還原mysql資料庫

來源:互聯網
上載者:User

標籤:

1、設定檔sql.config.php

<?php    $cfg_dbhost = "localhost"; //資料庫主機名稱     $cfg_dbuser = "root"; //資料庫使用者名稱     $cfg_dbpwd = "123456"; //資料庫密碼     $cfg_dbname = "familymart"; //資料庫名 ?>

2、備份程式db_backup.php

<?php     date_default_timezone_set(‘Asia/Shanghai‘);    require_once((dirname(__FILE__).‘/sql.config.php‘));     // 設定SQL檔案儲存檔案名稱     $filename=date("Y-m-d_H-i-s")."-".$cfg_dbname.".sql";     // 所儲存的檔案名稱     header("Content-disposition:filename=".$filename);     header("Content-type:application/octetstream");     header("Pragma:no-cache");     header("Expires:0");     // 擷取當前分頁檔路徑,SQL檔案就匯出到此檔案夾內     $tmpFile = (dirname(__FILE__))."/".$filename;     // 用MySQLDump命令匯出資料庫     exec("mysqldump -h$cfg_dbhost -u$cfg_dbuser -p$cfg_dbpwd --default-character-set=utf8 $cfg_dbname > ".$tmpFile);     $file = fopen($tmpFile, "r"); // 開啟檔案     echo fread($file,filesize($tmpFile));     fclose($file);     exit; ?> 

3、還原資料庫之restore_mysql.html

<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>還原資料庫</title></head><body>    <form id="form1" name="form1" method="post" action="db_restore.php">     【資料庫SQL檔案】:<input id="sqlFile" name="sqlFile" type="file" />     <input id="submit" name="submit" type="submit" value="還原" />     </form> </body></html>

4、還原程式db_restore.php

<?php // 我的資料庫資訊都存放到config.php檔案中,所以載入此檔案,如果你的不是存放到該檔案中,注釋此行即可; require_once((dirname(__FILE__).‘/sql.config.php‘)); if ( isset ( $_POST[‘sqlFile‘] ) ) {     $file_name = $_POST[‘sqlFile‘]; //要匯入的SQL檔案名稱     //$file_name = "2014-06-10_09-24-44-familymart-app.sql";    $dbhost = $cfg_dbhost; //資料庫主機名稱     $dbuser = $cfg_dbuser; //資料庫使用者名稱     $dbpass = $cfg_dbpwd; //資料庫密碼     $dbname = $cfg_dbname; //資料庫名     set_time_limit(0); //設定逾時時間為0,表示一直執行。當php在safe mode模式下無效,此時可能會導致匯入逾時,此時需要分段匯入     $fp = @fopen($file_name, "r") or die("不能開啟SQL檔案 $file_name");//開啟檔案     mysql_connect($dbhost, $dbuser, $dbpass) or die("不能串連資料庫 $dbhost");//串連資料庫     mysql_select_db($dbname) or die ("不能開啟資料庫 $dbname");//開啟資料庫     echo "<p>正在清空資料庫,請稍等....<br>";     $result = mysql_query("SHOW tables");     while ($currow=mysql_fetch_array($result))     {     mysql_query("drop TABLE IF EXISTS $currow[0]");     echo "清空資料表【".$currow[0]."】成功!<br>";     }     echo "<br>恭喜你清理MYSQL成功<br>";     echo "正在執行匯入資料庫操作<br>";     // 匯入資料庫的MySQL命令     exec("mysql -h$cfg_dbhost -u$cfg_dbuser -p$cfg_dbpwd $cfg_dbname < ".$file_name);     echo "<br>匯入完成!";     mysql_close(); } ?> 

 如果通過程式還原資料庫之後,探索資料出錯,可能是資料庫版本不一樣,對‘\n‘等字元解析結果不同,而上面的程式還原時又並沒有連結資料庫,

此時可通過phpmyadmin匯入功能還原,如果資料庫檔案較大,還需要把phpmyadmin和php.ini相關配置調大

 

php備份與還原mysql資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.