jspDatabase Backup匯出代碼

來源:互聯網
上載者:User

jspDatabase Backup匯出代碼
 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 ".'Database Backup完畢!', '?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');

  }

 }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.