首页 » Web前端 » php的备份符技巧_一个PHP文件高机能的MYSQL数据库备份代码必备

php的备份符技巧_一个PHP文件高机能的MYSQL数据库备份代码必备

访客 2024-12-18 0

扫一扫用手机浏览

文章目录 [+]

class DbManage {

var $db; // 数据库连接

php的备份符技巧_一个PHP文件高机能的MYSQL数据库备份代码必备

var $database; // 所用数据库

php的备份符技巧_一个PHP文件高机能的MYSQL数据库备份代码必备
(图片来自网络侵删)

var $sqldir; // 数据库备份文件夹

// 换行符

private $ds = \"大众\n\公众;

// 存储SQL的变量

public $sqlContent = \"大众\公众;

// 每条sql语句的结尾符

public $sqlEnd = \"大众;\"大众;

/

初始化

@param string $host

@param string $username

@param string $password

@param string $database

@param string $charset

/

function __construct($host = 'localhost', $username = 'root', $password = '', $database = 'test', $charset = 'utf8') {

$this->host = $host;

$this->username = $username;

$this->password = $password;

$this->database = $database;

$this->charset = $charset;

set_time_limit(0);//无韶光限定

@ob_end_flush();

// 连接数据库

$this->db = @mysql_connect ( $this->host, $this->username, $this->password ) or die( '<p class=\"大众dbDebug\公众><span class=\公众err\公众>Mysql Connect Error : </span>'.mysql_error().'</p>');

// 选择利用哪个数据库

mysql_select_db ( $this->database, $this->db ) or die('<p class=\"大众dbDebug\"大众><span class=\"大众err\"大众>Mysql Connect Error:</span>'.mysql_error().'</p>');

// 数据库编码办法

mysql_query ( 'SET NAMES ' . $this->charset, $this->db );

}

/

新增查询数据库表

/

function getTables() {

$res = mysql_query ( \公众SHOW TABLES\"大众 );

$tables = array ();

while ( $row = mysql_fetch_array ( $res ) ) {

$tables [] = $row [0];

}

return $tables;

}

/

------------------------------------------数据库备份start----------------------------------------------------------

/

/

数据库备份

参数:备份哪个表(可选),备份目录(可选,默认为backup),分卷大小(可选,默认2000,即2M)

@param $string $dir

@param int $size

@param $string $tablename

/

function backup($tablename = '', $dir='./', $size=20480) {

$dir = $dir ? $dir : './';

// 创建目录

if (! is_dir ( $dir )) {

@mkdir ( $dir, 0777, true ) or die ( '创建文件夹失落败' );

}

$size = $size ? $size : 102420;

$sql = '';

// 只备份某个表

if (! empty ( $tablename )) {

if(@mysql_num_rows(mysql_query(\"大众SHOW TABLES LIKE '\"大众.$tablename.\"大众'\"大众)) == 1) {

} else {

$this->_showMsg('表-<b>' . $tablename .'</b>-不存在,请检讨!
',true);

die();

}

$this->_showMsg('正在备份表 <span class=\"大众imp\"大众>' . $tablename.'</span>');

// 插入dump信息

$sql = $this->_retrieve ();

// 插入表构造信息

$sql .= $this->_insert_table_structure ( $tablename );

// 插入数据

$data = mysql_query ( \"大众select from \"大众 . $tablename );

// 文件名前面部分

$filename = date ( 'YmdHis' ) . \"大众_\"大众 . $tablename;

// 字段数量

$num_fields = mysql_num_fields ( $data );

// 第几分卷

$p = 1;

// 循环每条记录

while ( $record = mysql_fetch_array ( $data ) ) {

// 单条记录

$sql .= $this->_insert_record ( $tablename, $num_fields, $record );

// 如果大于分卷大小,则写入文件

if (strlen ( $sql ) >= $size 1024) {

$file = $filename . \"大众_v\"大众 . $p . \公众.sql\公众;

if ($this->_write_file ( $sql, $file, $dir )) {

$this->_showMsg(\"大众表-<b>\公众 . $tablename . \"大众</b>-卷-<b>\"大众 . $p . \公众</b>-数据备份完成,备份文件 [ <span class='01a5064d1c6f51a3 imp'>\公众 .$dir . $file .\"大众</span> ]\公众);

} else {

$this->_showMsg(\"大众备份表 -<b>\公众 . $tablename . \公众</b>- 失落败\"大众,true);

return false;

}

// 下一个分卷

$p ++;

// 重置$sql变量为空,重新打算该变量大小

$sql = \公众\"大众;

}

}

// 及时打消数据

unset($data,$record);

// sql大小不足分卷大小

if ($sql != \公众\"大众) {

$filename .= \"大众_v\"大众 . $p . \"大众.sql\公众;

if ($this->_write_file ( $sql, $filename, $dir )) {

$this->_showMsg( \"大众表-<b>\"大众 . $tablename . \"大众</b>-卷-<b>\"大众 . $p . \公众</b>-数据备份完成,备份文件 [ <span class='064d1c6f51a31e1e imp'>\"大众 .$dir . $filename .\"大众</span> ]\公众);

} else {

$this->_showMsg(\"大众备份卷-<b>\"大众 . $p . \"大众</b>-失落败<br />\"大众);

return false;

}

}

$this->_showMsg(\"大众恭喜您! <span class='1c6f51a31e1eed33 imp'>备份成功</span>\"大众);

} else {

$this->_showMsg('正在备份');

// 备份全部表

if ($tables = mysql_query ( \"大众show table status from \公众 . $this->database )) {

$this->_showMsg(\"大众读取数据库构造成功!
\公众);

} else {

$this->_showMsg(\"大众读取数据库构造失落败!
\"大众);

exit ( 0 );

}

// 插入dump信息

$sql .= $this->_retrieve ();

// 文件名前面部分

$filename = date ( 'YmdHis' ) . \公众_all\公众;

// 查出所有表

$tables = mysql_query ( 'SHOW TABLES' );

// 第几分卷

$p = 1;

// 循环所有表

while ( $table = mysql_fetch_array ( $tables ) ) {

// 获取表名

$tablename = $table [0];

// 获取表构造

$sql .= $this->_insert_table_structure ( $tablename );

$data = mysql_query ( \"大众select from \"大众 . $tablename );

$num_fields = mysql_num_fields ( $data );

// 循环每条记录

while ( $record = mysql_fetch_array ( $data ) ) {

// 单条记录

$sql .= $this->_insert_record ( $tablename, $num_fields, $record );

// 如果大于分卷大小,则写入文件

if (strlen ( $sql ) >= $size 1000) {

$file = $filename . \"大众_v\公众 . $p . \公众.sql\公众;

// 写入文件

if ($this->_write_file ( $sql, $file, $dir )) {

$this->_showMsg(\"大众-卷-<b>\"大众 . $p . \"大众</b>-数据备份完成,备份文件 [ <span class='51a31e1eed332ee5 imp'>\"大众.$dir.$file.\公众</span> ]\公众);

} else {

$this->_showMsg(\公众卷-<b>\"大众 . $p . \公众</b>-备份失落败!\"大众,true);

return false;

}

// 下一个分卷

$p ++;

// 重置$sql变量为空,重新打算该变量大小

$sql = \公众\"大众;

}

}

}

// sql大小不足分卷大小

if ($sql != \"大众\"大众) {

$filename .= \"大众_v\"大众 . $p . \公众.sql\"大众;

if ($this->_write_file ( $sql, $filename, $dir )) {

$this->_showMsg(\"大众-卷-<b>\公众 . $p . \公众</b>-数据备份完成,备份文件 [ <span class='1e1eed332ee5b8ac imp'>\"大众.$dir.$filename.\"大众</span> ]\公众);

} else {

$this->_showMsg(\"大众卷-<b>\"大众 . $p . \"大众</b>-备份失落败\"大众,true);

return false;

}

}

$this->_showMsg(\"大众恭喜您! <span class='ed332ee5b8ac25a9 imp'>备份成功</span>\公众);

}

}

// 及时输出信息

private function _showMsg($msg,$err=false){

$err = $err ? \公众<span class='2ee5b8ac25a93fee err'>ERROR:</span>\"大众 : '' ;

echo \公众<p class='c2a98249f388dcea dbDebug'>\公众.$err . $msg.\公众</p>\"大众;

flush();

}

/

插入数据库备份根本信息

@return string

/

private function _retrieve() {

$value = '';

$value .= '--' . $this->ds;

$value .= '-- MySQL database dump' . $this->ds;

$value .= '-- Created by DbManage class, Power By yanue. ' . $this->ds;

$value .= '-- http://yanue.net ' . $this->ds;

$value .= '--' . $this->ds;

$value .= '-- 主机: ' . $this->host . $this->ds;

$value .= '-- 天生日期: ' . date ( 'Y' ) . ' 年 ' . date ( 'm' ) . ' 月 ' . date ( 'd' ) . ' 日 ' . date ( 'H:i' ) . $this->ds;

$value .= '-- MySQL版本: ' . mysql_get_server_info () . $this->ds;

$value .= '-- PHP 版本: ' . phpversion () . $this->ds;

$value .= $this->ds;

$value .= '--' . $this->ds;

$value .= '-- 数据库: `' . $this->database . '`' . $this->ds;

$value .= '--' . $this->ds . $this->ds;

$value .= '-- -------------------------------------------------------';

$value .= $this->ds . $this->ds;

return $value;

}

/

插入表构造

@param unknown_type $table

@return string

/

private function _insert_table_structure($table) {

$sql = '';

$sql .= \"大众--\公众 . $this->ds;

$sql .= \"大众-- 表的构造\"大众 . $table . $this->ds;

$sql .= \"大众--\"大众 . $this->ds . $this->ds;

// 如果存在则删除表

$sql .= \公众DROP TABLE IF EXISTS `\"大众 . $table . '`' . $this->sqlEnd . $this->ds;

// 获取详细表信息

$res = mysql_query ( 'SHOW CREATE TABLE `' . $table . '`' );

$row = mysql_fetch_array ( $res );

$sql .= $row [1];

$sql .= $this->sqlEnd . $this->ds;

// 加上

$sql .= $this->ds;

$sql .= \公众--\公众 . $this->ds;

$sql .= \"大众-- 转存表中的数据 \"大众 . $table . $this->ds;

$sql .= \公众--\"大众 . $this->ds;

$sql .= $this->ds;

return $sql;

}

/

插入单条记录

@param string $table

@param int $num_fields

@param array $record

@return string

/

private function _insert_record($table, $num_fields, $record) {

// sql字段逗号分割

$insert = '';

$comma = \"大众\"大众;

$insert .= \"大众INSERT INTO `\"大众 . $table . \公众` VALUES(\"大众;

// 循环每个子段下面的内容

for($i = 0; $i < $num_fields; $i ++) {

$insert .= ($comma . \"大众'\"大众 . mysql_escape_string ( $record [$i] ) . \"大众'\"大众);

$comma = \"大众,\"大众;

}

$insert .= \"大众);\公众 . $this->ds;

return $insert;

}

/

写入文件

@param string $sql

@param string $filename

@param string $dir

@return boolean

/

private function _write_file($sql, $filename, $dir) {

$dir = $dir ? $dir : './backup/';

// 创建目录

if (! is_dir ( $dir )) {

mkdir ( $dir, 0777, true );

}

$re = true;

if (! @$fp = fopen ( $dir . $filename, \"大众w+\"大众 )) {

$re = false;

$this->_showMsg(\公众打开sql文件失落败!
\"大众,true);

}

if (! @fwrite ( $fp, $sql )) {

$re = false;

$this->_showMsg(\公众写入sql文件失落败,请文件是否可写\"大众,true);

}

if (! @fclose ( $fp )) {

$re = false;

$this->_showMsg(\公众关闭sql文件失落败!
\"大众,true);

}

return $re;

}

/

-------------------------------上:数据库导出-----------分割线----------下:数据库导入--------------------------------

/

/

导入备份数据

解释:分卷文件格式20120516211738_all_v1.sql

参数:文件路径(必填)

@param string $sqlfile

/

function restore($sqlfile) {

// 检测文件是否存在

if (! file_exists ( $sqlfile )) {

$this->_showMsg(\"大众sql文件不存在!
请检讨\公众,true);

exit ();

}

$this->lock ( $this->database );

// 获取数据库存储位置

$sqlpath = pathinfo ( $sqlfile );

$this->sqldir = $sqlpath ['dirname'];

// 检测是否包含分卷,将类似20120516211738_all_v1.sql从_v分开,有则解释有分卷

$volume = explode ( \"大众_v\"大众, $sqlfile );

$volume_path = $volume [0];

$this->_showMsg(\"大众请勿刷新及关闭浏览器以防止程序被中止,如有不慎!
将导致数据库构造受损\"大众);

$this->_showMsg(\"大众正在导入备份数据,请稍等!
\公众);

if (empty ( $volume [1] )) {

$this->_showMsg ( \"大众正在导入sql:<span class='8249f388dcea1e50 imp'>\公众 . $sqlfile . '</span>');

// 没有分卷

if ($this->_import ( $sqlfile )) {

$this->_showMsg( \"大众数据库导入成功!
\"大众);

} else {

$this->_showMsg('数据库导入失落败!
',true);

exit ();

}

} else {

// 存在分卷,则获取当前是第几分卷,循环实行余下分卷

$volume_id = explode ( \"大众.sq\"大众, $volume [1] );

// 当前分卷为$volume_id

$volume_id = intval ( $volume_id [0] );

while ( $volume_id ) {

$tmpfile = $volume_path . \公众_v\公众 . $volume_id . \公众.sql\"大众;

// 存在其他分卷,连续实行

if (file_exists ( $tmpfile )) {

// 实行导入方法

$this->msg .= \公众正在导入分卷 $volume_id :<span style='color:#f00;'>\"大众 . $tmpfile . '</span><br />';

if ($this->_import ( $tmpfile )) {

} else {

$volume_id = $volume_id ? $volume_id :1;

exit ( \"大众导入分卷:<span style='color:#f00;'>\公众 . $tmpfile . '</span>失落败!
可能是数据库构造已破坏!
请考试测验从分卷1开始导入' );

}

} else {

$this->msg .= \"大众此分卷备份全部导入成功!
<br />\"大众;

return;

}

$volume_id ++;

}

}if (empty ( $volume [1] )) {

$this->_showMsg ( \"大众正在导入sql:<span class='f388dcea1e505d89 imp'>\"大众 . $sqlfile . '</span>');

// 没有分卷

if ($this->_import ( $sqlfile )) {

$this->_showMsg( \"大众数据库导入成功!
\"大众);

} else {

$this->_showMsg('数据库导入失落败!
',true);

exit ();

}

} else {

// 存在分卷,则获取当前是第几分卷,循环实行余下分卷

$volume_id = explode ( \"大众.sq\公众, $volume [1] );

// 当前分卷为$volume_id

$volume_id = intval ( $volume_id [0] );

while ( $volume_id ) {

$tmpfile = $volume_path . \公众_v\"大众 . $volume_id . \"大众.sql\"大众;

// 存在其他分卷,连续实行

if (file_exists ( $tmpfile )) {

// 实行导入方法

$this->msg .= \"大众正在导入分卷 $volume_id :<span style='color:#f00;'>\公众 . $tmpfile . '</span><br />';

if ($this->_import ( $tmpfile )) {

} else {

$volume_id = $volume_id ? $volume_id :1;

exit ( \"大众导入分卷:<span style='color:#f00;'>\公众 . $tmpfile . '</span>失落败!
可能是数据库构造已破坏!
请考试测验从分卷1开始导入' );

}

} else {

$this->msg .= \"大众此分卷备份全部导入成功!
<br />\公众;

return;

}

$volume_id ++;

}

}

}

/

将sql导入到数据库(普通导入)

@param string $sqlfile

@return boolean

/

private function _import($sqlfile) {

// sql文件包含的sql语句数组

$sqls = array ();

$f = fopen ( $sqlfile, \"大众rb\公众 );

// 创建表缓冲变量

$create_table = '';

while ( ! feof ( $f ) ) {

// 读取每一行sql

$line = fgets ( $f );

// 这一步为了将创建表合成完全的sql语句

// 如果结尾没有包含';'(即为一个完全的sql语句,这里是插入语句),并且不包含'ENGINE='(即创建表的末了一句)

if (! preg_match ( '/;/', $line ) || preg_match ( '/ENGINE=/', $line )) {

// 将本次sql语句与创建表sql连接存起来

$create_table .= $line;

// 如果包含了创建表的末了一句

if (preg_match ( '/ENGINE=/', $create_table)) {

//实行sql语句创建表

$this->_insert_into($create_table);

// 清空当前,准备下一个表的创建

$create_table = '';

}

// 跳过本次

continue;

}

//实行sql语句

$this->_insert_into($line);

}

fclose ( $f );

return true;

}

//插入单条sql语句

private function _insert_into($sql){

if (! mysql_query ( trim ( $sql ) )) {

$this->msg .= mysql_error ();

return false;

}

}

/

-------------------------------数据库导入end---------------------------------

/

// 关闭数据库连接

private function close() {

mysql_close ( $this->db );

}

// 锁天命据库,以免备份或导入时出错

private function lock($tablename, $op = \"大众WRITE\"大众) {

if (mysql_query ( \"大众lock tables \公众 . $tablename . \"大众 \公众 . $op ))

return true;

else

return false;

}

// 解锁

private function unlock() {

if (mysql_query ( \"大众unlock tables\"大众 ))

return true;

else

return false;

}

// 析构

function __destruct() {

if($this->db){

mysql_query ( \公众unlock tables\"大众, $this->db );

mysql_close ( $this->db );

}

}

}

$db = new DbManage(\"大众localhost\公众,\"大众root\公众,\公众root\"大众,\公众dbname\"大众);

$db->backup(\"大众\"大众,\"大众\"大众,\"大众\公众);

?>

标签:

相关文章