php mysql备份还原类_PHP实现MYSQL备份还原
/**
* mysql備份*/
classMysqlBackup {function __construct($filename, $config) {$this->setFile($filename);$this->con($config);
}/**
* 輸出信息*/
private function info($code, $msg) {$json = json_encode(array('code' => $code, 'msg' => $msg),JSON_UNESCAPED_UNICODE);exit($json);
}/**
* --------------------------------------------------
* 數據庫
* --------------------------------------------------*/
private $dbHandle;private $database;/**
* 連接
* @param $config array(host,user,password,database);*/
private function con($config) {$this->dbHandle = mysqli_connect($config['host'], $config['user'], $config['password'], $config['database']);if ($this->dbHandle->connect_errno) {$this->info(-1, 'MySQL連接錯誤:' . $this->dbHandle->connect_error);
}$this->dbHandle->set_charset('utf8');$this->database = $config['database'];
}/**
* 自定義SQL
* @param $sql 條件語句
* @param $bind 條件綁定
* @param $resType 1:select,2:insert,3:delete/update*/
private function sql($sql, $resType = 1) {//執行
$result = $this->dbHandle->query($sql);//返回
if ($resType == 1) {return $result->fetch_all(MYSQLI_ASSOC);
}elseif ($resType == 2) {return $this->dbHandle->insert_id;
}elseif ($resType == 3) {return $this->dbHandle->affected_rows;
}
}/**
* 多語句SQL*/
private function sql_multi($sql) {$result = $this->dbHandle->multi_query($sql);if ($result) {while ($this->dbHandle->more_results() && $this->dbHandle->next_result()) {
}return true;
}else{return false;
}
}/**
* --------------------------------------------------
* 文件操作
* --------------------------------------------------*/
private $filename;/**
* 設置文件*/
private function setFile($filename) {$this->filename = $filename;
}/**
* 寫入*/
private function write($string) {//句柄
static $handle = null;if (!$handle) {$handle = fopen($this->filename, 'w+');
}//寫入
$res = fwrite($handle, $string);//返回
if ($res === false) {return false;
}return true;
}/**
* 讀取*/
private functionread() {//句柄
static $handle = null;if (!$handle) {$handle = fopen($this->filename, 'r');
}//讀取
$res = fgets($handle);return substr($res, 0, -strlen($this->split));
}/**
* --------------------------------------------------
* 備份
* --------------------------------------------------*/
//SQL分隔符,不可更改
private $split = ";\r\n";/**
* 表列表*/
private functiongetTable() {//查詢
$sql = "SHOW TABLES";$res = $this->sql($sql);//錯誤
if (!$res) {$this->info(-2, '查詢表列表失敗!');
}//格式化
$data = array();foreach ($res as $v) {$v = array_values($v);$data[] = $v[0];
}return $data;
}/**
* 表結構*/
private function tableCreate($table) {$res = $this->sql("SHOW CREATE TABLE `$table`");//錯誤
if (!$res) {$this->info(-2, "查詢`$table`表結構失敗!");
}//格式化
$res = array_values($res[0]);$res = str_replace(array("\r", "\n"), array('', ''), $res[1]) . $this->split;$drop_sql = "DROP TABLE IF EXISTS `$table`" . $this->split;$res = $drop_sql . $res;//寫入
$res = $this->write($res);if (!$res) {$this->info(-3, "導出`$table`表結構失敗!");
}return true;
}/**
* 表數據
* 主鍵模式,表必須設置主鍵*/
private function tableDataP($table, $start = 0, $len = 200) {//主鍵名
static $pkey = array();if (empty($pkey[$table])) {$sql = "SELECT k.column_name FROM information_schema.table_constraints t JOIN information_schema.key_column_usage k USING( CONSTRAINT_NAME, table_schema, TABLE_NAME )
WHERE t.constraint_type = 'PRIMARY KEY' AND t.table_schema = '" . $this->database . "' AND t.table_name = '$table' LIMIT 1";$res = $this->sql($sql);if (!$res) {$this->info(-2, '不支持[主鍵模式]導出數據!');
}$pkey[$table] = $res[0]['column_name'];
}//查詢數據
$sql = "SELECT * FROM `$table` WHERE `{$pkey[$table]}` > $start LIMIT $len";$data = $this->sql($sql);//寫入
if ($data) {$sql = '';foreach ($data as $row) {$key = '';$value = '';foreach ($row as $k => $v) {$v = $this->dbHandle->real_escape_string($v);$key .= "`$k`,";$value .= "'$v',";
}$sql .= "INSERT INTO `$table`(" . substr($key, 0, -1) . ") VALUES (" . substr($value, 0, -1) . ")" . $this->split;
}$res = $this->write($sql);if (!$res) {$this->info(-3, "導出`$table`表數據失敗!");
}//返回
if (count($data) >= $len) {$start = $data[$len - 1][$pkey[$table]];return $start;
}
}//返回
return false;
}/**
* 表數據
* limit模式*/
private function tableDataL($table, $offset = 0, $len = 200) {//查詢數據
$sql = "SELECT * FROM `$table` LIMIT $len OFFSET $offset";$data = $this->sql($sql);//寫入
if ($data) {$sql = '';foreach ($data as $row) {$key = '';$value = '';foreach ($row as $k => $v) {$v = $this->dbHandle->real_escape_string($v);$key .= "`$k`,";$value .= "'$v',";
}$sql .= "INSERT INTO `$table`(" . substr($key, 0, -1) . ") VALUES (" . substr($value, 0, -1) . ")" . $this->split;
}$res = $this->write($sql);if (!$res) {$this->info(-3, "導出`$table`表數據失敗!");
}//返回
if (count($data) >= $len) {$offset += $len;return $offset;
}
}//返回
return false;
}/**
* 導出*/
functionexport() {$tables = $this->getTable();//表結構
foreach ($tables as $v) {$this->tableCreate($v);
}//表數據
foreach ($tables as $v) {$res = 0;for ($i = 0; $i > -1; $i++) {$res = $this->tableDataP($v, $res);if (!$res) {break;
}
}
}return true;
}/**
* --------------------------------------------------
* 導入
* --------------------------------------------------*/
functionimport() {for ($i = 0; $i > -1; $i++) {//批量讀取
$sql = '';for ($i = 0; $i < 50; $i++) {$res = $this->read();if (!$res) {break;
}$sql .= $res . ';';
}//批量執行
if ($sql) {$res = $this->sql_multi($sql);if (!$res) {echo $sql;$this->info(-3, '導入失敗');
}
}else{break;
}
}return true;
}
}
總結
以上是生活随笔為你收集整理的php mysql备份还原类_PHP实现MYSQL备份还原的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单了解oop编程思想和常见的几种设计模
- 下一篇: 咋样查mysql的url_eclipse