Home Backend Development PHP Tutorial php备份mysql数据库

php备份mysql数据库

Jun 23, 2016 pm 01:36 PM

<?php   /*程序功能:mysql数据库备份功能*/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;   }
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles