zoukankan      html  css  js  c++  java
  • Extractor: 一个MySQL数据库备份工具

    因为要在Ant中导出MySQL数据库,写了mysql_extractor类,供大家参考。

     

    extractor.php: 

    if (!empty($_SERVER['argv'])) {
        for ($i = 1; $i < $_SERVER['argc']; $i++) {
            list($k, $v) = explode('=', $_SERVER['argv'][$i]);
            $_GET[$k] = $v;
        }
    }
    
    if (empty($_GET['table_prefix'])) {
        $_GET['table_prefix'] = '';
    }
    
    if (empty($_GET['output_file'])) {
        $_GET['output_file'] = '';
    }
    
    include_once(dirname(__FILE__) . '/../config.php');
    
    $db = mysql_connect(DB_HOST . ':' . DB_PORT, DB_USER, DB_PASS);
    if (!$db) {
        die('[' . mysql_errno() . '] ' . mysql_error());
    }
    
    if (!mysql_select_db(DB_NAME, $db)) {
        die('[' . mysql_errno() . '] ' . mysql_error());
    }
            
    mysql_query("SET NAMES 'utf8'", $db);
    mysql_query("SET NAMES 'utf8'", $db);
    
    $extractor = new mysql_extractor($db, $_GET['output_file']);
    
    $extractor->write_start(DB_PREFIX);
    
    $result = mysql_query("SHOW TABLE STATUS WHERE name like '" . DB_PREFIX . $_GET['table_prefix'] . "%'", $db);
    while (false !== ($row = mysql_fetch_array($result, MYSQL_NUM))) {
        $extractor->write_table($row[0]);
        $extractor->write_data($row[0]);
    }
    mysql_free_result($result);
    
    $extractor->write_end();
    
    class mysql_extractor
    {
        var $db;
        var $fp;
    
        public function __construct($db, $output_file)
        {
            $this->db = $db;
            
            if (!empty($output_file)) {
                $this->fp = fopen($output_file, 'w');
    
                if (!$this->fp) {
                    trigger_error('FILE_WRITE_FAIL', E_USER_ERROR);
                }
            }
        }
    
        public function write_start($table_prefix)
        {
            $sql_data = "#\n";
            $sql_data .= "# Omnitrix Backup Script\n";
            $sql_data .= "# Dump of tables for $table_prefix\n";
            $sql_data .= "# DATE : " . gmdate("d-m-Y H:i:s", time()) . " GMT\n";
            $sql_data .= "#\n\n";
            $this->flush($sql_data);
        }
    
        public function write_table($table_name)
        {
            $sql = 'SHOW CREATE TABLE ' . $table_name;
            $result = mysql_query($sql, $this->db);
            $row = mysql_fetch_array($result, MYSQL_ASSOC);
            mysql_free_result($result);
    
            $sql_data = "#\n";
            $sql_data .= "# Source for table $table_name \n";
            $sql_data .= "#\n";
            $sql_data .= "DROP TABLE IF EXISTS `$table_name`;\n";
            $this->flush($sql_data . $row['Create Table'] . ";\n\n");
        }
    
        public function write_data($table_name)
        {
            $sql_data = "#\n";
            $sql_data .= "# Dumping data for table $table_name \n";
            $sql_data .= "#\n";
            $this->flush($sql_data);
            
            $sql = "SELECT * FROM $table_name";
            $result = mysql_query($sql, $this->db);
            if ($result != false) {
                $fields_cnt = mysql_num_fields($result);
    
                // Get field information
                $field = $field_set = array();
                for ($j = 0; $j < $fields_cnt; $j++) {
                    $info = new stdClass();
                    $info->name = mysql_field_name($result, $j);
                    $info->type = mysql_field_type($result, $j);
                    $info->flags = mysql_field_flags($result, $j);
                    $field[$j] = $info;
                    
                    $field_set[] = $info->name;
                }
    
                $search            = array("\\", "'", "\x00", "\x0a", "\x0d", "\x1a", '"');
                $replace        = array("\\\\", "\\'", '\0', '\n', '\r', '\Z', '\\"');
    
                while (($row = mysql_fetch_row($result)) !== false) {
                    $values    = array();
    
                    for ($j = 0; $j < $fields_cnt; $j++) {
                        if (!isset($row[$j]) || is_null($row[$j])) {
                            $values[$j] = 'NULL';
                        } else if (($field[$j]->flags & 32768) && !($field[$j]->flags & 1024))
                        {
                            $values[$j] = $row[$j];
                        } else {
                            $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
                        }
                    }
                    
                    $query = 'INSERT INTO `' . $table_name . '` VALUES (' . implode(', ', $values) . ');';
                    $this->flush($query . "\n");
                }
                mysql_free_result($result);
                
                $this->flush("\n");
            }
        }
        
        public function write_end()
        {
            if ($this->fp) {
                fclose($this->fp);
            }    
        }
        
        public function flush($data)
        {
            if ($this->fp) {
                fwrite($this->fp, $data);
            } else {
                echo $data;            
            }
        }
    }

    在命令行执行:

    php -f extractor.php #直接输出
    php -f extractor.php output_file=omnitrix.sql #输出到文件
    php -f extractor.php output_file=omnitrix.sql table_prefix=system_user #限定指定开头的表

    在浏览器执行:

    http://localhost/omnitrix/utilities/extractor.php #直接输出
    http://localhost/omnitrix/utilities/extractor.php?output_file=omnitrix.sql #输出到文件
    http://localhost/omnitrix/utilities/extractor.php?output_file=omnitrix.sql&table_prefix=system_user #限定指定开头的表
  • 相关阅读:
    convert.c:7:3: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by
    80 多个 Linux 系统管理员必备的监控工具
    控件风格19种,必须倒背如流——其实就是控件所拥有的能力,即有条件使用VCL框架所提供的(功能)代码
    控件状态11种,必须倒背如流——记录控件当前的状态,防止误判(一般使用完以后就把状态改回去)
    FindChildControl与FindComponent
    保存网页为图片——滚动截取IE(WebBrowse)
    HDOJ 1755
    IP编辑控件(因为封装的是系统自带控件,所以也使用了CreateSubClass,不过为啥要封装CN_COMMAND和CN_NOTIFY不是很明白)
    QT之深入理解QThread
    Qt读取ANSI格式文件——利用QTextCodec将其他编码格式的QByteArray转换为Unicode格式,或者从文件中读出后直接做转换
  • 原文地址:https://www.cnblogs.com/eastson/p/2722125.html
Copyright © 2011-2022 走看看