<?php
/**
* @author yanue
* @copyright Copyright (c) 2012 yanue.net
* @link http://yanue.net/archives/174.html
* @version 1.1
* 创建时间: 2012年5月21日
更新时间: 2012年10月6日
更新说明: 1.去除sql导入的时候排除sql文件里面的注释'-- ' 从而解决sql中单双引号不能导入
2.单行读取后的sql直接执行,避免重新将sql语句组合到数组中再从数组中读取导入sql,提高效率
* 说明:分卷文件是以_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 = "seacms".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 = "seacms".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
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
xhcms靶场源码,无密码,解压即用 (508个子文件)
bootstrap.css 95KB
ueditor.css 45KB
ueditor.min.css 34KB
style.css 32KB
font-awesome.css 22KB
video-js.css 22KB
prettyPhoto.css 19KB
image.css 19KB
jquery-ui.css 17KB
video.css 15KB
attachment.css 15KB
bootstrap-switch.css 14KB
seacms-style.css 12KB
video-js.min.css 11KB
fullcalendar.css 10KB
shCoreDefault.css 8KB
shCoreDefault.css 8KB
widgets.css 7KB
bootstrap-datetimepicker.min.css 6KB
scrawl.css 4KB
codemirror.css 3KB
charts.css 3KB
background.css 2KB
emotion.css 2KB
dialogbase.css 2KB
music.css 2KB
rateit.css 2KB
jquery.cleditor.css 1KB
edittable.css 1KB
template.css 1KB
webuploader.css 543B
help.css 395B
iframe.css 42B
vjs.eot 3KB
UEditorSnapscreen.exe 508KB
wface.gif 49KB
jxface2.gif 40KB
yface.gif 28KB
bface.gif 27KB
icons.gif 20KB
file-icons.gif 20KB
file-icons.gif 20KB
tface.gif 19KB
fface.gif 18KB
cface.gif 8KB
icons-all.gif 4KB
star.gif 2KB
videologo.gif 2KB
cancelbutton.gif 1KB
button-bg.gif 1KB
lock.gif 1KB
alignicon.gif 1KB
word.gif 1019B
icon_doc.gif 1012B
icon_psd.gif 1009B
icon_rar.gif 1007B
icon_xls.gif 1005B
icon_mv.gif 1001B
icon_ppt.gif 1001B
icon_pdf.gif 996B
icon_mp3.gif 986B
icon_txt.gif 970B
icon_jpg.gif 950B
icon_exe.gif 949B
icon_chm.gif 923B
delete.gif 752B
loading.gif 734B
icons.gif 453B
icons.gif 453B
icons.gif 453B
success.gif 445B
success.gif 445B
success.gif 445B
cursor_v.gif 370B
cursor_h.gif 253B
anchor.gif 184B
highlighted.gif 111B
unhighlighted.gif 111B
bg.gif 84B
pagebreak.gif 54B
spacer.gif 43B
0.gif 43B
wordimage.html 6KB
map.html 6KB
emotion.html 6KB
image.html 6KB
show.html 5KB
charts.html 5KB
link.html 4KB
insertframe.html 4KB
searchreplace.html 4KB
video.html 4KB
gmap.html 4KB
scrawl.html 4KB
background.html 3KB
help.html 3KB
edittable.html 2KB
webapp.html 2KB
attachment.html 2KB
snapscreen.html 2KB
共 508 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
RL-007
- 粉丝: 12
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功