php備份mysql資料庫

來源:互聯網
上載者:User

標籤:

 

<?php   /*程式功能:mysqlDatabase Backup功能*/ini_set(‘max_execution_time‘,‘0‘);ini_set(‘memory_limit‘,‘1024M‘);//加上這兩行,可以備份大資料,昨晚測了一下公司的一個600M的表,很快就可以備份成功$code_type = ‘gbk‘;header("Content-type:text/html;charset=$code_type");header("Content-disposition:   filename=backup.sql");//所儲存的檔案名稱   header("Content-type:   application/octetstream");   header("Pragma:   no-cache");   header("Expires:   0");   $conn   =   mysql_connect("localhost","root","root");   $db   =   mysql_select_db($dbname,$conn);   mysql_query("set names $code_type");$dbname="test";//資料庫名$flag  = ‘1‘;//1:全部備份,2:只備份表結構,3:只備份資料echo back_dataBase($dbname,$conn,$flag);//備份資料庫//開始備份function back_database($dbname,$conn,$flag=‘1‘){$crlf="\r\n";$str = ‘‘;$now = date(‘Y年m月d日H點i分s秒‘,time());$str .=   "$crlf--$crlf-- 資料庫: `$dbname`$crlf--$crlf-- 匯出日期: `$now`$crlf--$crlf";$str .=   "$crlf--$crlf-- 作者: ****$crlf--$crlf-- QQ: 1019822077$crlf--$crlf";$str .=   "-- blog: http://hi.baidu.com/woaidelphi/blog$crlf--$crlf";$str .=   "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";$crlf";$tables       =   mysql_list_tables($dbname,$conn);   $num_tables   =   mysql_numrows($tables);//表的總數 $i   =   0;while($i<$num_tables){//迴圈所有的表     $table =   mysql_tablename($tables,$i);   //備份表結構if($flag==‘1‘ or $flag==‘2‘){$query =   mysql_query("SHOW CREATE TABLE $table");$row   =   mysql_fetch_row($query);$str .=  "-- --------------------------------------------------------$crlf$crlf";$str .=  "--$crlf-- 表的結構 `$table` $crlf--$crlf";$str .=  $row[1].";$crlf--$crlf";}//備份表內容 if($flag==‘1‘ or $flag==‘3‘){$str .= "--$crlf-- 匯出表中的資料 `$table` $crlf--$crlf";$str .=  get_table_content($dbname,$table);$str .= "$crlf$crlf";}$i++;   }return $str;}//得到表中的記錄sql   function   get_table_content($dbname,$table){   $crlf            =   "\r\n";$schema_create   =   "";   $temp            =   "";   $result          =   mysql_db_query($dbname,"SELECT   *   FROM   $table");   $i               =   0;   while($row   =   mysql_fetch_row($result)){   $schema_insert   =   "INSERT   INTO   $table   VALUES   (";   for($j=0;   $j<mysql_num_fields($result);$j++){   if(!isset($row[$j]))   $schema_insert   .=   "   NULL,";   elseif($row[$j]   !=   "")   $schema_insert   .=   "   ‘".addslashes($row[$j])."‘,";   else   $schema_insert   .=   "   ‘‘,";   }   $schema_insert   =   ereg_replace(",$",   "",   $schema_insert);   $schema_insert   .=   ");$crlf";   $temp   =   $temp.$schema_insert   ;   $i++;   }   return   $temp;   }

 

php備份mysql資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.