在GitHub上发现的一个PHP的MySQL数据库备份还原类,写的不错,目测用的人似乎不多,好东西就推广一下哈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | <?php /** * @author yanue * @copyright Copyright (c) 2012 yanue.net * @link http://yanue.net/archives/174.html * @version 1.1 * 创建时间: 2012年5月21日 * 说明:分卷文件是以_v1.sql为结尾(20120522021241_all_v1.sql) * 功能:实现mysql数据库分卷备份,选择表进行备份,实现单个sql文件及分卷sql导入 * 使用方法: * * ------1. 数据库备份(导出)------------------------------------------------------------ //分别是主机,用户名,密码,数据库名,数据库编码 $db = new DBManage ( 'localhost', 'root', 'root', 'test', 'utf8' ); // 参数:备份哪个表(可选),备份目录(可选,默认为backup),分卷大小(可选,默认2000,即2M) $db->backup (); * ------2. 数据库恢复(导入)------------------------------------------------------------ //分别是主机,用户名,密码,数据库名,数据库编码 $db = new DBManage ( 'localhost', 'root', 'root', 'test', 'utf8' ); //参数:sql文件 $db->restore ( './backup/20120516211738_all_v1.sql'); *---------------------------------------------------------------------- */ class DbManage { var $db; // 数据库连接 var $database; // 所用数据库 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) { $dir = $dir ? $dir : './backup/'; // 创建目录 if (! is_dir ( $dir )) { mkdir ( $dir, 0777, true ) or die ( '创建文件夹失败' ); } $size = $size ? $size : 2048; $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='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='imp'>" .$dir . $filename ."</span> ]"); } else { $this->_showMsg("备份卷-<b>" . $p . "</b>-失败<br />"); return false; } } $this->_showMsg("恭喜您! <span class='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='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='imp'>".$dir.$filename."</span> ]"); } else { $this->_showMsg("卷-<b>" . $p . "</b>-备份失败",true); return false; } } $this->_showMsg("恭喜您! <span class='imp'>备份成功</span>"); } } // 及时输出信息 private function _showMsg($msg,$err=false){ $err = $err ? "<span class='err'>ERROR:</span>" : '' ; echo "<p class='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='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='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 ); } } } |
半叶寒羽的DBManage class,出处:https://github.com/yanue/Dbmanage
不错的php代码,收藏下!我也在用wp感觉可扩展性很强,用这个类写一个备份插件不错哦!希望做个友情链接www.pcfox.com.cn
网站挺不错,赞一个支持一下!!!
网站弄得挺不错,支持一下!!!!
真心看不懂,RSS里天天盯着你的博客标题,一旦变粗,立即就进来….
不一定要看懂,会用就行了,很方便.