<?php
/*
*数据库备份类。
*支持MYSQL
*作者:过了半天
*email:459889042@qq.com
*/
class bak{
private $host;
private $user;
private $pass;
private $db;
private $links;
private $url;
function __construct(){ //连接数据库。
$this->host = HOST;
$this->user = USER;
$this->pass = PASS;
$this->db = DBNAME;
$this->url = "../bak/".date("Y-m-d").".sql";
$this->links = mysql_connect($this->host,$this->user,$this->pass) or die(mysql_error()."无法连接数据库!");
$this->query("set names gbk");
}
function show_table(){ //读取数据库中所有的表名。
$res = mysql_list_tables($this->db);
for($i=0;$i<mysql_num_rows($res);$i++){
$array[] = mysql_tablename($res,$i);
}
mysql_free_result($res);
return $array;
}
private function show_num($table){ //取得表中所有的字段
$fields = mysql_list_fields($this->db, $table, $this->links);
$columns = mysql_num_fields($fields);
for ($i = 0; $i < $columns; $i++) {
$imp[] = mysql_field_name($fields, $i);
}
return implode(",",$imp);
}
private function frame_table($table){ //生成表结构语句。
$res = $this->fetch_row("SHOW CREATE TABLE ".$table);
return $res[1].";";
}
private function select_table($table){ //查询数据表里所有的数据
$res = $this->fetch_array("select * from ".$this->db.".".$table);
return $res;
}
private function contents($table){ //生成备份语句
$array = $this->select_table($table);
$res = "insert into ".$table."(".$this->show_num($table).")values".$this->fors($array).";\n";
return $res;
}
function content(){ //生成备份文件
$table = $this->show_table();
for($i=0;$i<count($table);$i++){
$this->write($this->content_show($this->frame_table($table[$i]),$this->contents($table[$i]),$table[$i]),$table[$i]);
}
}
function bak_set($table){
for($i=0;$i<count($table);$i++){
$this->write($this->content_show($this->frame_table($table[$i]),$this->contents($table[$i]),$table[$i]),$table[$i]);
}
}
private function fors($array){ //循环读取所有数据
for($i=0;$i<count($array);$i++){
$imp[] = $this->foreachs($array[$i],$num)."\n";
}
if(is_array($imp)){
return implode(",",$imp);
}else{
return $imp;
}
}
private function foreachs($array){ //对数组进行过滤组合
foreach($array as $key=>$value){
if(!eregi("^[0-9]",$key)){
$vals[] = $this->real_string($value);
}
}
return "('".implode("','",$vals)."')";
}
private function content_show($frame,$show,$table){
return "-- mysql_bak\n-- version 2.11.9.2\n-- http://www.phpmyadmin.net\n--\n-- 主机: ".$this->host."\n-- 生成日期:".date("Y年 m月 d日 H:i:s")."\n-- 服务器版本:".$this->get_info()."\n-- PHP 版本: ".phpversion()."\n\nSET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";\n\n\n\n/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n/*!40101 SET NAMES utf8 */;\n\n--\n-- 数据库: `".$this->db."`\n--\n\n-- --------------------------------------------------------\n\n--\n-- 表的结构 ".$table."\n--\n\n".$frame."\n\n\n--\n-- 导出表中的数据".$table."\n--\n\n".$show."\n\n-- --------------------------------------------------------";
}
private function query($sql){ //发送一条SQL语句
return mysql_query($sql);
}
private function fetch_row($sql){ //读取一行
$query = $this->query($sql);
return mysql_fetch_row($query);
}
private function fetch_array($sql){ //读取所有数据
$query=$this->query($sql);
$i = 0;
while($temp = mysql_fetch_array($query)){
$array[] = $temp;
$i++;
}
return $array;
}
private function get_info(){ //取得版本信息
return mysql_get_server_info();
}
private function real_string($content){ //特殊字符转义
return mysql_real_escape_string($content);
}
private function write($content,$table){
return file_put_contents(preg_replace("/bak\//i","bak\/".$table."_",$this->url),$content);
}
function __destruct(){ //关闭数据库
mysql_close($this->links);
}
}
?>