# -*- coding: utf-8 -*- # """ 1.输入表名,自动生成比对配置文件 2.执行sync-diff比对 3.记录比对日志 """ import random import string import os import re import subprocess import time from datetime import date, datetime from configparser import ConfigParser import ast import mysql.connector # import insert_testdata def f_generate_sync_config(table_schema='', table_name='', conf_dir='/tmp' ): """ generate toml config file :param table_schema: :param table_name: :return: """ print("%s [INFO] start generate o2t-sync-diff config for %s.%s" % ( time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()), table_schema, table_name)) config_text = ''' # Sync Diff Configuration. ######################### Global config ######################### log-level = "info" chunk-size = 100000 check-thread-count = 40 sample-percent = 100 use-checksum = true only-use-checksum = true use-checkpoint = false ignore-data-check = false ignore-struct-check = true fix-sql-file = "%s.%s_fix.sql" #fix-target = "Oracle" ######################### Tables config ######################### [[check-tables]] schema = "%s" oracle-schema = "%s" tables = ["%s"] ######################### Databases config ######################### [[source-db]] type = "Oracle" user = "%s" password = "%s" connectString = "127.0.0.1:1521/oratidb?connect_timeout=2" instance-id = "source-1" #charset = "zhs16gbk" [target-db] host = "127.0.0.1" port = 4100 user = "" password = "" instance-id = "source-2" ''' %(table_schema, table_name, table_schema, table_schema, table_name, table_schema, table_schema) column_name='col1' ignore_column_text = ''' ######################### Tables config ######################### [[table-config]] schema = "%s" table = "%s" ignore-columns = ["%s"] ''' %(table_schema, table_name, column_name) ignore_column_text = '' # print(config_text) config_name = '%s/%s_%s.toml' %(conf_dir, table_schema, table_name) # print(config_name) with open(config_name, 'w') as file: file.write(config_text+'\n'+ignore_column_text) return config_name def f_run_sync_diff(binary_path=".", config_file="", log_file=""): print("%s [INFO] start run o2t-sync-diff program,config=%s" % ( time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()), config_file)) shell_cmd = '%s/o2t_sync_diff -config %s > /dev/null ' %(binary_path, config_file) rt_code = os.system(shell_cmd) if rt_code == 0: print("%s [SUCC] run o2t-sync-diff success,retrun code %s" % ( time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()), rt_code)) else: print("%s [ERRO] run o2t-sync-diff failed,retrun code %s" % ( time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime()), rt_code)) print("%s [INFO] CMD: %s" %(time.strftime("%Y-%m-%d-%H.%M.%S", shell_cmd) ) def f_get_table_list(): stmt_output = ["bs_intr_acsetl_detail2", "ogg_checkpoint_table", "YUJIE1123B", "ogg_checkpoint_table", "ogg_checkpoint_table_lox" ] return stmt_output def main(): workDir = os.path.dirname(os.path.abspath(__file__)) confDir = "%s/sync_config" % (workDir) logDir = "%s/logs" % (workDir) if not os.path.exists(confDir): os.makedirs(confDir) if not os.path.exists(logDir): os.makedirs(logDir) tableSchema = "heronsdb1" tables_lst = f_get_table_list() for tableName in tables_lst: logFile = "%s/sync_%s_%s.log" % (logDir, tableSchema, tableName) confFile = f_generate_sync_config(table_name=tableName, table_schema=tableSchema, conf_dir=confDir) # print(confFile, type(confFile)) f_run_sync_diff(binary_path=workDir, config_file=confFile, log_file=logFile) if __name__ == '__main__': main()