欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php mysql 数据库备份程序

程序员文章站 2022-05-16 23:49:15
...
提供一款实例的php mysql 数据库备份程序,很好方法的可以对你的数据库进行在线实时备份,这样可以保存数据库的安全,并且他是以.sql文件保存在bakdata目录还日期生成的数据库备份文件的,代码如下:
 
	 
	 
	数据管理
esweb系统数据管理
php mysql 数据库备份程序 如果需要备份、还原或者优化 大量的数据 , 推荐使用"帝国备份王2010"
esweb系统数据备份
选择备份方式 备份全部数据 备份单张表数据
选择目标位置 备份到服务器 备份到本地
使用分卷备份 分卷备份 k (只有选择备份到服务器,才能使用分卷备份功能)
程序说明 1.对于较大的数据表,强烈建议使用分卷备份.
2.只有选择备份到服务器,才能使用分卷备份功能.
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; global $backup; 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." "; $d->query("show create table ".$table); $d->nextrecord(); $tmp=preg_replace("/ /","",$d->f("create table")); $sql.=$tmp." "; return $sql; } function make_record($table,$num_fields) {global $d; $comma=""; $sql .= "insert into ".$table." values("; for($i = 0; $i record[$i])."'"); $comma = ",";} $sql .= ") "; return $sql; } function show_msg($msgs) { $title="提示:"; echo ""; echo ""; echo "
".$title."

    "; while (list($k,$v)=each($msgs)) { echo "
  • ".$v."
  • "; } echo "
"; } function pageend() { exit(); }