首页 > php教程 > php手册 > 正文

php 数据库备份、还原

WBOY
发布: 2016-07-11 20:00:42
原创
1483 人浏览过
1. mydb.php <span style="color: #008000;">//</span><span style="color: #008000;">DB类</span>
2. backup.php <span style="color: #008000;">//</span><span style="color: #008000;">备份脚本</span>
3. restore.php <span style="color: #008000;">//</span><span style="color: #008000;">还原脚本</span>
<span style="color: #000000;">
mydb</span>.<span style="color: #000000;">php

</span>
<span style="color: #0000ff;">class</span><span style="color: #000000;"> db{

</span><span style="color: #0000ff;">var</span> <span style="color: #800080;">$linkid</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">var</span> <span style="color: #800080;">$sqlid</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">var</span> <span style="color: #800080;">$record</span><span style="color: #000000;">;

</span><span style="color: #0000ff;">function</span> db(<span style="color: #800080;">$host</span>="",<span style="color: #800080;">$username</span>="",<span style="color: #800080;">$password</span>="",<span style="color: #800080;">$database</span>=""<span style="color: #000000;">)
{
</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$this</span>->linkid)    @<span style="color: #800080;">$this</span>->linkid = <span style="color: #008080;">mysql_connect</span>(<span style="color: #800080;">$host</span>, <span style="color: #800080;">$username</span>, <span style="color: #800080;">$password</span>) or <span style="color: #0000ff;">die</span>("连接服务器失败."<span style="color: #000000;">);
@</span><span style="color: #008080;">mysql_select_db</span>(<span style="color: #800080;">$database</span>,<span style="color: #800080;">$this</span>->linkid) or <span style="color: #0000ff;">die</span>("无法打开数据库"<span style="color: #000000;">);
</span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">linkid;}

</span><span style="color: #0000ff;">function</span> query(<span style="color: #800080;">$sql</span><span style="color: #000000;">)
{</span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->sqlid=<span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span>,<span style="color: #800080;">$this</span>->linkid)) <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">sqlid;
</span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
    </span><span style="color: #800080;">$this</span>->err_report(<span style="color: #800080;">$sql</span>,<span style="color: #008080;">mysql_error</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;}
}

</span><span style="color: #0000ff;">function</span> nr(<span style="color: #800080;">$sql_id</span>=""<span style="color: #000000;">)
{</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$sql_id</span>) <span style="color: #800080;">$sql_id</span>=<span style="color: #800080;">$this</span>-><span style="color: #000000;">sqlid;
</span><span style="color: #0000ff;">return</span> <span style="color: #008080;">mysql_num_rows</span>(<span style="color: #800080;">$sql_id</span><span style="color: #000000;">);}

</span><span style="color: #0000ff;">function</span> nf(<span style="color: #800080;">$sql_id</span>=""<span style="color: #000000;">)
{</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$sql_id</span>) <span style="color: #800080;">$sql_id</span>=<span style="color: #800080;">$this</span>-><span style="color: #000000;">sqlid;
</span><span style="color: #0000ff;">return</span> <span style="color: #008080;">mysql_num_fields</span>(<span style="color: #800080;">$sql_id</span><span style="color: #000000;">);}

</span><span style="color: #0000ff;">function</span> nextrecord(<span style="color: #800080;">$sql_id</span>=""<span style="color: #000000;">)
{</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$sql_id</span>) <span style="color: #800080;">$sql_id</span>=<span style="color: #800080;">$this</span>-><span style="color: #000000;">sqlid;
</span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->record=<span style="color: #008080;">mysql_fetch_array</span>(<span style="color: #800080;">$sql_id</span>))    <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>-><span style="color: #000000;">record;
</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
}

</span><span style="color: #0000ff;">function</span> f(<span style="color: #800080;">$name</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$this</span>->record[<span style="color: #800080;">$name</span>]) <span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->record[<span style="color: #800080;">$name</span><span style="color: #000000;">];
</span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
}

