PHP Mysql数据库备份类程序总结
数据库备份类我们只要在网上一搜索在N多的类代码,下面我来总结几款不错数据库备份希望对大家有所帮助.
数据库备份类,使用方法,代码如下:
- 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'));
-
-
参1为:数据库名
参2为:要存放备份数据的位置(即目录地址)
第三个为:你要保存那些表
backdata.class.php,代码如下:
- <?php
-
-
-
-
-
- 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;
- }
-
-
-
- $sqls .= "DROP TABLE IF EXISTS $tablename;n";
-
- $rs = mysql_query("SHOW CREATE TABLE $tablename",$this->mysql_link);
- $row=mysql_fetch_row($rs);
-
- $sqls.=$row['1'].";nn";
- unset($rs);
- unset($row);
-
-
-
- $rs=mysql_query("select * from $tablename",$this->mysql_link);
-
- $field=mysql_num_fields($rs);
-
- 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;
- }
- }
- ?>