scribble

守望的麦子

About Projects Tool Guestbook

27 Nov 2007
数据库备份(支持分卷b/r)— mysql back/restore, can split (p

共三个文件和一个文件夹
1. mydb.php //DB类
2. backup.php //备份脚本
3. restore.php //还原脚本
4.backup //临时文件目录

只要修改一下开头的数据库信息就可用

-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-—mydb.php:-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-—- linkid) @$this->linkid = mysql_connect($host, $username, $password) or die("连接服务器失败."); @mysql\_select\_db($database,$this->linkid) or die("无法打开数据库"); return $this->linkid;} function query($sql) {if($this->sqlid=mysql_query($sql,$this->linkid)) return $this->sqlid; else { $this->err\_report($sql,mysql\_error); return false;} } function nr($sql_id="") {if(!$sql\_id) $sql\_id=$this->sqlid; return mysql\_num\_rows($sql_id);} function nf($sql_id="") {if(!$sql\_id) $sql\_id=$this->sqlid; return mysql\_num\_fields($sql_id);} function nextrecord($sql_id="") {if(!$sql\_id) $sql\_id=$this->sqlid; if($this->record=mysql\_fetch\_array($sql_id)) return $this->record; else return false; } function f($name) { if($this->record[$name]) return $this->record[$name]; else return false; } function close() {mysql_close($this->linkid);} function lock($tblname,$op="WRITE") {if(mysql_query("lock tables ".$tblname." ".$op)) return true; else return false;} function unlock() {if(mysql_query("unlock tables")) return true; else return false;} function ar() { return @mysql\_affected\_rows($this->linkid); } function i_id() { return mysql\_insert\_id(); } function err_report($sql,$err) { echo "Mysql查询错误 "; echo "查询语句:".$sql." "; echo "错误信息:".$err; } /\***\***\***\***\***\***\***\***\***\***\***\****\*\\*\*类结束\*\*\***\***\***\***\***\***\***\****/ }?>

-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-—-

-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-—backup.php :-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-—- 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 = ""; $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 " "; while (list($k,$v)=each($msgs)) { echo " * ".$v." "; } echo "
"; } function pageend() { exit(); } ?>

-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-—-

-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-—restore.php :-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-—- } 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(!isset($\_POST['act'])&&isset($\_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 " "; while (list($k,$v)=each($msgs)) { echo " * ".$v." "; } echo "
"; } function pageend() { exit(); } ?>

-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-–-—-

FROM: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1263017

Til next time,
Jason at 00:00

scribble

About Projects Tool Guestbook