国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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>("連接服務(wù)器失敗."<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>("無法打開數(shù)據(jù)庫"<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;">***************************************類結(jié)束**************************</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>[]="服務(wù)器備份目錄為backup"<span style="color: #000000;">;
</span><span style="color: #800080;">$msgs</span>[]="對于較大的數(shù)據(jù)表,強(qiáng)烈建議使用分卷備份"<span style="color: #000000;">;
</span><span style="color: #800080;">$msgs</span>[]="只有選擇備份到服務(wù)器,才能使用分卷備份功能"<span style="color: #000000;">;
show_msg(</span><span style="color: #800080;">$msgs</span><span style="color: #000000;">);
</span>?>
class='header'>class="cells">
數(shù)據(jù)備份
備份方式
備份全部數(shù)據(jù) 備份全部數(shù)據(jù)表中的數(shù)據(jù)到一個備份文件
備份單張表數(shù)據(jù) 備份選中數(shù)據(jù)表中的數(shù)據(jù)到單獨的備份文件
使用分卷備份
分卷備份 K
選擇目標(biāo)位置
備份到服務(wù)器
備份到本地
/*-------------界面結(jié)束-------------*/}/*---------------------------------*/ /*----*/else{/*--------------主程序-----------------------------------------*/ if($_POST['weizhi']=="localpc"&&$_POST['fenjuan']=='yes') {$msgs[]="只有選擇備份到服務(wù)器,才能使用分卷備份功能"; 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[]="讀數(shù)據(jù)庫結(jié)構(gòu)錯誤"; 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[]="全部數(shù)據(jù)表數(shù)據(jù)備份完成,生成備份文件'./backup/$filename'"; else $msgs[]="備份全部數(shù)據(jù)表失敗"; show_msg($msgs); pageend(); } /*-----------------不要卷結(jié)束*/}/*-----------------------*/ /*-----------------分卷*/else{/*-------------------------*/ if(!$_POST['filesize']) {$msgs[]="請?zhí)顚憘浞菸募志泶笮?; show_msg($msgs);pageend();} if(!$tables=$d->query("show table status from $mysqldb")) {$msgs[]="讀數(shù)據(jù)庫結(jié)構(gòu)錯誤"; 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[]="全部數(shù)據(jù)表-卷-".$p."-數(shù)據(jù)備份完成,生成備份文件'./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[]="全部數(shù)據(jù)表-卷-".$p."-數(shù)據(jù)備份完成,生成備份文件'./backup/$filename'";} show_msg($msgs); /*---------------------分卷結(jié)束*/}/*--------------------------------------*/ /*--------備份全部表結(jié)束*/}/*---------------------------------------------*/ /*--------備份單表------*/elseif($_POST['bfzl']=="danbiao"){/*------------*/ if(!$_POST['tablename']) {$msgs[]="請選擇要備份的數(shù)據(jù)表"; 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']."-數(shù)據(jù)備份完成,生成備份文件'./backup/$filename'"; else $msgs[]="備份表-".$_POST['tablename']."-失敗"; show_msg($msgs); pageend(); } /*----------------不要卷結(jié)束*/}/*------------------------------------*/ /*----------------分卷*/else{/*--------------------------------------*/ if(!$_POST['filesize']) {$msgs[]="請?zhí)顚憘浞菸募志泶笮?; 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."-數(shù)據(jù)備份完成,生成備份文件'./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."-數(shù)據(jù)備份完成,生成備份文件'./backup/$filename'";} show_msg($msgs); /*----------分卷結(jié)束*/}/*--------------------------------------------------*/ /*----------備份單表結(jié)束*/}/*----------------------------------------------*/ /*---*/}/*-------------主程序結(jié)束------------------------------------------*/ 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>[]="本功能在恢復(fù)備份數(shù)據(jù)的同時,將全部覆蓋原有數(shù)據(jù),請確定是否需要恢復(fù),以免造成數(shù)據(jù)損失"<span style="color: #000000;">;
</span><span style="color: #800080;">$msgs</span>[]="數(shù)據(jù)恢復(fù)功能只能恢復(fù)由dShop導(dǎo)出的數(shù)據(jù)文件,其他軟件導(dǎo)出格式可能無法識別"<span style="color: #000000;">;
</span><span style="color: #800080;">$msgs</span>[]="從本地恢復(fù)數(shù)據(jù)需要服務(wù)器支持文件上傳并保證數(shù)據(jù)尺寸小于允許上傳的上限,否則只能使用從服務(wù)器恢復(fù)"<span style="color: #000000;">;
</span><span style="color: #800080;">$msgs</span>[]="如果您使用了分卷備份,只需手工導(dǎo)入文件卷1,其他數(shù)據(jù)文件會由系統(tǒng)自動導(dǎo)入"<span style="color: #000000;">;
show_msg(</span><span style="color: #800080;">$msgs</span><span style="color: #000000;">);
</span>?>
class="header">
數(shù)據(jù)恢復(fù)
從服務(wù)器文件恢復(fù)
從本地文件恢復(fù)
/**************************界面結(jié)束*/}/*************************************/ /****************************主程序*/if($_POST['act']=="恢復(fù)"){/**************/ /***************服務(wù)器恢復(fù)*/if($_POST['restorefrom']=="server"){/**************/ if(!$_POST['serverfile']) {$msgs[]="您選擇從服務(wù)器文件恢復(fù)備份,但沒有指定備份文件"; show_msg($msgs); pageend(); } if(!preg_match("/_v[0-9]+/",$_POST['serverfile'])) {$filename="./backup/".$_POST['serverfile']; if(import($filename)) $msgs[]="備份文件".$_POST['serverfile']."成功導(dǎo)入數(shù)據(jù)庫"; else $msgs[]="備份文件".$_POST['serverfile']."導(dǎo)入失敗"; show_msg($msgs); pageend(); } else { $filename="./backup/".$_POST['serverfile']; if(import($filename)) $msgs[]="備份文件".$_POST['serverfile']."成功導(dǎo)入數(shù)據(jù)庫"; else {$msgs[]="備份文件".$_POST['serverfile']."導(dǎo)入失敗";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[]="程序?qū)⒃?秒鐘后自動開始導(dǎo)入此分卷備份的下一部份:文件".$tmpfile.",請勿手動中止程序的運行,以免數(shù)據(jù)庫結(jié)構(gòu)受損"; $_SESSION['data_file']=$tmpfile; show_msg($msgs); sleep(3); echo ""; } else { $msgs[]="此分卷備份全部導(dǎo)入成功"; show_msg($msgs); } } /**************服務(wù)器恢復(fù)結(jié)束*/}/********************************************/ /*****************本地恢復(fù)*/if($_POST['restorefrom']=="localpc"){/**************/ switch ($_FILES['myfile']['error']) { case 1: case 2: $msgs[]="您上傳的文件大于服務(wù)器限定值,上傳未成功"; 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[]="本地備份文件成功導(dǎo)入數(shù)據(jù)庫"; unlink("./backup/".$fname);} else $msgs[]="本地備份文件導(dǎo)入數(shù)據(jù)庫失敗"; } else ($msgs[]="從本地上傳備份文件失敗"); show_msg($msgs); /****本地恢復(fù)結(jié)束*****/}/****************************************************/ /****************************主程序結(jié)束*/}/**********************************/ /*************************剩余分卷備份恢復(fù)**********************************/ if(!$_POST['act']&&$_SESSION['data_file']) { $filename="./backup/".$_SESSION['data_file']; if(import($filename)) $msgs[]="備份文件".$_SESSION['data_file']."成功導(dǎo)入數(shù)據(jù)庫"; else {$msgs[]="備份文件".$_SESSION['data_file']."導(dǎo)入失敗";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[]="程序?qū)⒃?秒鐘后自動開始導(dǎo)入此分卷備份的下一部份:文件".$tmpfile.",請勿手動中止程序的運行,以免數(shù)據(jù)庫結(jié)構(gòu)受損"; $_SESSION['data_file']=$tmpfile; show_msg($msgs); sleep(3); echo ""; } else { $msgs[]="此分卷備份全部導(dǎo)入成功"; unset($_SESSION['data_file']); show_msg($msgs); } } /**********************剩余分卷備份恢復(fù)結(jié)束*******************************/ 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(); } ?> 文件結(jié)構(gòu)非常清晰,只要在文件2和3里面設(shè)置好數(shù)據(jù)庫服務(wù)器的地址、用戶名、密碼就可以備份還原數(shù)據(jù)了。需要注意的是: ·使用時候要在同級目錄下建一個Backup目錄,權(quán)限需要可寫,用于存放導(dǎo)出的腳本。 ·當(dāng)備份的數(shù)據(jù)庫比較大的時候,服務(wù)器腳本超時時間要調(diào)大一些。 ·支持分卷備份,還原時候只要選擇分卷備份的第一個腳本就會自動還原所有的腳本。 ·分卷文件大小不要太大,最好不超過2MB。 ·安全起見,腳本不用時候記得從服務(wù)器上刪除。 數(shù)據(jù)庫備份 1.php備份數(shù)據(jù)庫的原理 查找所有表 查找所有字段 查找所有數(shù)據(jù) 生成SQL 2.php中mysql相關(guān)函數(shù) mysql_list_tables()表查詢函數(shù),類似mysql_query()函數(shù) mysql_fetch_field()字段信息函數(shù),返回句柄 Name字段的名稱 Table字段所屬數(shù)據(jù)庫的名稱 type 字段的類型 max_length字段的最大長度 not_null字段是否為空 3.備份時注意事項 a.注意數(shù)據(jù)庫的大小,過大或者過多分段處理 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

