The code is as follows:
<?<span>php </span><span>/*</span><span> * Created on 2014 * Link for 527891885@qq.com * This is seocheck backup class </span><span>*/</span> <span>class</span><span> DbBackUp { </span><span>private</span> <span>$conn</span><span>; </span><span>private</span> <span>$dbName</span><span>; </span><span>private</span> <span>$host</span><span>; </span><span>private</span> <span>$tag</span> = '_b'<span>; </span><span>//</span><span>构造方法 链接数据库</span> <span>public</span> <span>function</span> __construct(<span>$host</span>='localhost', <span>$dbUser</span>='root', <span>$dbPwd</span>='', <span>$dbName</span>="seocheck", <span>$charset</span>='utf8'<span>) { @</span><span>ob_start</span><span>(); @</span><span>set_time_limit</span>(0<span>); </span><span>$this</span>->conn = <span>mysql_connect</span>(<span>$host</span>, <span>$dbUser</span>, <span>$dbPwd</span>, <span>true</span><span>); </span><span>if</span>(!<span>$this</span>->conn) <span>die</span>("数据库系统连接失败!"<span>); </span><span>mysql_query</span>("set names ".<span>$charset</span>, <span>$this</span>-><span>conn); </span><span>mysql_select_db</span>(<span>$dbName</span>, <span>$this</span>->conn) or <span>die</span>("数据库连接失败!"<span>); </span><span>$this</span>->host = <span>$host</span><span>; </span><span>$this</span>->dbName = <span>$dbName</span><span>; } </span><span>//</span><span>获取数据库所有表名</span> <span>public</span> <span>function</span><span> getTableNames () { </span><span>$tables</span> = <span>array</span><span>(); </span><span>$result</span> = <span>mysql_list_tables</span>(<span>$this</span>->dbName, <span>$this</span>-><span>conn); </span><span>if</span>(!<span>$result</span>) <span>die</span>('MySQL Error: ' . <span>mysql_error</span><span>()); </span><span>while</span>(<span>$row</span> = <span>mysql_fetch_row</span>(<span>$result</span><span>)) { </span><span>$tables</span>[] = <span>$row</span>[0<span>]; } </span><span>return</span> <span>$tables</span><span>; } </span><span>//</span><span>获取数据库表的字段信息</span> <span>public</span> <span>function</span> getFieldsByTable (<span>$table</span><span>) { </span><span>$fields</span> = <span>array</span><span>(); </span><span>$str</span> = ''<span>; </span><span>$res</span> = <span>mysql_query</span>("SHOW CREATE TABLE `{<span>$table</span>}`", <span>$this</span>-><span>conn); </span><span>if</span>(!<span>$res</span>) <span>die</span>('MySQL Error: ' . <span>mysql_error</span><span>()); </span><span>while</span>(<span>$rows</span> = <span>mysql_fetch_assoc</span>(<span>$res</span><span>)) { </span><span>$str</span> = <span>str_replace</span>("CREATE TABLE `{<span>$table</span>}` (", "", <span>$rows</span>['Create Table']);<span>//</span><span>DROP TABLE IF EXISTS `{$table}`\n</span> <span>$str</span> = "--\n-- Table structure for table `{<span>$table</span>}`\n--\n\nCREATE TABLE IF NOT EXISTS `{<span>$table</span>}` ( ".<span>$str</span><span>; </span><span>$str</span> = <span>str_replace</span>(",", ", ", <span>$str</span><span>); </span><span>$str</span> = <span>str_replace</span>("`) ) ENGINE=InnoDB ", "`)\n ) ENGINE=InnoDB ", <span>$str</span><span>); </span><span>$str</span> .=";\n\n"<span>; </span><span>//</span><span>$str = $str.";\n\n--\n-- Dumping data for table `{$table}`\n--\n\n";</span> <span>$fields</span>[<span>$rows</span>['Table']] = <span>$str</span><span>; } </span><span>return</span> <span>$fields</span><span>; } www</span>.jbxue.<span>com </span><span>//</span><span>获取表中的数据</span> <span>public</span> <span>function</span> getDataByTable(<span>$table</span><span>) { </span><span>$data</span> = <span>array</span><span>(); </span><span>$str</span> = ''<span>; </span><span>$res</span> = <span>mysql_query</span>("SELECT * FROM `{<span>$table</span>}`", <span>$this</span>-><span>conn); </span><span>if</span>(!<span>$res</span>) <span>die</span>('MySQL Error: ' . <span>mysql_error</span><span>()); </span><span>while</span>(<span>$rows</span> = <span>mysql_fetch_assoc</span>(<span>$res</span><span>)) { </span><span>if</span>(!<span>empty</span>(<span>$rows</span><span>)) { </span><span>$data</span>[] = <span>$rows</span><span>; } } </span><span>$keys</span> = <span>array_keys</span>(<span>$data</span>[0<span>]); </span><span>foreach</span> (<span>$keys</span> <span>as</span> <span>$k</span>=><span>$v</span><span>) { </span><span>$keys</span>[<span>$k</span>] = '`'.<span>$v</span>.'`'<span>; } </span><span>$key</span> = <span>join</span>(', ', <span>$keys</span><span>); </span><span>$str</span> = "INSERT INTO `{<span>$table</span>}` ({<span>$key</span>}) VALUES\n"<span>; </span><span>foreach</span> (<span>$data</span> <span>as</span> <span>$k</span>=><span>$v</span><span>) { </span><span>$str</span>.="("<span>; </span><span>while</span> (<span>list</span>(<span>$key</span>, <span>$val</span>) = <span>each</span>(<span>$v</span><span>)) { </span><span>if</span>(!<span>is_numeric</span>(<span>$val</span><span>)) { </span><span>$str</span>.= "'".<span>$val</span>."', "<span>; } </span><span>else</span><span> { </span><span>$str</span>.= <span>$val</span>.', '<span>; } } </span><span>$str</span> = <span>substr</span>(<span>$str</span>, 0, -2);<span>//</span><span> 后边有空格 所以从-2 开始截取</span> <span>if</span>(<span>$k</span>+1 == <span>count</span>(<span>$data</span><span>)) { </span><span>$str</span>.=");\n\n-- --------------------------------------------------------\n\n"<span>; } </span><span>else</span><span> { </span><span>$str</span>.="),\n"<span>; } } </span><span>return</span> <span>$str</span><span>; } </span><span>//</span><span>备份数据库</span> <span>public</span> <span>function</span> getBackUpDataByTable (<span>$tables</span>, <span>$path</span>='', <span>$fileName</span> = 'seocheck', <span>$subsection</span> = '2'<span>) { </span><span>if</span>(<span>empty</span>(<span>$tables</span>)) <span>$this</span>->_showMsg('未能指定要备份的表!!!', <span>true</span><span>); </span><span>$page</span> = 0;<span>//</span><span>卷数</span> <span>$path</span> = <span>empty</span>(<span>$path</span>) ? <span>$_SERVER</span>['DOCUMENT_ROOT'].'/core/Runtime/Data/'.<span>$fileName</span>.'Demo/' : <span>$path</span><span>; </span><span>if</span>(!<span>file_exists</span>(<span>$path</span><span>)) { </span><span>mkdir</span>(<span>$path</span>, 0777, <span>true</span><span>); } www</span>.jbxue.<span>com </span><span>$mysql_info</span> = <span>$this</span>-><span>_retrieve(); </span><span>$fieldsByTable</span> = <span>array</span><span>(); </span><span>if</span>(<span>is_array</span>(<span>$tables</span><span>)) { </span><span>$this</span>->_showMsg('开始备份,数据正在初始化中,请勿关闭浏览器...'<span>); </span><span>$fw</span> = <span>$this</span>->writeFileByBackUpData(<span>$path</span>.<span>$this</span>->dbName.'_table.sql', <span>$mysql_info</span>, <span>$method</span>="ab+"<span>); </span><span>if</span>(<span>$fw</span> !== <span>false</span><span>) { </span><span>$this</span>->_showMsg('备份数据库基本信息成功。。。'<span>); } </span><span>foreach</span> (<span>$tables</span> <span>as</span> <span>$table</span><span>) { </span><span>$tableInfo</span> = <span>$this</span>->getFieldsByTable(<span>$table</span><span>); </span><span>if</span>(!<span>empty</span>(<span>$tableInfo</span><span>)) { </span><span>$this</span>->_showMsg('获取表['.<span>$table</span>.']结构成功。。。'<span>); </span><span>$fw</span> = <span>$this</span>->writeFileByBackUpData(<span>$path</span>.<span>$this</span>->dbName.'_table.sql', <span>$tableInfo</span>[<span>$table</span>], <span>$method</span>="ab+"<span>); </span><span>if</span>(<span>$fw</span> === <span>false</span><span>) { </span><span>$this</span>->_showMsg('备份表['.<span>$table</span>.']结构失败。。。', <span>true</span><span>); } </span><span>else</span><span> { </span><span>$this</span>->_showMsg('备份表['.<span>$table</span>.']结构成功,开始获取数据。。。'<span>); }; } </span><span>else</span><span> { </span><span>$this</span>->_showMsg('获取数据库['.<span>$this</span>->dbName.']表结构失败,请稍后再试!。。。', <span>true</span><span>); } </span><span>$this</span>->_insertSqlByTableForAll(<span>$path</span>, <span>$table</span>, <span>$subsection</span><span>); } } </span><span>else</span><span> { </span><span>$this</span>->_showMsg('开始备份,数据正在初始化中,请勿关闭浏览器...'<span>); </span><span>$tableInfo</span> = <span>$this</span>->getFieldsByTable(<span>$tables</span><span>); </span><span>if</span>(!<span>empty</span>(<span>$tableInfo</span><span>)) { </span><span>$this</span>->_showMsg('获取表['.<span>$tables</span>.']结构成功。。。'<span>); </span><span>$fw</span> = <span>$this</span>->writeFileByBackUpData(<span>$path</span>.<span>$this</span>->dbName.'_'.<span>$tables</span>.'_table.sql', <span>$mysql_info</span>.<span>$tableInfo</span>[<span>$tables</span><span>]); </span><span>if</span>(<span>$fw</span> === <span>false</span><span>) { </span><span>$this</span>->_showMsg('备份表['.<span>$tables</span>.']结构失败。。。', <span>true</span><span>); } </span><span>else</span><span> { </span><span>$this</span>->_showMsg('备份表['.<span>$tables</span>.']结构成功,开始获取数据。。。'<span>); } } </span><span>else</span><span> { </span><span>$this</span>->_showMsg('获取表['.<span>$tables</span>.']结构失败,请稍后再试!。。。', <span>true</span><span>); } </span><span>$res</span> = <span>$this</span>->_insertSqlByTableForAll(<span>$path</span>, <span>$tables</span>, <span>$subsection</span><span>); } } </span><span>//</span><span>数据库基本信息</span> <span>private</span> <span>function</span><span> _retrieve() { </span><span>$backUp</span> = ''<span>; </span><span>$backUp</span> .= '--' . "\n"<span>; </span><span>$backUp</span> .= '-- MySQL database dump' . "\n"<span>; </span><span>$backUp</span> .= '-- Created by DbBackUp class, Power By chujiu. ' . "\n"<span>; </span><span>$backUp</span> .= '--' . "\n"<span>; </span><span>$backUp</span> .= '-- 主机: ' . <span>$this</span>->host . "\n"<span>; </span><span>$backUp</span> .= '-- 生成日期: ' . <span>date</span> ( 'Y' ) . ' 年 ' . <span>date</span> ( 'm' ) . ' 月 ' . <span>date</span> ( 'd' ) . ' 日 ' . <span>date</span> ( 'H:i' ) . "\n"<span>; </span><span>$backUp</span> .= '-- MySQL版本: ' . <span>mysql_get_server_info</span> () . "\n"<span>; </span><span>$backUp</span> .= '-- PHP 版本: ' . <span>phpversion</span> () . "\n"<span>; </span><span>$backUp</span> .= "\n\n"<span>; </span><span>$backUp</span> .= "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';\n"<span>; </span><span>$backUp</span> .= "SET time_zone = '+00:00';\n\n"<span>; </span><span>$backUp</span> .= "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n"<span>; </span><span>$backUp</span> .= "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n"<span>; </span><span>$backUp</span> .= "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n"<span>; </span><span>$backUp</span> .= "/*!40101 SET NAMES utf8*/;\n\n"<span>; </span><span>$backUp</span> .= "--\n-- Database: `{<span>$this</span>->dbName}`\n--\n\n-- --------------------------------------------------------\n\n"<span>; </span><span>return</span> <span>$backUp</span><span>; } </span><span>/*</span><span>* * 插入单条记录 * * @param string $row </span><span>*/</span> <span>private</span> <span>function</span> _insertSql(<span>$row</span>, <span>$table</span><span>) { </span><span>//</span><span> sql字段逗号分割</span> <span>$insert</span> = ''<span>; </span><span>$insert</span> .= "INSERT INTO `" . <span>$table</span> . "` VALUES("<span>; </span><span>foreach</span>(<span>$row</span> <span>as</span> <span>$key</span>=><span>$val</span><span>) { </span><span>$insert</span> .= "'".<span>$val</span>."',"<span>; } </span><span>$insert</span> = <span>substr</span>(<span>$insert</span>, 0 ,-1<span>); </span><span>$insert</span> .= ");" . "\n"<span>; </span><span>return</span> <span>$insert</span><span>; } </span><span>/*</span><span>* * 生成一个表的inser语句 * @param string $table * @param string $subsection 分卷大小 </span><span>*/</span> <span>private</span> <span>function</span> _insertSqlByTableForAll(<span>$path</span>, <span>$table</span>, <span>$subsection</span><span>) { </span><span>$i</span> = 0<span>; </span><span>$insertSqlByTable</span> = ''<span>; </span><span>$res</span> = <span>mysql_query</span>("SELECT * FROM `{<span>$table</span>}`", <span>$this</span>-><span>conn); </span><span>if</span>(!<span>$res</span>) <span>die</span>('MySQL Error: ' . <span>mysql_error</span><span>()); </span><span>while</span>(<span>$rows</span> = <span>mysql_fetch_assoc</span>(<span>$res</span><span>)) { </span><span>$insertSqlByTable</span> .= <span>$this</span>->_insertSql(<span>$rows</span>, <span>$table</span><span>); </span><span>$size</span> = <span>strlen</span>(<span>$insertSqlByTable</span><span>); </span><span>if</span>(<span>$size</span> > <span>$subsection</span>*1024*1024<span>) { </span><span>$fw</span> = <span>$this</span>->writeFileByBackUpData(<span>$path</span>.<span>$table</span>.<span>$i</span>.<span>$this</span>->tag.'.sql', <span>$insertSqlByTable</span><span>); </span><span>if</span>(<span>$fw</span> === <span>false</span>) <span>$this</span>->_showMsg('数据库表['.<span>$table</span>.'],卷 '.<span>$i</span>.' 写入文件失败,请稍后再试!!!',<span>true</span><span>); </span><span>$this</span>->_showMsg('数据库表['.<span>$table</span>.'],卷 '.<span>$i</span>.' 备份成功!备份文件:[ '.<span>$path</span>.<span>$table</span>.<span>$i</span>.<span>$this</span>->tag.'.sql ]'<span>); </span><span>$insertSqlByTable</span> = ''<span>; </span><span>$i</span>+=1<span>; } } </span><span>//</span><span> insertSqlByTable大小不够分卷大小</span> <span>if</span> (<span>$insertSqlByTable</span> != ""<span>) { </span><span>$fw</span> = <span>$this</span>->writeFileByBackUpData(<span>$path</span>.<span>$table</span>.<span>$this</span>->tag.'.sql', <span>$insertSqlByTable</span><span>); </span><span>if</span>(<span>$fw</span> === <span>false</span>) <span>$this</span>->_showMsg('数据库表['.<span>$table</span>.']写入文件失败,请稍后再试!!!备份文件:[ '.<span>$path</span>.<span>$table</span>.<span>$this</span>->tag.'.sql ]',<span>true</span><span>); </span><span>$this</span>->_showMsg('数据库表['.<span>$table</span>.'] 备份成功!备份文件:[ '.<span>$path</span>.<span>$table</span>.<span>$this</span>->tag.'.sql ]'<span>); } </span><span>$this</span>->_showMsg('数据库表['.<span>$table</span>.']全部备份成功!'<span>); } </span><span>//</span><span> 写入文件</span> <span>public</span> <span>function</span> writeFileByBackUpData(<span>$fileName</span>, <span>$data</span>, <span>$method</span>="rb+", <span>$iflock</span>=1, <span>$check</span>=1, <span>$chmod</span>=1<span>){ </span><span>$check</span> && @<span>strpos</span>(<span>$fileName</span>, '..')!==<span>false</span> && <span>exit</span>('Forbidden'<span>); @</span><span>touch</span>(<span>$fileName</span><span>); </span><span>$handle</span> = @<span>fopen</span>(<span>$fileName</span>, <span>$method</span><span>); </span><span>if</span>(<span>$iflock</span><span>) { @</span><span>flock</span>(<span>$handle</span>,<span>LOCK_EX); } </span><span>$fw</span> = @<span>fwrite</span>(<span>$handle</span>,<span>$data</span><span>); </span><span>if</span>(<span>$method</span> == "rb+") <span>ftruncate</span>(<span>$handle</span>, <span>strlen</span>(<span>$data</span><span>)); </span><span>fclose</span>(<span>$handle</span><span>); </span><span>$chmod</span> && @<span>chmod</span>(<span>$fileName</span>,0777<span>); </span><span>return</span> <span>$fw</span><span>; } </span><span>/*</span><span>* * path: 生成压缩包的路径 * fileName : 要压缩的文件名 通常和path 同一目录 </span><span>*/</span> <span>public</span> <span>function</span> createZipByBackUpFile(<span>$path</span><span>) { </span><span>$db_base_files</span> = <span>$this</span>->getFileByBackUpDir(<span>$path</span><span>); </span><span>if</span>(!<span>empty</span>(<span>$db_base_files</span><span>)) { </span><span>$zip</span> = <span>new</span><span> ZipArchive; </span><span>if</span>(<span>$zip</span>->open(<span>$path</span>.<span>$this</span>->dbName.<span>date</span>('Ymd').'.zip', ZipArchive::CREATE | ZIPARCHIVE::OVERWRITE) !== <span>true</span><span>) </span><span>die</span> ("cannot open".<span>$this</span>->dbName.<span>date</span>('Ymd')."zip for writing."<span>); </span><span>foreach</span> (<span>$db_base_files</span> <span>as</span> <span>$key</span> => <span>$value</span><span>) { </span><span>if</span>(<span>is_file</span>(<span>$value</span><span>)) { </span><span>$file_name</span> = <span>basename</span>(<span>$value</span><span>); </span><span>$info</span>[] = <span>$zip</span>->addFile(<span>$value</span>, <span>$file_name</span>);<span>//</span><span> 避免压缩包里有文件的路径</span> <span> } } </span><span>$zip</span>-><span>close(); </span><span>if</span>(<span>file_exists</span>(<span>$path</span>.<span>$this</span>->dbName.<span>date</span>('Ymd').'.zip'<span>)) </span><span>foreach</span> (<span>$db_base_files</span> <span>as</span> <span>$val</span><span>) { </span><span>unlink</span>(<span>$val</span><span>); } </span><span>if</span>(<span>count</span>(<span>array_filter</span>(<span>$info</span>)) > 0) <span>return</span> <span>true</span><span>; } </span><span>return</span> <span>false</span><span>; } </span><span>//</span><span>获取文件</span> <span>public</span> <span>function</span> getFileByBackUpDir(<span>$path</span><span>) { </span><span>$info</span> = <span>array</span><span>(); </span><span>$db_base_files</span> = <span>array</span><span>(); </span><span>if</span>( @<span>file_exists</span>(<span>$path</span>) && <span>is_dir</span>(<span>$path</span><span>) ) { </span><span>if</span> (<span>$dh</span> = <span>opendir</span>(<span>$path</span><span>)) { </span><span>while</span> ((<span>$file</span> = <span>readdir</span>(<span>$dh</span>)) !== <span>false</span><span>) { </span><span>if</span>(<span>$file</span> != '.' && <span>$file</span> != '..'<span>) { </span><span>if</span>( <span>strripos</span>(<span>$file</span>, 'seocheck') !== <span>false</span><span> ) { </span><span>$db_base_files</span>[] = <span>$path</span>.<span>$file</span><span>; } } } </span><span>closedir</span>(<span>$dh</span><span>); } } </span><span>return</span> <span>$db_base_files</span><span>; } </span><span>/*</span><span>* * @path: 生成压缩包的路径 * @fileName : 要解压的文件名 默认解压到path 目录 </span><span>*/</span> <span>public</span> <span>function</span> uncompressZip(<span>$path</span>, <span>$zipName</span><span>) { </span><span>$path</span> = <span>empty</span>(<span>$path</span>) ? <span>$_SERVER</span>['DOCUMENT_ROOT'].'/core/Runtime/Data/' : <span>$path</span><span>; </span><span>$zip</span> = <span>new</span><span> ZipArchive; </span><span>if</span> (<span>$zip</span>->open(<span>$path</span>.<span>$zipName</span>) === <span>TRUE</span><span>) { </span><span>$zip</span>->extractTo(<span>$path</span><span>); </span><span>$zip</span>-><span>close(); </span><span>return</span> <span>true</span><span>; } </span><span>else</span><span> { </span><span>return</span> <span>false</span><span>; } } </span><span>//</span><span>导入数据库</span> <span>public</span> <span>function</span><span> importingDataBySqlFile () { } </span><span>//</span><span> 及时输出信息</span> <span>private</span> <span>function</span> _showMsg(<span>$msg</span>,<span>$err</span>=<span>false</span><span>){ </span><span>if</span>(<span>$err</span> === <span>true</span><span>) { </span><span>echo</span> "<p style='font-size:14px;'><span style='color:red;'>ERROR: --- " . <span>$msg</span> . "</span></p>";<span>exit</span><span>; } </span><span>echo</span> "<p style='font-size:14px;'><span style='color:green;'>OK: --- " . <span>$msg</span> . "</span></p>"<span>; } </span><span>//</span><span> 锁定数据库,以免备份或导入时出错</span> <span>private</span> <span>function</span> lock(<span>$table</span>, <span>$op</span> = "WRITE"<span>) { </span><span>if</span> (<span>mysql_query</span> ( "lock tables " . <span>$table</span> . " " . <span>$op</span><span> )) </span><span>return</span> <span>true</span><span>; </span><span>else</span> <span>return</span> <span>false</span><span>; } </span><span>//</span><span> 解锁</span> <span>private</span> <span>function</span><span> unlock() { </span><span>if</span> (<span>mysql_query</span> ( "unlock tables"<span> )) </span><span>return</span> <span>true</span><span>; </span><span>else</span> <span>return</span> <span>false</span><span>; } </span><span>//</span><span> 析构</span> <span>public</span> <span>function</span><span> __destruct() { </span><span>if</span>(<span>$this</span>-><span>conn){ </span><span>mysql_query</span> ( "unlock tables", <span>$this</span>-><span>conn ); </span><span>mysql_close</span> ( <span>$this</span>-><span>conn ); } } } </span>?>