数据库备份类
使用方法:
代码如下 复制代码
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
代码如下 复制代码
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 $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 array_push($tablelist,mysql_tablename($newtables, $i));
}
$tablelist=$tablelist;
}else{
$tablelist=$array;
break;
}
}
return $tablelist;
}
}
?>
http://www.bkjia.com/phpjc/630673.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/630673.htmltecharticle数据库备份类我们只要在网上一搜索在n多的类代码,下面我来总结几款不错数据库备份希望对大家有所帮助。 数据库备份类 使用方法: 代...
