php MYSQL 數(shù)據(jù)備份類
更新時(shí)間:2009年06月19日 21:41:25 作者:
一個(gè)簡單MYSQL的數(shù)據(jù)備份類 這些一直都在搞數(shù)據(jù),因此數(shù)據(jù)的備份就少不了的了,如果不寫這類一個(gè)簡單MYSQL的數(shù)據(jù)備份類,那將是很麻煩的。自己就下定決心,寫了一個(gè)。
功能上有:
require_once("backdata.class.php");
$link = @mysql_connect("localhost","數(shù)據(jù)庫名","密碼") or die ('Could not connect to server.');
mysql_query("use cms",$link);
mysql_query("set names utf8",$link);
$dbbck=new backupData($link);//實(shí)例化它,只要一個(gè)鏈接標(biāo)識就行了
//備份數(shù)據(jù)時(shí),如想備份一個(gè)數(shù)據(jù)庫中的所有表,你可這樣寫:
$dbbck->backupTables("cms","./",array('*'));
//備份數(shù)據(jù)時(shí),如想備份一個(gè)數(shù)據(jù)庫中的僅一個(gè)表時(shí),你可這樣寫:
$dbbck->backupTables("cms","./",array('user'));
//備份數(shù)據(jù)時(shí),如想備份一個(gè)數(shù)據(jù)庫中的多個(gè)表時(shí),你可這樣寫:
$dbbck->backupTables("cms","./",array('user','acl','informatoin'));
//注解:$dbbck->backupTables("參1","參2",array());中,
參1為:數(shù)據(jù)庫名,
參2為:要存放備份數(shù)據(jù)的位置(即目錄地址)
第三個(gè)為:你要保存那些表
ok...
以下為代碼:
<?php
/*
*
*簡單的一個(gè)備份數(shù)據(jù)類
*author FC
*
*/
class backupData{
private $mysql_link;//鏈接標(biāo)識
private $dbName;//數(shù)據(jù)庫名
private $dataDir; //數(shù)據(jù)所要存放的目錄
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==''){//表不存在時(shí)
continue;
}
//************************以下是形成SQL的前半部分**************
//如果存在表,就先刪除
$sqls .= "DROP TABLE IF EXISTS $tablename;\n";
//讀取表結(jié)構(gòu)
$rs = mysql_query("SHOW CREATE TABLE $tablename",$this->mysql_link);
$row=mysql_fetch_row($rs);
//獲得表結(jié)構(gòu)組成SQL
$sqls.=$row['1'].";\n\n";
unset($rs);
unset($row);
//************************以下是形成SQL的后半部分**************
//查尋出表中的所有數(shù)據(jù)
$rs=mysql_query("select * from $tablename",$this->mysql_link);
//表的字段個(gè)數(shù)
$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.=");\n\n\n";
}
}
$backfilepath=$this->dataDir.date("Ymdhis",time()).'.sql';
//寫入文件
$filehandle = fopen($backfilepath, "w");
fwrite($filehandle, $sqls);
fclose($filehandle);
}
private function delarray($array){//處理傳入進(jìn)來的數(shù)組
foreach($array as $tables){
if($tables=='*'){//所有的表(獲得表名時(shí)不能按常規(guī)方式來組成一個(gè)數(shù)組)
$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;
}
}
require_once("backdata.class.php");
$link = @mysql_connect("localhost","數(shù)據(jù)庫名","密碼") or die ('Could not connect to server.');
mysql_query("use cms",$link);
mysql_query("set names utf8",$link);
$dbbck=new backupData($link);//實(shí)例化它,只要一個(gè)鏈接標(biāo)識就行了
//備份數(shù)據(jù)時(shí),如想備份一個(gè)數(shù)據(jù)庫中的所有表,你可這樣寫:
$dbbck->backupTables("cms","./",array('*'));
//備份數(shù)據(jù)時(shí),如想備份一個(gè)數(shù)據(jù)庫中的僅一個(gè)表時(shí),你可這樣寫:
$dbbck->backupTables("cms","./",array('user'));
//備份數(shù)據(jù)時(shí),如想備份一個(gè)數(shù)據(jù)庫中的多個(gè)表時(shí),你可這樣寫:
$dbbck->backupTables("cms","./",array('user','acl','informatoin'));
//注解:$dbbck->backupTables("參1","參2",array());中,
參1為:數(shù)據(jù)庫名,
參2為:要存放備份數(shù)據(jù)的位置(即目錄地址)
第三個(gè)為:你要保存那些表
ok...
以下為代碼:
復(fù)制代碼 代碼如下:
<?php
/*
*
*簡單的一個(gè)備份數(shù)據(jù)類
*author FC
*
*/
class backupData{
private $mysql_link;//鏈接標(biāo)識
private $dbName;//數(shù)據(jù)庫名
private $dataDir; //數(shù)據(jù)所要存放的目錄
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==''){//表不存在時(shí)
continue;
}
//************************以下是形成SQL的前半部分**************
//如果存在表,就先刪除
$sqls .= "DROP TABLE IF EXISTS $tablename;\n";
//讀取表結(jié)構(gòu)
$rs = mysql_query("SHOW CREATE TABLE $tablename",$this->mysql_link);
$row=mysql_fetch_row($rs);
//獲得表結(jié)構(gòu)組成SQL
$sqls.=$row['1'].";\n\n";
unset($rs);
unset($row);
//************************以下是形成SQL的后半部分**************
//查尋出表中的所有數(shù)據(jù)
$rs=mysql_query("select * from $tablename",$this->mysql_link);
//表的字段個(gè)數(shù)
$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.=");\n\n\n";
}
}
$backfilepath=$this->dataDir.date("Ymdhis",time()).'.sql';
//寫入文件
$filehandle = fopen($backfilepath, "w");
fwrite($filehandle, $sqls);
fclose($filehandle);
}
private function delarray($array){//處理傳入進(jìn)來的數(shù)組
foreach($array as $tables){
if($tables=='*'){//所有的表(獲得表名時(shí)不能按常規(guī)方式來組成一個(gè)數(shù)組)
$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;
}
}
您可能感興趣的文章:
- php生成mysql的數(shù)據(jù)字典
- PHP實(shí)現(xiàn)獲取并生成數(shù)據(jù)庫字典的方法
- ThinkPHP框架實(shí)現(xiàn)的MySQL數(shù)據(jù)庫備份功能示例
- PHP備份/還原MySQL數(shù)據(jù)庫的代碼
- php實(shí)現(xiàn)mysql數(shù)據(jù)庫備份類
- 使用PHP備份MYSQL數(shù)據(jù)的多種方法
- php實(shí)現(xiàn)MySQL數(shù)據(jù)庫備份與還原類實(shí)例
- 使用php自動備份數(shù)據(jù)庫表的實(shí)現(xiàn)方法
- 用PHP實(shí)現(xiàn)XML備份Mysql數(shù)據(jù)庫
- PHP實(shí)現(xiàn)生成數(shù)據(jù)字典功能示例
相關(guān)文章
PHP分多步驟填寫發(fā)布信息的簡單方法實(shí)例代碼
有的時(shí)候,在做發(fā)布信息提交頁面時(shí),需要把很多復(fù)雜信息提交頁面分成多個(gè)步驟來提交。下現(xiàn)就是實(shí)現(xiàn)的這種功能的基本方法2012-09-09動態(tài)表單驗(yàn)證的操作方法和TP框架里面的ajax表單驗(yàn)證
這篇文章主要介紹了動態(tài)表單驗(yàn)證的操作方法和TP框架里面的ajax表單驗(yàn)證問題,需要的朋友可以參考下2017-07-07PHP筆記之:基于面向?qū)ο笤O(shè)計(jì)的詳解
本篇文章對面向?qū)ο笤O(shè)計(jì)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05PHP程序員簡單的開展服務(wù)治理架構(gòu)操作詳解(二)
這篇文章主要介紹了PHP程序員簡單的開展服務(wù)治理架構(gòu)操作,結(jié)合實(shí)例形式分析了rpc客戶端與服務(wù)器相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-05-05