How do I stay up-to-date with the latest PHP developments and best practices? How do I stay up-to-date with the latest PHP developments and best practices? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

What is PHP, and why is it used for web development? What is PHP, and why is it used for web development? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

How to set PHP time zone? How to set PHP time zone? Jun 25, 2025 am 01:00 AM

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

How do I validate user input in PHP to ensure it meets certain criteria? How do I validate user input in PHP to ensure it meets certain criteria? Jun 22, 2025 am 01:00 AM

TovalidateuserinputinPHP,usebuilt-invalidationfunctionslikefilter_var()andfilter_input(),applyregularexpressionsforcustomformatssuchasusernamesorphonenumbers,checkdatatypesfornumericvalueslikeageorprice,setlengthlimitsandtrimwhitespacetopreventlayout

What is data serialization in PHP (serialize(), unserialize())? What is data serialization in PHP (serialize(), unserialize())? Jun 22, 2025 am 01:03 AM

ThePhpfunctionSerialize () andunserialize () AreusedtoconvertcomplexdaTastructdestoresintostoraSandaBackagain.1.Serialize () c OnvertsdatalikecarraysorobjectsraystringcontainingTypeandstructureinformation.2.unserialize () Reconstruct theoriginalatataprom

How do I embed PHP code in an HTML file? How do I embed PHP code in an HTML file? Jun 22, 2025 am 01:00 AM

