mysql分卷备份程序

<? php
// 2007-9-4
/*

    数据库备份流程
    1.选择要备份的表,设置相关参数 提交
    2.循环要备份的表
        得到当前表 结构
        得到当前表 数据
        如果是分卷备份
        得到
    3.
        
*/
include ( " inc/mysql.php " );
include ( " inc/function.php " );
set_time_limit ( 0 );              // 设定该页最久执行时间。
define ( " PASSWD " , " ggg123 " );     // 设置的备份密码
$dsnArray      =   require ( ' config.php ' );
$db_obj   =   new  sql_db( $dsnArray [ ' host ' ] ,   $dsnArray [ ' user ' ] ,    $dsnArray [ ' pass ' ] ,   $dsnArray [ ' dbname ' ] ,   $dsnArray [ ' pconnect ' ]);
if  ( ! $db_obj )
{
    show_error(
" 连接数据库失败,请正确配置 config.php 文件! " );
    
die ();
}    
if (is_post())     // 提交表单后
{
    
if  ( $_POST [ ' password ' <>   constant ( " PASSWD " ))
    {
        show_error(
" 密码不正确! " );
        
die ();
    }        
    
if  ( empty ( $_POST [ ' tablename ' ]))
    {
        show_error(
" 请选择要备份的数据表 " );
        
die ();
    }    
    
if ( $_POST [ " is_vol " ])     // 分卷
     if  ( ! $_POST [ ' vol_size ' ])
    {
        show_error(
" 请填写备份文件分卷大小 " );
        
die ();
    }

$start_time      =  get_microtime();
$back_path      =   " ./backup/ " ;                     // 备份路径
$pre_file      =   $dsnArray [ ' dbname ' ] . date ( " Ymd " ,   time ());             // 文件名前辍
$exte_file      =   " .txt " ;                         // 文件扩展名
$file_name      =   $pre_file . $exte_file ;             // 保存成的文件名

$fp = fopen ( $back_path . $file_name ,   " wb+ " );
if ( ! $fp )
{    show_error(
$back_path . $file_name . "  文件打开失败! " );
    
die ();
}

// $_POST['tablename']    = array("ynjob_news","ynjob_puser","ynjob_company","ynjob_cjobinfo");
$sql = "" ;
$curr_len      =   0 ;     // 保存当前写入到文件的长度
$vol_numb      =   1 ;     // 保存当前卷序号
//$is_vol    = true;
//$vol_size    = 1000;                            //每卷文件大小 ?K

foreach ( $_POST [ ' tablename ' as   $table )     // 循环所有选中表
{
    
$table      =   trim ( $table );
    
if ( empty ( $table ))
        
continue ;
    
echo   " <br /> reading  " . $table . "  ...  " ;
    
// 写表结构到文件
     fwrite ( $fp , get_sql_struct( $db_obj , $table )); // 产生 DROP TABLE CREATE TABLE  代码 得到表结构
     $db_obj -> sql_query( " select * from $table " );     // 得到数据
     while  ( $record      =   $db_obj -> sql_fetchrow())
    {
        
$sql = get_sql_insert( $db_obj , $table ,   $record );     // 产生 INSERT 语句
         if ( $_POST [ " is_vol " ])     // 分卷
        {
            
$curr_len   +=   strlen ( $sql );
            
if ( $curr_len   >=   $_POST [ " vol_size " *   1000 )     // 超过大小 重新打开新卷
            {
                
fclose ( $fp );
                
echo   "  [save ok] <a href=' " . $back_path . $file_name . " ' target='_blank'> 下载 </a> " ;                
                
echo   " <br />... creat  vol  " . $vol_numb . "   " ;                
                
$file_name      =   $pre_file . " _v " . $vol_numb . $exte_file ;     // 保存成的文件名
                 $fp = fopen ( $back_path . $file_name ,   " wb+ " );
                
$vol_numb ++ ;
                
$curr_len   =   0 ;
            }
        }
        
fwrite ( $fp , $sql );
    }
    
// flush();
}
fclose ( $fp );
echo   "  [save ok] <a href=' " . $back_path . $file_name . " ' target='_blank'> 下载 </a><br/> " ;    
echo   " <script language=javascript>alert(" 备份结束! ");</script><a title=返回上一页 href=javascript:history.go(-1)>&lt;&lt;&lt;返回上一页</a> " ;

echo  pass_time( $start_time );
}
else  
{
?>
< form name = " form1 "  method = " post "  action = " <?=$_SERVER[ " PHP_SELF " ]?> " >
  
< table width = " 99% "  bordercolorlight = " #888888 "  bordercolordark = " #FFFFFF "   cellpadding = " 1 "  cellspacing = " 1 "  border = 1 >
    
< tr align = " center "   class = ' header ' >< td colspan = " 2 " > 数据备份 </ td ></ tr >
    
< tr >< td align = " center " >< select name = " tablename[] "  size = " 12 "  multiple = " MULTIPLE " >
          
< option value = "" >----------- 请选择要备份的表 -----------</ option >
          
<?
$db_obj -> sql_query( " show table status from  " . $dsnArray [ ' dbname ' ]);
while  ( $record      =   $db_obj -> sql_fetchrow())
{
    
echo " <option value=' " . $record [ ' Name ' ] . " '> " . $record [ ' Name ' ] . " </option> " ;
}

?>
        
</ select ></ td >< td >< table width = " 99% "  border = " 1 "   cellpadding = " 1 "  cellspacing = " 1 "  bordercolorlight = " #888888 "  bordercolordark = " #FFFFFF " >
          
< tr >
            
< td colspan = " 2 " > 使用分卷备份 </ td >
          
</ tr >
          
< tr >
            
< td colspan = " 2 " >< input type = " checkbox "  name = " is_vol "  value = 1   />
              分卷备份
              
< input name = " vol_size "  type = " text "  size = " 10 "  value = " 1000 "   />
              K
</ td >
          
</ tr >
          
< tr >
            
< td colspan = " 2 " > 选择目标位置 </ td >
          
</ tr >
          
< tr >
            
< td colspan = " 2 " >< input type = " radio "  name = " position "  value = " server "  checked = " checked "   />
              备份到服务器
</ td >
          
</ tr >
          
< tr >
            
< td colspan = " 2 " > 备份密码 </ td >
          
</ tr >
          
< tr >
            
< td colspan = " 2 " > 只有输入正确密码才能进行备份  < input name = " password "   type = " password "  size = " 10 "   />
              
</ td >
          
</ tr >
          
        
</ table ></ td ></ tr >
    
< tr >< td colspan = " 2 "  align = ' center ' >< input type = " submit "  name = " act "  value = " 备份 " ></ td ></ tr >
  
</ table >
</ form >
<?
}
$db_obj -> sql_close();
?>
 

你可能感兴趣的:(sql,mysql,File,table,Path,FP)