php MYSQL 數(shù)據(jù)備份類
更新時(shí)間:2009年06月19日 21:41:25 作者:
一個(gè)簡(jiǎn)單MYSQL的數(shù)據(jù)備份類 這些一直都在搞數(shù)據(jù),因此數(shù)據(jù)的備份就少不了的了,如果不寫這類一個(gè)簡(jiǎn)單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í)就行了
//備份數(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
/*
*
*簡(jiǎn)單的一個(gè)備份數(shù)據(jù)類
*author FC
*
*/
class backupData{
private $mysql_link;//鏈接標(biāo)識(shí)
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='';//逗號(hào)
$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í)就行了
//備份數(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
/*
*
*簡(jiǎn)單的一個(gè)備份數(shù)據(jù)類
*author FC
*
*/
class backupData{
private $mysql_link;//鏈接標(biāo)識(shí)
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='';//逗號(hào)
$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自動(dòng)備份數(shù)據(jù)庫表的實(shí)現(xiàn)方法
- 用PHP實(shí)現(xiàn)XML備份Mysql數(shù)據(jù)庫
- PHP實(shí)現(xiàn)生成數(shù)據(jù)字典功能示例
相關(guān)文章
ThinkPHP靜態(tài)緩存簡(jiǎn)單配置和使用方法詳解
這篇文章主要介紹了ThinkPHP靜態(tài)緩存簡(jiǎn)單配置和使用方法,結(jié)合實(shí)例形式詳細(xì)分析了ThinkPHP靜態(tài)緩存簡(jiǎn)單配置方法,常用參數(shù)含義與相關(guān)使用技巧,需要的朋友可以參考下2016-03-03
實(shí)例講解PHP設(shè)計(jì)模式編程中的簡(jiǎn)單工廠模式
這篇文章主要介紹了PHP設(shè)計(jì)模式編程中的簡(jiǎn)單工廠模式,舉了一個(gè)水果銷售和一個(gè)計(jì)算器設(shè)計(jì)的例子,需要的朋友可以參考下2016-02-02
PHP命令行執(zhí)行整合pathinfo模擬定時(shí)任務(wù)實(shí)例
下面小編就為大家?guī)硪黄狿HP命令行執(zhí)行整合pathinfo模擬定時(shí)任務(wù)實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08
Drupal7中常用的數(shù)據(jù)庫操作實(shí)例
Drupal 7 提供的新功能其中一個(gè)就是可以使用 Query Builder and Query Objects 查詢生成器來構(gòu)造查詢對(duì)象的能力,無需在代碼中寫原始的SQL語句,一是提高了代碼可閱讀性,二是兼容所有數(shù)據(jù)庫2014-03-03
Yii2使用小技巧之通過 Composer 添加 FontAwesome 字體資源
前天幫同事改個(gè)十年前的網(wǎng)站 bug,頁面上一堆 include require 不禁讓人抱頭痛哭。看到 V2EX 上的討論說,寫 PHP 不用框架等同于耍流氓。Yii Framework 是我使用了 2 年多的 PHP 框架,器大活好,皮實(shí)耐操。 Yii2 還在 Beta 中,不過不影響拿來預(yù)研。2014-06-06
php使用file_get_contents(‘php://input‘)和$_POST的區(qū)別實(shí)例對(duì)比
這篇文章主要介紹了php使用file_get_contents(‘php://input‘)和$_POST的區(qū)別實(shí)例對(duì)比,這個(gè)知識(shí)點(diǎn)是比較常用的,有需要的可以參考下2021-03-03

