PHP MysqlDatabase Backup類程式總結

來源:互聯網
上載者:User

Database Backup類

使用方法:

 代碼如下 複製代碼

 

require_once("backdata.class.php");
$link = @mysql_connect("localhost","資料庫名","密碼") or die ('Could not connect to server.');
mysql_query("use cms",$link);
mysql_query("set names utf8",$link);

$dbbck = new backupData($link);//執行個體化它,只要一個連結標識就行了

//備份資料時,如想備份一個資料庫中的所有表,你可這樣寫:
$dbbck->backupTables("cms","./",array('*'));

//備份資料時,如想備份一個資料庫中的僅一個表時,你可這樣寫:
$dbbck->backupTables("cms","./",array('user'));

//備份資料時,如想備份一個資料庫中的多個表時,你可這樣寫:
$dbbck->backupTables("cms","./",array('user','acl','informatoin'));

//註解:$dbbck->backupTables("參1","參2",array());中,

參1為:資料庫名,
參2為:要存放備份資料的位置(即目錄位址)
第三個為:你要儲存那些表 

backdata.class.php
 

 代碼如下 複製代碼

<?php
/*
*
*一個簡單的Mysql備份資料類
*
*/
class backupData{
    private    $mysql_link;//連結標識
    private    $dbName;    //資料庫名
    private    $dataDir;     //資料所要存放的目錄
    private    $tableNames;//表名

    public function __construct($mysql_link){
         $this->mysql_link = $mysql_link;
    }
    public function backupTables($dbName,$dataDir,$tableNames){//開始備份
        $this->dbName  = $dbName;
        $this->dataDir  = $dataDir;
        $this->tableNames = $tableNames;
        $tables=$this->delarray($this->tableNames);
        $sqls='';
        foreach($tables as $tablename){
            if($tablename==''){//表不存在時
                continue;
            }
           
            //************************以下是形成SQL的前半部分**************
            //如果存在表,就先刪除
            $sqls .= "DROP TABLE IF EXISTS $tablename;n";
            //讀取表結構
            $rs = mysql_query("SHOW CREATE TABLE $tablename",$this->mysql_link); 
            $row=mysql_fetch_row($rs);
            //獲得表結構組成SQL
            $sqls.=$row['1'].";nn";
            unset($rs);
            unset($row);
           
            //************************以下是形成SQL的後半部分**************
            //查尋出表中的所有資料
            $rs=mysql_query("select * from $tablename",$this->mysql_link);
            //表的欄位個數
            $field=mysql_num_fields($rs);
            //形成此種SQL語句:"INSERT INTO `groups` VALUES('1499e0ca25988d','主任','','0');"
            while($rows=mysql_fetch_row($rs)){
                $comma='';//逗號
                $sqls.="INSERT INTO `$tablename` VALUES(";
                for($i=0;$i<$field;$i++){
                    $sqls.=$comma."'".$rows[$i]."'";
                    $comma=',';
                }
                $sqls.=");nnn";
            }
        }
        $backfilepath=$this->dataDir.date("Ymdhis",time()).'.sql';
       
        //寫入檔案
        $filehandle = fopen($backfilepath, "w");
        fwrite($filehandle, $sqls);
        fclose($filehandle);
    }
    private function delarray($array){    //處理傳入進來的數組
        foreach($array as $tables){
            if($tables=='*'){    //所有的表(獲得表名時不能按常規方式來組成一個數組)
                $newtables=mysql_list_tables($this->dbName,$this->mysql_link);
                $tableList = array();
                for ($i = 0; $i < mysql_numrows($newtables); $i++){
                    array_push($tableList,mysql_tablename($newtables, $i));
                }
                $tableList=$tableList;
            }else{
                $tableList=$array;
                break;
            }
        }
        return $tableList;
    }
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.