PHP backup MySQL Database

Source: Internet
Author: User

<?PHP/*program function: MySQL database backup function*/Ini_set(' Max_execution_time ', ' 0 ');Ini_set(' Memory_limit ', ' 1024M ');//Plus these two lines, you can back up big data, last night measured a company's 600M table, can soon be backed up successfully$code _type= ' GBK ';Header("content-type:text/html;charset=$code _type");Header("Content-disposition:filename=backup.sql");//The file name that you savedHeader("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";//Database name$flag= ' 1 ';//1: Full backup, 2: Back up table structure only, 3: Back up data onlyEchoBack_database ($dbname,$conn,$flag);//Backup Database//start BackupfunctionBack_database ($dbname,$conn,$flag= ' 1 '){$crlf= "\ r \ n";$str= ' ';$now=Date(' Y year m D Day H point I min s S ', Time());$str.=   "$crlf--$crlf--Database: '$dbname`$crlf--$crlf--Export Date: '$now`$crlf--$crlf";$str.=   "$crlf--$crlf--Author: * * *$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);//total number of tables$i= 0; while($i<$num _tables){//loop all the tables$table=Mysql_tablename($tables,$i); //Backup Table Structureif($flag= = ' 1 ' or$flag= = ' 2 '){$query=mysql_query("SHOW CREATE TABLE$table");$row=Mysql_fetch_row($query);$str.=  "-- --------------------------------------------------------$crlf $crlf";$str.=  "--$crlf--The structure of the table '$table`$crlf--$crlf";$str.=$row[1]. ";$crlf--$crlf";}//Back up table contentsif($flag= = ' 1 ' or$flag= = ' 3 '){$str.= "--$crlf--Export the data in the table '$table`$crlf--$crlf";$str. = Get_table_content ($dbname,$table);$str.= "$crlf $crlf";}$i++; }return $str;}//get the records in the table SQLfunctionGet_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$tableVALUES (";  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 backup MySQL Database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.