柳暗花明笔记

    自写php同步DB之服务端

    所属分类:[ php&web ] 猫哲学家 发表于:2015-03-20 16:05 浏览:807

    <?php

    $post = $_POST;

    if (empty($post))
    {
    exit('Must post.');
    }

    // print_r($post);

    set_error_handler(array('dbsync_server', 'runtime_error_handler'));

    $param = $post;
    $param['success_log'] = './server-success.log';
    $param['error_log'] = './server-error.log';
    $param['mysql_bin_path'] = '/usr/local/mysql/bin/';

    dbsync_server::run($param);

     

    /**
    * Log file ./success.log ./error.log
    */
    class dbsync_server
    {
    public static $error_log_file;
    public static $mysql_bin_path;

    public static function run($param = array())
    {
    // Set variable
    $success_log_file = $param['success_log'];
    $error_log_file = $param['error_log'];
    $mysql_bin_path = $param['mysql_bin_path'];

    self::$error_log_file = $error_log_file;
    self::$mysql_bin_path = $mysql_bin_path;

    // Check log file
    if (is_file($success_log_file) and is_writable($success_log_file) and
    is_file($error_log_file) and is_writable($error_log_file))
    {
    // Do nothing
    }
    else
    {
    self::log($error_log_file, "Check '{$success_log_file}','{$error_log_file}' must exist and writable.");
    exit;
    }

    // Do
    $dbsync_server = new dbsync_server;
    $dbsync_server->{$param['api']}($param);

    }

    /**
    * Get all tables and total rows
    * @param $param('host'=>'127.0.0.1', 'user'=>'user', 'pass'=>'pass', 'db'=>'db')
    */
    public function get_all_tables_and_rows($param = array())
    {
    $result = array('status' => 0, 'data' => null);
    try
    {
    // mysql_pconnect 如果出现异常,try catch捕获不到的,晕,后来得知这个是运行时的错误,还有set_error_handler这个可以对付它,哈哈
    // $connection = mysql_pconnect($param['host'], $param['user'], $param['pass']) or $this->log(0, mysql_error());
    $connection = mysql_pconnect($param['host'], $param['user'], $param['pass']);

    mysql_select_db($param['db'], $connection); // Must have it

    // mysql_query("SET NAMES UTF8;", $connection);
    // $tables = mysql_list_tables($param['db'], $connection);
    $tables = mysql_query("show tables from `{$param['db']}`", $connection);

    $data = array();
    while ($table = mysql_fetch_row($tables)) {
    $tmp = array();
    $table_name = $table[0];
    $tmp['table_name'] = $table_name;

    // Get rows
    $row_rource = mysql_query("SELECT count(*) from `{$table_name}`", $connection);

    if($row_rource === FALSE)
    {
    self::log(self::$error_log_file, mysql_error());
    }

    $row = mysql_fetch_row($row_rource);
    $tmp['table_rows'] = $row[0];

    $command_result = shell_exec(self::$mysql_bin_path . "mysqldump -u{$param['user']} -p{$param['pass']} --no-data {$param['db']} {$table_name}");

    // $tmp['created_table'] = addslashes($command_result);
    $tmp['created_table'] = $command_result;

    $data[] = $tmp;
    }

    $result['status'] = 1;
    $result['data'] = $data;

    }
    catch (Exception $e)
    {
    self::log(self::$error_log_file, $e->getMessage());
    }

    $this->output($result);
    }

    /**
    * Get table data from page
    * @param $param = $param('host'=>'127.0.0.1', 'user'=>'user', 'pass'=>'pass', 'db'=>'db', 'table'=>'table', 'offset'=>'offset', 'rows'=>'rows')
    */
    public function get_table_data_by_page($param = array())
    {
    $result = array('status' => 0, 'data' => null);
    try
    {
    $command_result = shell_exec(self::$mysql_bin_path . "mysqldump -u{$param['user']} -p{$param['pass']} --no-create-info --opt --where='true limit {$param['offset']},{$param['rows']}' {$param['db']} {$param['table']}");

    $result['status'] = 1;
    $result['data'] = $command_result;

    }
    catch (Exception $e)
    {
    self::log(self::$error_log_file, $e->getMessage());
    }

    $this->output($result);
    }

    public function output($data = '')
    {
    die(json_encode($data));
    }

    public static function log($log_file, $mess = '')
    {
    error_log(date('[Y-m-d H:i:s]') . ' ' . print_r($mess, 1) . PHP_EOL, 3, $log_file);
    }

    public function runtime_error_handler($level, $string, $file, $line)
    {
    throw new Exception("{$level}|{$string} in {$file} on line {$line}");
    }


    }

     

     

    ?>

    标签: php 
    上一编文章 linux下查看命令行模式下的php运行进程 下一编文章 自写php同步DB之客户端
    最近文章
    发表评论