You can embed PHP code into HTML files, but make sure that the file has an extension of .php so that the server can parse it correctly. Use standard tags to wrap PHP code, insert dynamic content anywhere in HTML. In addition, you can switch PHP and HTML multiple times in the same file to realize dynamic functions such as conditional rendering. Be sure to pay attention to the server configuration and syntax correctness to avoid problems caused by short labels, quotation mark errors or omitted end labels.

What are the best practices for writing clean and maintainable PHP code? What are the best practices for writing clean and maintainable PHP code? Jun 24, 2025 am 12:53 AM

The key to writing clean and easy-to-maintain PHP code lies in clear naming, following standards, reasonable structure, making good use of comments and testability. 1. Use clear variables, functions and class names, such as $userData and calculateTotalPrice(); 2. Follow the PSR-12 standard unified code style; 3. Split the code structure according to responsibilities, and organize it using MVC or Laravel-style catalogs; 4. Avoid noodles-style code and split the logic into small functions with a single responsibility; 5. Add comments at key points and write interface documents to clarify parameters, return values ??and exceptions; 6. Improve testability, adopt dependency injection, reduce global state and static methods. These practices improve code quality, collaboration efficiency and post-maintenance ease.

How do I execute SQL queries using PHP? How do I execute SQL queries using PHP? Jun 24, 2025 am 12:54 AM

Yes,youcanrunSQLqueriesusingPHP,andtheprocessinvolveschoosingadatabaseextension,connectingtothedatabase,executingqueriessafely,andclosingconnectionswhendone.Todothis,firstchoosebetweenMySQLiorPDO,withPDObeingmoreflexibleduetosupportingmultipledatabas

See all articles