zoukankan      html  css  js  c++  java
  • php备份mysql数据库

    <?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="
    ";
    $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            =   "
    ";
    $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;   
    }
  • 相关阅读:
    SZU:B47 Big Integer II
    Plan : 破晓
    C程序设计语言(第二版)习题:第二章
    Linux : fedora 安装 vnc server
    Linux系统编程:客户端-服务器用FIFO进行通信
    Linux系统编程:dup2()重定向
    Vijos: P1046观光旅游
    FLOYD 求最小环
    uva 401.Palindromes
    codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers
  • 原文地址:https://www.cnblogs.com/yhdsir/p/5528975.html
Copyright © 2011-2022 走看看