function convert_dbtable_history($db_config, $history_out_path) { $db = new mysqli($db_config['db_host'], $db_config['db_user'], $db_config['db_passwd'], $db_config['dbname'], $db_config['db_port']); $latest_gets = NULL; $uptime = time(); $out_file = "{$history_out_path}/{$db_config['dbname']}_{$db_config['tblname']}.history"; exec("rm -f {$out_file} && touch {$out_file}"); do { db_get_history_rows($db, $db_config, $latest_gets); if (!empty($latest_gets) && !empty($latest_gets['rows'])) { foreach ($latest_gets['rows'] as $one_row) { $hive_item_key = $latest_gets['dbtbl_prefix']; foreach ($db_config['hive_item_key'] as $idk => $idv) { $hive_item_key = $hive_item_key . "" . $one_row[intval($idk) - 1]; } $out_line = "{$hive_item_key}" . "{$uptime}" . 0; for ($i = 0; $i < count($one_row); $i++) { $attr = table_column_callback($latest_gets['dbtbl_prefix'], $db_config['dbname'], $db_config['tblname'], $one_row[$i], $i + 1); $out_line = $out_line . "" . $attr; } list($part_db_name, $part_tbl_name) = explode(".", $latest_gets['dbtbl_prefix'], 2); if ($db_config['db_partitions'] > 1) { $out_line = $out_line . "" . $part_db_name; } else { if ($db_config['tbl_partitions'] > 1) { $out_line = $out_line . "" . $part_tbl_name; } } $out_line = table_row_callback($db_config['dbname'], $db_config['tblname'], $out_line); file_put_contents($out_file, $out_line . "\n", FILE_APPEND | LOCK_EX); } } } while (!empty($latest_gets) && !empty($latest_gets['rows'])); return $out_file; }
function parse_binlog_rows($binlog_file, $cfg, $out_file) { write2log("parse {$binlog_file}"); $fd = fopen($binlog_file, "r"); $unix_time_update = 0; $db_name = $cfg['dbname']; $tbl_name = $cfg['tblname']; $dbtbl_parttern = $cfg['db_tbl_pattern']; $hive_keys = $cfg['hive_item_key']; $tbl_pattern = preg_replace("/\\%d/", "\\d+", $dbtbl_parttern); $_pattern = sprintf($tbl_pattern, $db_name, $tbl_name); while (!feof($fd)) { $line = ""; $line = fgets($fd); if (empty($line)) { break; } while (!feof($fd) && preg_match("/^### /", $line, $matches)) { if (preg_match("/^### (UPDATE|INSERT INTO) ({$_pattern})\$/", $line, $matches)) { $set_flag = false; $hive_item_key = trim($matches[2]); $db_tbl_str = trim($matches[2]); while (!feof($fd)) { $line = fgets($fd); if (preg_match("/^### SET\$/", $line, $matches)) { $set_flag = true; break; } if (!preg_match("/^### /", $line, $matches)) { break; } } $out_line = "{$unix_time_update}" . 0; while ($set_flag && !feof($fd)) { $line = fgets($fd); if (preg_match("/^### @(\\d+)=(.*)\$/", $line, $matches)) { if (array_key_exists(intval($matches[1]), $hive_keys)) { $hive_item_key = $hive_item_key . "" . $matches[2]; } $attr = table_column_callback($db_tbl_str, $db_name, $tbl_name, $matches[2], $matches[1]); $out_line = $out_line . "" . $attr; } else { break; } } $out_line = "{$hive_item_key}" . $out_line; list($part_db_name, $part_tbl_name) = explode(".", $db_tbl_str, 2); if ($cfg['db_partitions'] > 1) { $out_line = $out_line . "" . $part_db_name; } else { if ($cfg['tbl_partitions'] > 1) { $out_line = $out_line . "" . $part_tbl_name; } } $out_line = table_row_callback($db_name, $tbl_name, $out_line, "UPINS"); if (!empty($out_line)) { file_put_contents($out_file, $out_line . "\n", FILE_APPEND | LOCK_EX); } } else { if (preg_match("/^### (DELETE FROM) ({$_pattern})\$/", $line, $matches)) { $set_flag = false; $hive_item_key = trim($matches[2]); $db_tbl_str = trim($matches[2]); while (!feof($fd)) { $line = fgets($fd); if (preg_match("/^### WHERE\$/", $line, $matches)) { $set_flag = true; break; } if (!preg_match("/^### /", $line, $matches)) { break; } } $out_line = "{$unix_time_update}" . 1; while ($set_flag && !feof($fd)) { $line = fgets($fd); if (preg_match("/^### @(\\d+)=(.*)\$/", $line, $matches)) { if (array_key_exists(intval($matches[1]), $hive_keys)) { $hive_item_key = $hive_item_key . "" . $matches[2]; } $attr = table_column_callback($db_tbl_str, $db_name, $tbl_name, $matches[2], $matches[1]); $out_line = $out_line . "" . $attr; } else { break; } } $out_line = "{$hive_item_key}" . $out_line; $out_line = table_row_callback($db_name, $tbl_name, $out_line, "DEL"); if (!empty($out_line)) { file_put_contents($out_file, $out_line . "\n", FILE_APPEND | LOCK_EX); } } } $line = ""; $line = fgets($fd); if (empty($line)) { break; } } if (preg_match("/^#(\\d\\d\\d\\d\\d\\d ( |\\d)\\d:\\d\\d:\\d\\d).*end_log_pos (\\d+)/", $line, $matches)) { $unix_time_update = str2time($matches[1]) . $matches[3]; } } fclose($fd); write2log("parse {$binlog_file} finish"); return $out_file; }