# ----------------------------------------------------------------------------------------
# 功能:初始化binlog里的所有表名和列名,用全局字典result_dict来储存每个表有哪些列
# ----------------------------------------------------------------------------------------
def init_col_name():
global result_dict
global pri_dict
global fileContent
result_dict = {}
pri_dict = {}
table_list = re.findall(‘`.*`\\。`.*`’,fileContent)
table_list = list(set(table_list))
#table_list 为所有在这段binlog里出现过的表
print “正在初始化列名。..。.”
for table in table_list:
sname = table.split(‘。’)[0].replace(‘`’,‘’)
tname = table.split(‘。’)[1].replace(‘`’,‘’)
#连接数据库获取列和列id
try:
conn = MySQLdb.connect(host=host,user=user,passwd=password,port=int(port))
cursor = conn.cursor()
cursor.execute(“select ordinal_position,column_name \
from information_schema.columns \
where table_schema=‘%s’ and table_name=‘%s’ ” %(sname,tname))
result=cursor.fetchall()
if result == () :
print ‘Warning:’+sname+‘。’+tname+‘已删除’
#sys.exit()
result_dict[sname+‘。’+tname]=result
cursor.execute(“select ordinal_position,column_name \
from information_schema.columns \
where table_schema=‘%s’ and table_name=‘%s’ and column_key=‘PRI’ ” %(sname,tname))
pri=cursor.fetchall()
#print pri
pri_dict[sname+‘。’+tname]=pri
cursor.close()
conn.close()
except MySQLdb.Error, e:
try:
print “Error %d:%s” % (e.args[0], e.args[1])
except IndexError:
print “MySQL Error:%s” % str(e)
sys.exit()
#print result_dict
#print pri_dict
# ----------------------------------------------------------------------------------------
# 功能:拼凑回滚sql,逆序
# ----------------------------------------------------------------------------------------
def gen_rollback_sql():
global only_primary
fileOutput = open(output, ‘w’)
#先将文件根据‘--’分块,每块代表一个sql
area_list=fileContent.split(‘--\n’)
#逆序读取分块
print “正在开始拼凑sql.。..。”
for area in area_list[::-1]:
#由于一条sql可能影响多行,每个sql又可以分成多个逐条执行的sql
sql_list = area.split(‘##’)
#先将pos点和timestamp传入输出文件中
for sql_head in sql_list[0].splitlines():
sql_head = ‘#’+sql_head+‘\n’
fileOutput.write(sql_head)
#逐条sql进行替换更新,逆序
for sql in sql_list[::-1][0:-1]:
try:
if sql.split()[0] == ‘INSERT’:
rollback_sql = re.sub(‘^INSERT INTO’, ‘DELETE FROM’, sql, 1)
rollback_sql = re.sub(‘SET\n’, ‘WHERE\n’, rollback_sql, 1)
tablename_pos = 2
table_name = rollback_sql.split()[tablename_pos].replace(‘`’, ‘’)
# 获取该sql中的所有列
col_list = sorted(list(set(re.findall(‘@\d+’, rollback_sql))))
# 因为第一个列前面没有逗号或者and,所以单独替换
rollback_sql = rollback_sql.replace(‘@1=’, result_dict[table_name][0][1]+‘=’)
for col in col_list[1:]:
i = int(col[1:]) - 1
rollback_sql = rollback_sql.replace(col+‘=’, ‘AND ’ + result_dict[table_name][i][1]+‘=’,1)
# 如果only_primary开启且存在主键,where条件里就只列出主键字段
if int(only_primary) == 1 and pri_dict[table_name] 《》 ():
sub_where = ‘’
for primary in pri_dict[table_name]:
primary_name = primary[1]
for condition in rollback_sql.split(‘WHERE’, 1)[1].splitlines():
if re.compile(‘^\s*’+primary_name).match(condition) or re.compile(‘^\s*AND\s*’+primary_name).match(condition):
sub_where = sub_where + condition + ‘\n’
sub_where = re.sub(‘^\s*AND’, ‘’, sub_where, 1)
rollback_sql = rollback_sql.split(‘WHERE’, 1)[0] + ‘WHERE\n’ + sub_where
if sql.split()[0] == ‘UPDATE’:
rollback_sql = re.sub(‘SET\n’, ‘#SET#\n’, sql, 1)
rollback_sql = re.sub(‘WHERE\n’, ‘SET\n’, rollback_sql, 1)
rollback_sql = re.sub(‘#SET#\n’, ‘WHERE\n’, rollback_sql, 1)
tablename_pos = 1
table_name = rollback_sql.split()[tablename_pos].replace(‘`’, ‘’)
# 获取该sql中的所有列
col_list = sorted(list(set(re.findall(‘@\d+’, rollback_sql))))
# 因为第一个列前面没有逗号或者and,所以单独替换
rollback_sql = rollback_sql.replace(‘@1=’, result_dict[table_name][0][1] + ‘=’)
for col in col_list[1:]:
i = int(col[1:]) - 1
rollback_sql = rollback_sql.replace(col+‘=’, ‘,’ + result_dict[table_name][i][1]+‘=’, 1).replace(col+‘=’,‘AND ’ +result_dict[table_name][i][1]+‘=’)
# 如果only_primary开启且存在主键,where条件里就只列出主键字段
if int(only_primary) == 1 and pri_dict[table_name] 《》 ():
sub_where = ‘’
for primary in pri_dict[table_name]:
primary_name = primary[1]
for condition in rollback_sql.split(‘WHERE’, 1)[1].splitlines():
if re.compile(‘^\s*’ + primary_name).match(condition) or re.compile(‘^\s*AND\s*’+primary_name).match(condition):
sub_where = sub_where + condition + ‘\n’
sub_where = re.sub(‘^\s*AND’, ‘’, sub_where, 1)
rollback_sql = rollback_sql.split(‘WHERE’, 1)[0] + ‘WHERE\n’ + sub_where
if sql.split()[0] == ‘DELETE’:
rollback_sql = re.sub(‘^DELETE FROM’, ‘INSERT INTO’, sql, 1)
rollback_sql = re.sub(‘WHERE\n’, ‘SET\n’, rollback_sql, 1)
tablename_pos = 2
table_name = rollback_sql.split()[tablename_pos].replace(‘`’, ‘’)
评论
查看更多