当前位置: 首页 > news >正文

记录一次数据恢复,mysql8 - 义美

SHOW VARIABLES LIKE 'log_bin_basename';

Variable_name Value
log_bin_basename E:\environment\mysql-8.0.41-winx64\data\binlog

SHOW MASTER STATUS;

File Position Binlog_Do_DB Binlog_Ignore_DB Executed_Gtid_Set
binlog.000046 1889087

保存日志,(E:\environment\mysql-8.0.41-winx64\data\binlog.000046  这个是日志的路径)
mysqlbinlog --no-defaults --start-datetime="2025-11-03 16:40:00" --stop-datetime="2025-11-03 17:10:00" --base64-output=DECODE-ROWS -v "E:\environment\mysql-8.0.41-winx64\data\binlog.000046" > "E:\recovery_data1.sql"

import redef generate_recovery_sql(binlog_file_path, output_file_path):with open(binlog_file_path, 'r', encoding='utf-16') as f_in:binlog_content = f_in.read()# Regex to find DELETE statements and capture the SET_VAR lines that contain the column values# We look for '### DELETE FROM `mall_wxyoupin_com`.`yoshop_upload_file`' followed by '### WHERE' and then '### SET_VAR' linesdelete_pattern = re.compile(r'### DELETE FROM `mall_wxyoupin_com`\.`yoshop_upload_file`\n'r'### WHERE\n'r'(.*?)(?=\n### @|\n# at |$)',re.DOTALL)# Column mapping based on the table structure and @ variable indicescolumn_mapping = {1: 'file_id',2: 'group_id',3: 'channel',4: 'storage',5: 'domain',6: 'file_type',7: 'file_name',8: 'file_path',9: 'file_size',10: 'file_ext',11: 'cover',12: 'uploader_id',13: 'is_recycle',14: 'is_delete',15: 'store_id',16: 'create_time',17: 'update_time',}recovery_statements = []for match in delete_pattern.finditer(binlog_content):where_clause_block = match.group(1)# Extract all @ variables and their values from the WHERE clause blockset_var_matches = re.findall(r'###   @(\d+)=([\s\S]*?)(?=\n###   @|\n)', where_clause_block)row_values = {}for var_index_str, var_value_str in set_var_matches:var_index = int(var_index_str)# Clean up the value string: remove leading/trailing spaces and quotes if presentvar_value = var_value_str.strip()if var_value.startswith("''") and var_value.endswith("''"):var_value = var_value[1:-1] # Remove outer single quoteselif var_value.startswith("'") and var_value.endswith("'"):var_value = var_value[1:-1] # Remove outer single quotesrow_values[var_index] = var_value# Construct the INSERT statementcolumns = []values = []for i in range(1, len(column_mapping) + 1):if i in row_values:columns.append(f'`{column_mapping[i]}`')# Handle string values by quoting them, numeric values directlyif isinstance(row_values[i], str) and not row_values[i].isdigit():values.append(f"'{row_values[i].replace("'", "''")}'") # Escape single quotes within stringelse:values.append(str(row_values[i]))else:# If a column is not found in the DELETE statement, it might be NULL or have a default value.# For recovery, it's safer to explicitly set it to NULL or its default if known.# For simplicity, we'll assume all columns are present in the DELETE's WHERE clause for now.# If not, this part needs more sophisticated handling based on schema defaults.pass # This case should ideally not happen if all columns are in the WHERE clauseif columns and values:insert_statement = f"INSERT INTO `mall_wxyoupin_com`.`yoshop_upload_file` ({', '.join(columns)}) VALUES ({', '.join(values)});"recovery_statements.append(insert_statement)with open(output_file_path, 'w', encoding='utf-8') as f_out:for statement in recovery_statements:f_out.write(statement + '\\n')return len(recovery_statements)if __name__ == "__main__":binlog_file = 'd:/phpstudy_pro/WWW/yp/yoshop2.0/数据库修改记录/圈子/recovery_data1.sql'output_sql_file = 'd:/phpstudy_pro/WWW/yp/yoshop2.0/数据库修改记录/圈子/recovery_yoshop_upload_file.sql'print(f"Starting recovery SQL generation from {binlog_file}...")num_statements = generate_recovery_sql(binlog_file, output_sql_file)print(f"Generated {num_statements} INSERT statements into {output_sql_file}")print("Recovery SQL generation complete. Please review the generated SQL file before executing it.")

 



python d:/phpstudy_pro/WWW/yp/yoshop2.0/generate_recovery_sql.py 执行会得到 recovery_yoshop_upload_file.sql


http://www.zskr.cn/news/38805.html

相关文章:

  • 2025年新能源水冷电机壳铝合金浇铸机批发厂家权威推荐榜单:户外围墙配件铝合金浇铸机/厨具锅铝合金浇铸机/手套模具铝合金浇铸机源头厂家精选
  • 2025 年 11 月石墨烯,可膨胀石墨,导热石墨母粒厂家最新推荐,产能、专利、环保三维数据透视!
  • Cisco Jabber 15.1 (Andriod, iOS, macOS, Windows) - 面向企业的多合一通信工具
  • 2025年青石栏杆制造厂权威推荐榜单:别墅石栏杆/石栏杆/河道石栏杆源头厂家精选
  • 2025年手动叠片过滤器生产厂家权威推荐榜单:全自动反冲洗叠片过滤器/离心过滤器/钢制离心过滤器设备源头厂家精选
  • 企业热线电话系统的多渠道支持与服务拓展策略!
  • DockerDeskTop安装常用的中间件
  • 2025高性价比Facebook广告品牌企业TOP5推荐:精准引流与高效转化的权威测评指南
  • 如何优化机器人拨打电话软件的通话效率?实用技巧!
  • 2025 年工业商城小程序最新推荐排行榜:涵盖多领域设备,解析实力企业核心优势与选择要点节能环保/车间工具/智能制造/数控转台工业商城推荐
  • SAP 字段名+RANGE表
  • 系统关键信息收集
  • 2025年金属线材加工设备企业排名:江苏优轧机械有限公司
  • 成都恒利泰滤波器HT-LFCG-490+
  • Photoshop 新伴侣!ACR 2026 五大新功能实战教学(附使用技巧)
  • 2025 年塔钟生产厂家最新推荐榜单:电子 / 户外 / 建筑等多类型塔钟品牌实测,核心机芯与服务深度解析
  • 2025年钢格栅生产商哪家好?中国十大钢格栅厂家年度排名权威测评与深度解析
  • 免登录使用 WPS Office 编辑功能
  • 2025年装配式卸落块定做厂家权威推荐榜单:桥梁落架卸落块/100吨卸落块/200吨卸落块源头厂家精选
  • 2025年黑龙江高考美术适用真题教材辅导学校TOP5排行榜,艺考生择校避坑指南
  • 治愈的极简风大片by NanoBanana
  • 2025年11月十大征拆服务机构排名:盛廷律师事务所领跑专业法律服
  • 「Diary Solution Set」November 2025
  • 《代码大全》读后感(4)
  • 安全的轮胎哪家好:最新测评与精选推荐
  • 基于MATLAB的光机耦合仿真中光学镜面变形分析及泽尼克多项式耦合的实现
  • 2025年重庆粉尘处理公司权威推荐榜单:重庆各类粉尘收集/重庆处理粉尘/重庆粉尘治理设备厂家精选
  • 磁盘空间深度检查脚本
  • OceanBase数据库(Oracle模式)从零开始
  • P14361 [CSP-S 2025] 社团招新 / club 题解