php实例分享之实例分享之mysql数据备份数据备份
备份:表结构和数据完全分开,默认有一个文件会记录所有表的结构,然后表中数据的备份 如果超过分卷的大小则会分成多个
文件,不然则一个文件,参考了别人的代码,不过写的嘛,差强 人意,以后慢慢改吧。。。
代码如下:
复制代码 代码如下:
<?php
/*
* Created on 2014
* Link for 527891885@qq.com
* This is seocheck backup class
*/
class DbBackUp {
private $conn;
private $dbName;
private $host;
private $tag = ‘_b’;
//构造方法 链接数据库
public function __construct($host=’localhost’, $dbUser=’root’, $dbPwd=”, $dbName=”seocheck”, $charset=’utf8′) {
@ob_start();
@set_time_limit(0);
$this->conn = mysql_connect($host, $dbUser, $dbPwd, true);
if(!$this->conn) die(“数据库系统连接失败!”);
mysql_query(“set names “.$charset, $this->conn);
mysql_select_db($dbName, $this->conn) or die(“数据库连接失败!”);
$this->host = $host;
$this->dbName = $dbName;
}
//获取数据库所有表名
public function getTableNames () {
$tables = array();
$result = mysql_list_tables($this->dbName, $this->conn);
if(!$result) die(‘MySQL Error: ‘ . mysql_error());
while($row = mysql_fetch_row($result)) {
$tables[] = $row[0];
}
return $tables;
}
//获取数据库表的字段信息
public function getFieldsByTable ($table) {
$fields = array();
$str = ”;
$res = mysql_query(“SHOW CREATE TABLE `{$table}`”, $this->conn);
if(!$res) die(‘MySQL Error: ‘ . mysql_error());
while($rows = mysql_fetch_assoc($res)) {
$str = str_replace(“CREATE TABLE `{$table}` (“, “”, $rows[‘Create Table’]);//DROP TABLE IF EXISTS `{$table}`
$str = “–– Table structure for table `{$table}`–CREATE TABLE IF NOT EXISTS `{$table}` ( “.$str;
$str = str_replace(“,”, “, “, $str);
$str = str_replace(“`) ) ENGINE=InnoDB “, “`) ) ENGINE=InnoDB “, $str);
$str .=”;”;
//$str = $str.”;–– Dumping data for table `{$table}`–”;
$fields[$rows[‘Table’]] = $str;
}
return $fields;
}