esweb系统数据管理
/*--------------界面--------------*/if(!$_post['act']){/*----------------------*/
$msgs[]="服务器备份目录为$backup";
$msgs[]="对于较大的数据表,强烈建议使用分卷备份";
$msgs[]="只有选择备份到服务器,才能使用分卷备份功能";
//show_msg($msgs);
?>
/*----*/else{/*--------------Main program---------------------- ------------------*/
if($_post['weizhi']=="localpc"&&$_post['fenjuan']=='yes')
{$msgs[]="The volume backup function can only be used if you choose to back up to the server";
show_msg($msgs); pageend();}
if($_post['fenjuan']=="yes"&&!$_post['filesize'])
{$msgs[]="You selected the volume backup function, but did not fill in the volume file size";
show_msg($msgs); pageend();}
if($_post['weizhi']=="server"&&!writeable($backup))
{$msgs[]="The backup file storage directory '$backup' is not writable, please modify the directory attributes";
show_msg($msgs); pageend();}
/*----------Back up all tables-------------*/if($_post['bfzl']=="quanbubiao"){/ *----*/
/*----Not divided into volumes*/if(!$_post['fenjuan']){/*------------------------- -------*/
if(!$tables=$d->query("show table status from $mysqldb"))
{$msgs[]="Error in reading database structure"; 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[]="All data table data backup completed, backup file '$backup/$filename' is generated";
else $msgs[]="Backup of all data tables failed";
show_msg($msgs);
pageend();
}
/*-----------------Don't end the roll*/}/*----------------------- */
/*-----------------Volume*/else{/*----------------------- --*/
if(!$_post['filesize'])
{$msgs[]="Please fill in the backup file volume size"; show_msg($msgs);pageend();}
if(!$tables=$d->query("show table status from $mysqldb"))
{$msgs[]="Error in reading database structure"; 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[]="All data tables-volume-".$p."-Data backup completed, backup file '$backup/$filename'" is generated;
else $msgs[]="Backup table-".$_post['tablename']."-Failed";
$p++;
$filename=date("ymd",time())."_all";
$sql="";}
}
}
if($sql!=""){$filename.=("_v".$p.".sql");
if(write_file($sql,$filename))
$msgs[]="All data tables-volume-".$p."-data backup completed, generate backup file '$backup/$filename'";}
show_msg($msgs);
/*---------------------End of volume*/}/*------------------ ------------------*/
/*--------End of backing up all tables*/}/*---------------------------------- ---------------*/
/*--------Backup single table------*/elseif($_post['bfzl']=="danbiao"){/*-------- ----*/
if(!$_post['tablename'])
{$msgs[]="Please select the data table to be backed up"; show_msg($msgs); pageend();}
/*--------Not divided into volumes*/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[]="Table-".$_post['tablename']."-Data backup is completed and the backup file '$backup/$filename'" is generated;
else $msgs[]="Backup table-".$_post['tablename']."-failed";
show_msg($msgs);
pageend();
}
/*----------------Don't end the roll*/}/*------------------------ ------------*/
/*----------------Volume*/else{/*-------------------------- ---------------*/
if(!$_post['filesize'])
{$msgs[]="Please fill in the backup file volume size"; 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[]="Table-".$_post['tablename']."-Volume-".$p."-Data backup is completed, and the backup file '$backup/$filename'" is generated;
else $msgs[]="Backup table-".$_post['tablename']."-Failed";
$p++;
$filename=date("ymd",time())."_".$_post['tablename'];
$sql="";}
}
if($sql!=""){$filename.=("_v".$p.".sql");
if(write_file($sql,$filename))
$msgs[]="Table-".$_post['tablename']."-Volume-".$p."-Data backup is completed and the backup file '$backup/$filename'" is generated;}
show_msg($msgs);
/*----------End of volume*/}/*-------------------------------- ------------------*/
/*----------End of backup single table*/}/*-------------------------------- ------------------*/
/*---*/}/*-------------End of main program--------------------- ---------------------*/
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."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 "
".$title." |
while (list($k,$v)=each($msgs)) { echo " } echo " |
}
function pageend()
{
exit();
}
?>