</span><span style="color: #0000ff;">function</span> close() {<span style="color: #008080;">mysql_close</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">linkid);}

</span><span style="color: #0000ff;">function</span> lock(<span style="color: #800080;">$tblname</span>,<span style="color: #800080;">$op</span>="WRITE"<span style="color: #000000;">)
{</span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">mysql_query</span>("lock tables ".<span style="color: #800080;">$tblname</span>." ".<span style="color: #800080;">$op</span>)) <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span>; <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;}

</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> unlock()
{</span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">mysql_query</span>("unlock tables")) <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span>; <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;}

</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> ar() {
      </span><span style="color: #0000ff;">return</span> @<span style="color: #008080;">mysql_affected_rows</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">linkid);
    }

</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> i_id() {
    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">mysql_insert_id</span><span style="color: #000000;">();
}

</span><span style="color: #0000ff;">function</span> err_report(<span style="color: #800080;">$sql</span>,<span style="color: #800080;">$err</span><span style="color: #000000;">)
{
</span><span style="color: #0000ff;">echo</span> "Mysql查询错误<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "查询语句:".<span style="color: #800080;">$sql</span>."<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "错误信息:".<span style="color: #800080;">$err</span><span style="color: #000000;">;
}
</span><span style="color: #008000;">/*</span><span style="color: #008000;">***************************************类结束**************************</span><span style="color: #008000;">*/</span><span style="color: #000000;">
}</span>?><span style="color: #000000;">

backup</span>.<span style="color: #000000;">php

</span>
登录后复制
<span style="color: #0000ff;">error_reporting(E_ALL & ~ E_NOTICE);</span>
登录后复制
<span style="color: #0000ff;">global</span> <span style="color: #800080;">$mysqlhost</span>, <span style="color: #800080;">$mysqluser</span>, <span style="color: #800080;">$mysqlpwd</span>, <span style="color: #800080;">$mysqldb</span><span style="color: #000000;">;
</span><span style="color: #800080;">$mysqlhost</span>="localhost"; <span style="color: #008000;">//</span><span style="color: #008000;">host name</span>
<span style="color: #800080;">$mysqluser</span>="root";                <span style="color: #008000;">//</span><span style="color: #008000;">login name</span>
<span style="color: #800080;">$mysqlpwd</span>="";                <span style="color: #008000;">//</span><span style="color: #008000;">password</span>
<span style="color: #800080;">$mysqldb</span>="";          <span style="color: #008000;">//</span><span style="color: #008000;">name of database</span>

<span style="color: #0000ff;">include</span>("mydb.php"<span style="color: #000000;">);
</span><span style="color: #800080;">$d</span>=<span style="color: #0000ff;">new</span> db(<span style="color: #800080;">$mysqlhost</span>,<span style="color: #800080;">$mysqluser</span>,<span style="color: #800080;">$mysqlpwd</span>,<span style="color: #800080;">$mysqldb</span><span style="color: #000000;">);
</span><span style="color: #008000;">/*</span><span style="color: #008000;">--------------界面--------------</span><span style="color: #008000;">*/</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$_POST</span>['act']){<span style="color: #008000;">/*</span><span style="color: #008000;">----------------------</span><span style="color: #008000;">*/</span>
<span style="color: #800080;">$msgs</span>[]="服务器备份目录为backup"<span style="color: #000000;">;
</span><span style="color: #800080;">$msgs</span>[]="对于较大的数据表,强烈建议使用分卷备份"<span style="color: #000000;">;
</span><span style="color: #800080;">$msgs</span>[]="只有选择备份到服务器,才能使用分卷备份功能"<span style="color: #000000;">;
show_msg(</span><span style="color: #800080;">$msgs</span><span style="color: #000000;">);
</span>?>
登录后复制
class='header'>class="cells">
数据备份
备份方式
备份全部数据 备份全部数据表中的数据到一个备份文件
备份单张表数据 备份选中数据表中的数据到单独的备份文件
使用分卷备份
分卷备份 K
选择目标位置
备份到服务器
备份到本地
/*-------------界面结束-------------*/}/*---------------------------------*/ /*----*/else{/*--------------主程序-----------------------------------------*/ if($_POST['weizhi']=="localpc"&&$_POST['fenjuan']=='yes') {$msgs[]="只有选择备份到服务器,才能使用分卷备份功能"; show_msg($msgs); pageend();} if($_POST['fenjuan']=="yes"&&!$_POST['filesize']) {$msgs[]="您选择了分卷备份功能,但未填写分卷文件大小"; show_msg($msgs); pageend();} if($_POST['weizhi']=="server"&&!writeable("./backup")) {$msgs[]="备份文件存放目录'./backup'不可写,请修改目录属性"; show_msg($msgs); pageend();} /*----------备份全部表-------------*/if($_POST['bfzl']=="quanbubiao"){/*----*/ /*----不分卷*/if(!$_POST['fenjuan']){/*--------------------------------*/ if(!$tables=$d->query("show table status from $mysqldb")) {$msgs[]="读数据库结构错误"; show_msg($msgs); pageend();} $sql=""; while($d->nextrecord($tables)) { $table=$d->f("Name"); $sql.=make_header($table); $d->query("select * from $table"); $num_fields=$d->nf(); while($d->nextrecord()) {$sql.=make_record($table,$num_fields);} } $filename=date("Ymd",time())."_all.sql"; if($_POST['weizhi']=="localpc") down_file($sql,$filename); elseif($_POST['weizhi']=="server") {if(write_file($sql,$filename)) $msgs[]="全部数据表数据备份完成,生成备份文件'./backup/$filename'"; else $msgs[]="备份全部数据表失败"; show_msg($msgs); pageend(); } /*-----------------不要卷结束*/}/*-----------------------*/ /*-----------------分卷*/else{/*-------------------------*/ if(!$_POST['filesize']) {$msgs[]="请填写备份文件分卷大小"; show_msg($msgs);pageend();} if(!$tables=$d->query("show table status from $mysqldb")) {$msgs[]="读数据库结构错误"; show_msg($msgs); pageend();} $sql=""; $p=1; $filename=date("Ymd",time())."_all"; while($d->nextrecord($tables)) { $table=$d->f("Name"); $sql.=make_header($table); $d->query("select * from $table"); $num_fields=$d->nf(); while($d->nextrecord()) {$sql.=make_record($table,$num_fields); if(strlen($sql)>=$_POST['filesize']*1000){ $filename.=("_v".$p.".sql"); if(write_file($sql,$filename)) $msgs[]="全部数据表-卷-".$p."-数据备份完成,生成备份文件'./backup/$filename'"; else $msgs[]="备份表-".$_POST['tablename']."-失败"; $p++; $filename=date("Ymd",time())."_all"; $sql="";} } } if($sql!=""){$filename.=("_v".$p.".sql"); if(write_file($sql,$filename)) $msgs[]="全部数据表-卷-".$p."-数据备份完成,生成备份文件'./backup/$filename'";} show_msg($msgs); /*---------------------分卷结束*/}/*--------------------------------------*/ /*--------备份全部表结束*/}/*---------------------------------------------*/ /*--------备份单表------*/elseif($_POST['bfzl']=="danbiao"){/*------------*/ if(!$_POST['tablename']) {$msgs[]="请选择要备份的数据表"; show_msg($msgs); pageend();} /*--------不分卷*/if(!$_POST['fenjuan']){/*-------------------------------*/ $sql=make_header($_POST['tablename']); $d->query("select * from ".$_POST['tablename']); $num_fields=$d->nf(); while($d->nextrecord()) {$sql.=make_record($_POST['tablename'],$num_fields);} $filename=date("Ymd",time())."_".$_POST['tablename'].".sql"; if($_POST['weizhi']=="localpc") down_file($sql,$filename); elseif($_POST['weizhi']=="server") {if(write_file($sql,$filename)) $msgs[]="表-".$_POST['tablename']."-数据备份完成,生成备份文件'./backup/$filename'"; else $msgs[]="备份表-".$_POST['tablename']."-失败"; show_msg($msgs); pageend(); } /*----------------不要卷结束*/}/*------------------------------------*/ /*----------------分卷*/else{/*--------------------------------------*/ if(!$_POST['filesize']) {$msgs[]="请填写备份文件分卷大小"; show_msg($msgs);pageend();} $sql=make_header($_POST['tablename']); $p=1; $filename=date("Ymd",time())."_".$_POST['tablename']; $d->query("select * from ".$_POST['tablename']); $num_fields=$d->nf(); while ($d->nextrecord()) { $sql.=make_record($_POST['tablename'],$num_fields); if(strlen($sql)>=$_POST['filesize']*1000){ $filename.=("_v".$p.".sql"); if(write_file($sql,$filename)) $msgs[]="表-".$_POST['tablename']."-卷-".$p."-数据备份完成,生成备份文件'./backup/$filename'"; else $msgs[]="备份表-".$_POST['tablename']."-失败"; $p++; $filename=date("Ymd",time())."_".$_POST['tablename']; $sql="";} } if($sql!=""){$filename.=("_v".$p.".sql"); if(write_file($sql,$filename)) $msgs[]="表-".$_POST['tablename']."-卷-".$p."-数据备份完成,生成备份文件'./backup/$filename'";} show_msg($msgs); /*----------分卷结束*/}/*--------------------------------------------------*/ /*----------备份单表结束*/}/*----------------------------------------------*/ /*---*/}/*-------------主程序结束------------------------------------------*/ function write_file($sql,$filename) { $re=true; if(!@$fp=fopen("./backup/".$filename,"w+")) {$re=false; echo "failed to open target file";} if(!@fwrite($fp,$sql)) {$re=false; echo "failed to write file";} if(!@fclose($fp)) {$re=false; echo "failed to close target file";} return $re; } function down_file($sql,$filename) { ob_end_clean(); header("Content-Encoding: none"); header("Content-Type: ".(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ? 'application/octetstream' : 'application/octet-stream')); header("Content-Disposition: ".(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') ? 'inline; ' : 'attachment; ')."filename=".$filename); header("Content-Length: ".strlen($sql)); header("Pragma: no-cache"); header("Expires: 0"); echo $sql; $e=ob_get_contents(); ob_end_clean(); } function writeable($dir) { if(!is_dir($dir)) { @mkdir($dir, 0777); } if(is_dir($dir)) { if($fp = @fopen("$dir/test.test", 'w')) { @fclose($fp); @unlink("$dir/test.test"); $writeable = 1; } else { $writeable = 0; } } return $writeable; } function make_header($table) {global $d; $sql="DROP TABLE IF EXISTS ".$table."\n"; $d->query("show create table ".$table); $d->nextrecord(); $tmp=preg_replace("/\n/","",$d->f("Create Table")); $sql.=$tmp."\n"; return $sql; } function make_record($table,$num_fields) {global $d; $comma=""; $sql .= "INSERT INTO ".$table." VALUES("; for($i = 0; $i $num_fields; $i++) {$sql .= ($comma."'".mysql_escape_string($d->record[$i])."'"); $comma = ",";} $sql .= ")\n"; return $sql; } function show_msg($msgs) { $title="提示:"; echo ""; echo ""; echo "
".$title."

    "; while (list($k,$v)=each($msgs)) { echo "
  • ".$v."
  • "; } echo "
"; } function pageend() { exit(); } ?> restore.php
error_reporting(E_ALL & ~ E_NOTICE);
登录后复制
<span style="color: #008080;">session_start</span><span style="color: #000000;">();
</span><span style="color: #0000ff;">global</span> <span style="color: #800080;">$mysqlhost</span>, <span style="color: #800080;">$mysqluser</span>, <span style="color: #800080;">$mysqlpwd</span>, <span style="color: #800080;">$mysqldb</span><span style="color: #000000;">;
</span><span style="color: #800080;">$mysqlhost</span>="localhost"; <span style="color: #008000;">//</span><span style="color: #008000;">host name</span>
<span style="color: #800080;">$mysqluser</span>="root";                <span style="color: #008000;">//</span><span style="color: #008000;">login name</span>
<span style="color: #800080;">$mysqlpwd</span>="";                <span style="color: #008000;">//</span><span style="color: #008000;">password</span>
<span style="color: #800080;">$mysqldb</span>="";          <span style="color: #008000;">//</span><span style="color: #008000;">name of database</span>

<span style="color: #0000ff;">include</span>("mydb.php"<span style="color: #000000;">);
</span><span style="color: #800080;">$d</span>=<span style="color: #0000ff;">new</span> db(<span style="color: #800080;">$mysqlhost</span>,<span style="color: #800080;">$mysqluser</span>,<span style="color: #800080;">$mysqlpwd</span>,<span style="color: #800080;">$mysqldb</span><span style="color: #000000;">);

</span><span style="color: #008000;">/*</span><span style="color: #008000;">*****界面</span><span style="color: #008000;">*/</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$_POST</span>['act']&&!<span style="color: #800080;">$_SESSION</span>['data_file']){<span style="color: #008000;">/*</span><span style="color: #008000;">********************</span><span style="color: #008000;">*/</span>
<span style="color: #800080;">$msgs</span>[]="本功能在恢复备份数据的同时,将全部覆盖原有数据,请确定是否需要恢复,以免造成数据损失"<span style="color: #000000;">;
</span><span style="color: #800080;">$msgs</span>[]="数据恢复功能只能恢复由dShop导出的数据文件,其他软件导出格式可能无法识别"<span style="color: #000000;">;
</span><span style="color: #800080;">$msgs</span>[]="从本地恢复数据需要服务器支持文件上传并保证数据尺寸小于允许上传的上限,否则只能使用从服务器恢复"<span style="color: #000000;">;
</span><span style="color: #800080;">$msgs</span>[]="如果您使用了分卷备份,只需手工导入文件卷1,其他数据文件会由系统自动导入"<span style="color: #000000;">;
show_msg(</span><span style="color: #800080;">$msgs</span><span style="color: #000000;">);
</span>?>
登录后复制
class="header">
数据恢复
从服务器文件恢复
从本地文件恢复
/**************************界面结束*/}/*************************************/ /****************************主程序*/if($_POST['act']=="恢复"){/**************/ /***************服务器恢复*/if($_POST['restorefrom']=="server"){/**************/ if(!$_POST['serverfile']) {$msgs[]="您选择从服务器文件恢复备份,但没有指定备份文件"; show_msg($msgs); pageend(); } if(!preg_match("/_v[0-9]+/",$_POST['serverfile'])) {$filename="./backup/".$_POST['serverfile']; if(import($filename)) $msgs[]="备份文件".$_POST['serverfile']."成功导入数据库"; else $msgs[]="备份文件".$_POST['serverfile']."导入失败"; show_msg($msgs); pageend(); } else { $filename="./backup/".$_POST['serverfile']; if(import($filename)) $msgs[]="备份文件".$_POST['serverfile']."成功导入数据库"; else {$msgs[]="备份文件".$_POST['serverfile']."导入失败";show_msg($msgs);pageend();} $voltmp=explode("_v",$_POST['serverfile']); $volname=$voltmp[0]; $volnum=explode(".sq",$voltmp[1]); $volnum=intval($volnum[0])+1; $tmpfile=$volname."_v".$volnum.".sql"; if(file_exists("./backup/".$tmpfile)) { $msgs[]="程序将在3秒钟后自动开始导入此分卷备份的下一部份:文件".$tmpfile.",请勿手动中止程序的运行,以免数据库结构受损"; $_SESSION['data_file']=$tmpfile; show_msg($msgs); sleep(3); echo ""; } else { $msgs[]="此分卷备份全部导入成功"; show_msg($msgs); } } /**************服务器恢复结束*/}/********************************************/ /*****************本地恢复*/if($_POST['restorefrom']=="localpc"){/**************/ switch ($_FILES['myfile']['error']) { case 1: case 2: $msgs[]="您上传的文件大于服务器限定值,上传未成功"; break; case 3: $msgs[]="未能从本地完整上传备份文件"; break; case 4: $msgs[]="从本地上传备份文件失败"; break; case 0: break; } if($msgs){show_msg($msgs);pageend();} $fname=date("Ymd",time())."_".sjs(5).".sql"; if (is_uploaded_file($_FILES['myfile']['tmp_name'])) { copy($_FILES['myfile']['tmp_name'], "./backup/".$fname);} if (file_exists("./backup/".$fname)) { $msgs[]="本地备份文件上传成功"; if(import("./backup/".$fname)) {$msgs[]="本地备份文件成功导入数据库"; unlink("./backup/".$fname);} else $msgs[]="本地备份文件导入数据库失败"; } else ($msgs[]="从本地上传备份文件失败"); show_msg($msgs); /****本地恢复结束*****/}/****************************************************/ /****************************主程序结束*/}/**********************************/ /*************************剩余分卷备份恢复**********************************/ if(!$_POST['act']&&$_SESSION['data_file']) { $filename="./backup/".$_SESSION['data_file']; if(import($filename)) $msgs[]="备份文件".$_SESSION['data_file']."成功导入数据库"; else {$msgs[]="备份文件".$_SESSION['data_file']."导入失败";show_msg($msgs);pageend();} $voltmp=explode("_v",$_SESSION['data_file']); $volname=$voltmp[0]; $volnum=explode(".sq",$voltmp[1]); $volnum=intval($volnum[0])+1; $tmpfile=$volname."_v".$volnum.".sql"; if(file_exists("./backup/".$tmpfile)) { $msgs[]="程序将在3秒钟后自动开始导入此分卷备份的下一部份:文件".$tmpfile.",请勿手动中止程序的运行,以免数据库结构受损"; $_SESSION['data_file']=$tmpfile; show_msg($msgs); sleep(3); echo ""; } else { $msgs[]="此分卷备份全部导入成功"; unset($_SESSION['data_file']); show_msg($msgs); } } /**********************剩余分卷备份恢复结束*******************************/ function import($fname) {global $d; $sqls=file($fname); foreach($sqls as $sql) { str_replace("\r","",$sql); str_replace("\n","",$sql); if(!$d->query(trim($sql))) return false; } return true; } function show_msg($msgs) { $title="提示:"; echo ""; echo ""; echo "
".$title."

    "; while (list($k,$v)=each($msgs)) { echo "
  • ".$v."
  • "; } echo "
"; } function pageend() { exit(); } ?> 文件结构非常清晰,只要在文件2和3里面设置好数据库服务器的地址、用户名、密码就可以备份还原数据了。需要注意的是: ·使用时候要在同级目录下建一个Backup目录,权限需要可写,用于存放导出的脚本。 ·当备份的数据库比较大的时候,服务器脚本超时时间要调大一些。 ·支持分卷备份,还原时候只要选择分卷备份的第一个脚本就会自动还原所有的脚本。 ·分卷文件大小不要太大,最好不超过2MB。 ·安全起见,脚本不用时候记得从服务器上删除。 数据库备份 1.php备份数据库的原理 查找所有表 查找所有字段 查找所有数据 生成SQL 2.php中mysql相关函数 mysql_list_tables()表查询函数,类似mysql_query()函数 mysql_fetch_field()字段信息函数,返回句柄 Name字段的名称 Table字段所属数据库的名称 type 字段的类型 max_length字段的最大长度 not_null字段是否为空 3.备份时注意事项 a.注意数据库的大小,过大或者过多分段处理 b.生成的SQL文件名或者存在不已被猜到 c.备份生成文件可以表或者自动为单位保存 d.可以使用ZIP组件压缩生成的文件以便保存 $dbname="root" mysql_connect('localhost','root',''); mysql_select_db(); $tq=mysql_list_tables($dbname); while($tr=mysql_fetch_rows($tq)){ } function get_table_fd($dbname){ $query=mysql_query("select * from $dbname"); while($row=mysql_fetch_field($query)){ echo $row->name } }

 

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板