Home php教程 php手册 php database backup and restore

php database backup and restore

Jul 11, 2016 pm 08:00 PM
php code Open source database backup programming programming language software development

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>
Copy after login
<span style="color: #0000ff;">error_reporting(E_ALL & ~ E_NOTICE);</span>
Copy after login
<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>?>
Copy after login
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);
Copy after login
<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>?>
Copy after login
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 } }

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles