// 设置SQL文件保存文件名
$filename=date("Y-m-d_H-i-s")."-".$cfg_dbname.".sql"
// 所保存的文件名
header("Content-disposition:filename=".$filename)
header("Content-type:application/octetstream")
header("Pragma:no-cache")
header("Expires:0")
// 获取当前页面文件路径,SQL文件就导出到此文件夹内
$tmpFile = (dirname(__FILE__))."\\".$filename
// 用MySQLDump命令导出数据库
exec("mysqldump -u$cfg_dbuser -p$cfg_dbpwd --default-character-set=utf8 $cfg_dbname > ".$tmpFile)
$file = fopen($tmpFile, "r") // 打开文件
echo fread($file,filesize($tmpFile))
fclose($file)
exit
?>
功能上有:
require_once("backdata.class.php")
$link
=
@mysql_connect("localhost","数据库名","密码")
or
die
('Could
not
connect
to
server.')
mysql_query("use
cms",$link)
mysql_query("set
names
utf8",$link)
$dbbck=new
backupData($link)//实例化它,只要一个链接标识就行了
//备份数据时,如想备份一个数据库中的所有表,你可这样写:
$dbbck->backupTables("cms","./",array('*'))
//备份数据时,如想备份一个数据库中的仅一个表时,你可这样写:
$dbbck->backupTables("cms","./",array('user'))
//备份数据时,如想备份一个数据库中的多个表时,你可这样写:
$dbbck->backupTables("cms","./",array('user','acl','informatoin'))
//注解:$dbbck->backupTables("参1","参2",array())中,
参1为:数据库名,
参2为:要存放备份数据的位置(即目录地址)
第三个为:你要保存那些表
ok...
以下为代码:
复制代码
代码如下:
<?php
/*
*
*简单的一个备份数据类
*author
FC
*
*/
class
backupData{
private
$mysql_link//链接标识
private
$dbName//数据库名
private
$dataDir
//数据所要存放的目录
private
$tableNames//表名
public
function
__construct($mysql_link){
$this->mysql_link
=
$mysql_link
}
public
function
backupTables($dbName,$dataDir,$tableNames){//开始备份
$this->dbName
=
$dbName
$this->dataDir
=
$dataDir
$this->tableNames
=
$tableNames
$tables=$this->delarray($this->tableNames)
$sqls=''
foreach($tables
as
$tablename){
if($tablename==''){//表不存在时
continue
}
//************************以下是形成SQL的前半部分**************
//如果存在表,就先删除
$sqls
.=
"DROP
TABLE
IF
EXISTS
$tablename\n"
//读取表结构
$rs
=
mysql_query("SHOW
CREATE
TABLE
$tablename",$this->mysql_link)
$row=mysql_fetch_row($rs)
//获得表结构组成SQL
$sqls.=$row['1']."\n\n"
unset($rs)
unset($row)
//************************以下是形成SQL的后半部分**************
//查寻出表中的所有数据
$rs=mysql_query("select
*
from
$tablename",$this->mysql_link)
//表的字段个数
$field=mysql_num_fields($rs)
//形成此种SQL语句:"INSERT
INTO
`groups`
VALUES('1499e0ca25988d','主任','','0')"
while($rows=mysql_fetch_row($rs)){
$comma=''//逗号
$sqls.="INSERT
INTO
`$tablename`
VALUES("
for($i=0$i<$field$i++){
$sqls.=$comma."'".$rows[$i]."'"
$comma=','
}
$sqls.=")\n\n\n"
}
}
$backfilepath=$this->dataDir.date("Ymdhis",time()).'.sql'
//写入文件
$filehandle
=
fopen($backfilepath,
"w")
fwrite($filehandle,
$sqls)
fclose($filehandle)
}
private
function
delarray($array){//处理传入进来的数组
foreach($array
as
$tables){
if($tables=='*'){//所有的表(获得表名时不能按常规方式来组成一个数组)
$newtables=mysql_list_tables($this->dbName,$this->mysql_link)
$tableList
=
array()
for
($i
=
0
$i
<
mysql_numrows($newtables)
$i++){
array_push($tableList,mysql_tablename($newtables,
$i))
}
$tableList=$tableList
}else{
$tableList=$array
break
}
}
return
$tableList
}
}
分卷导出思路:统计sql语句变量的长度,按1个字符当成1字节比较,如果大于设定分卷大小,则写入一个sql文件(我也不知道这样统计是否稳当,这也是借鉴其他的人的)。
分卷导入思路:按行读取sql文件,将每一行当作完整的sql语句存到数组再循环执行插入数据库就可以了,但是在创建表语句分了多行,这个需要单独处理(就这个花了我好长时间的);
<?php
//宋正河
转载请注明出处
set_time_limit(0)
header('content-type:text/htmlcharset=utf-8')
mysql_connect('localhost','root','root')
mysql_select_db('test')
$table_array=get_tables('test')
mysql_query('set
names
utf8')
$filesize=1024*1024*4
$start=$_GET['start']?$_GET['start']:0
$part=$_GET['part']?$_GET['part']:'1'
$table_index=$_GET['table_index']?$_GET['table_index']:'0'
$table=$table_array[$table_index]
$num=200000000//这个数要足够大,可以是总记录数
$backupdata=''
if($start=='0'){
$query="SHOW
CREATE
TABLE
`{$table}`"
$result
=
mysql_query($query)
$row
=
mysql_fetch_row($result)
$backupdata
.=
"DROP
TABLE
IF
EXISTS
`{$table}`\n"
.
$row[1]
.
"\n\n"
}
$limit=($start=='0')?'':"
limit
$start,$num
"
$query="select
*
from
`{$table}`
$limit
"
$result=mysql_query($query)
$numfields
=
mysql_num_fields($result)
//统计字段数
while($row=mysql_fetch_row($result)){
$comma
=
''
//存储逗号
$backupdata_tmp
=
"INSERT
INTO
`{$table}`
VALUES
("
for($i=0
$i<$numfields
$i++){
$backupdata_tmp
.=
$comma
.
"'"
.
mysql_escape_string($row[$i])
.
"'"
$comma
=
','
}
$backupdata_tmp
.=
")\n"
if(strlen($backupdata)+strlen($backupdata_tmp)
>
$filesize){
//写入文件并跳转
$file='data/'.$table.'-'.$part.'.sql'
file_put_contents($file,$backupdata)
echo
$file.'
备份完成,程序继续进行!'
$part++
//分段
//表名
//起点
//跳转
sleep(3)
echo
"<script>location.href='?start={$start}&table_index={$table_index}&part={$part}'</script>"
exit
}
$backupdata.=$backupdata_tmp
$start++
}
if($backupdata){
$file='data/'.$table.'-'.$part.'.sql'
file_put_contents($file,$backupdata)
}
echo
$table.'备份完成!<br
/>'
sleep(2)
$table_index++
if($table_array[$table_index]){
echo
"<script>location.href='?table_index={$table_index}'</script>"
exit
}else{
echo
'恭喜你,数据库备份完毕!'
}
function
get_tables($db){
$tq
=
mysql_list_tables($db)
while($tr
=
mysql_fetch_row($tq)){
$arrtb[]
=
$tr[0]
}
return
$arrtb
}
?>
以上所述就是本文的全部内容了,希望大家能够喜欢。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)