【www.gdgbn.com--Action】

jsp数据库备份导出代码
 function export($tables,$sqlcompat,$sqlcharset,$sizelimit,$action,$fileid,$random,$tableid,$startfrom,$tabletype)

 {

  $dumpcharset = $sqlcharset ? $sqlcharset : str_replace("-", "", CHARSET);

  $fileid = isset($fileid) ? $fileid : 1;

  if($fileid==1 && $tables)

  {

   if(!isset($tables) || !is_array($tables)) showmessage("select_bak_table");

   $random = mt_rand(1000, 9999);

   cache_write("bakup_tables.php", $tables);

  }

  else

  {

   if(!$tables = cache_read("bakup_tables.php")) showmessage("select_bak_table");

  }

  if($this->db->version() > "4.1")

  {

   if($sqlcharset)

   {

    $this->db->query("SET NAMES "".$sqlcharset."";nn");

   }

   if($sqlcompat == "MYSQL40")

   {

    $this->db->query("SET SQL_MODE="MYSQL40"");

   }

   elseif($sqlcompat == "MYSQL41")

   {

    $this->db->query("SET SQL_MODE=""");

   }

  }

  $tabledump = "";

  $tableid = isset($tableid) ? $tableid - 1 : 0;

  $startfrom = isset($startfrom) ? intval($startfrom) : 0;

  for($i = $tableid; $i < count($tables) && strlen($tabledump) < $sizelimit * 1000; $i++)

  {

   global $startrow;

   $offset = 100;

   if(!$startfrom)

   {

    $tabledump .= "DROP TABLE IF EXISTS `$tables[$i]`;n";

    $createtable = $this->db->query("SHOW CREATE TABLE `$tables[$i]` ");

    $create = $this->db->fetch_row($createtable);

    $tabledump .= $create[1].";nn";

    if($sqlcompat == "MYSQL41" && $this->db->version() < "4.1")

    {

     $tabledump = preg_replace("/TYPE=([a-zA-Z0-9]+)/", "ENGINE=\1 DEFAULT CHARSET=".$dumpcharset, $tabledump);

    }

    if($this->db->version() > "4.1" && $sqlcharset)

    {

     $tabledump = preg_replace("/(DEFAULT)*s*CHARSET=[a-zA-Z0-9]+/", "DEFAULT CHARSET=".$sqlcharset, $tabledump);

    }

   }

 

   $numrows = $offset;

   while(strlen($tabledump) < $sizelimit * 1000 && $numrows == $offset)

   {

    $rows = $this->db->query("SELECT * FROM `$tables[$i]` LIMIT $startfrom, $offset");

    $numfields = $this->db->num_fields($rows);

    $numrows = $this->db->num_rows($rows);

    while ($row = $this->db->fetch_row($rows))

    {

     $comma = "";

     $tabledump .= "INSERT INTO `$tables[$i]` VALUES(";

     for($j = 0; $j < $numfields; $j++)

     {

      $tabledump .= $comma.""".mysql_escape_string($row[$j]).""";

      $comma = ",";

     }

     $tabledump .= ");n";

    }

    $this->db->free_result($rows);

    $startfrom += $offset;

   }

   $tabledump .= "n";

   $startrow = $startfrom;

   $startfrom = 0;

  }

 

  if(trim($tabledump))

  {

   $tabledump = "#bakfilen# version:COUVOLn# time:".date("Y-m-d H:i:s")."n# type:hl_databasen# email:263645928@qq.comn# sourc:http://www.u731.com/?1n# --------------------------------------------------------nnn".$tabledump;

   $tableid = $i;

   $filename = "hl_".date("Ymd")."_".$random."_".$fileid.".sql";

   $fileid++;

   $bakfile = PHPCMS_ROOT."/data/bakup/".$filename;

   if(!is_writable(PHPCMS_ROOT."/data/bakup/")) showmessage("数据无法备份到服务器!请检查 ".$bakfile." 目录是否可写", $forward);

   file_put_contents($bakfile, $tabledump);

   @chmod($bakfile, 0777);

   showmessage("bak_file"." $filename "."数据库备份完毕!", "?mod=".$this->mod."&file=".$this->file."&action=".$action."&sizelimit=".$sizelimit."&sqlcompat=".$sqlcompat."&sqlcharset=".$sqlcharset."&tableid=".$tableid."&fileid=".$fileid."&startfrom=".$startrow."&random=".$random."&dosubmit=1&tabletype=".$tabletype);

  }

  else

  {

     cache_delete("bakup_tables.php");

     showmessage("database_bak_success","?file=database&action=export");

  }

 }

本文来源:http://www.gdgbn.com/flash/21436/