日期:2014-05-16  浏览次数:20822 次

从MYSQL的binlog恢复指定的SQL语句,解决中文乱码问题。
系统错误导致3天中的部分数据被错误覆盖,只能从MYSQL的binlog中恢复数据。
1、使用mysqlbinlog工具将binlog文件转换为sql文件:
    mysqlbinlog sd-bin.000040 > a.sql
    mysqlbinlog sd-bin.000041 >> a.sql
2、遍历a.sql的所有语句:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class SQLparser {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		if (args == null || args.length != 1) {
			System.out.println("error on args");
			return;
		}
		File file = new File(args[0]);
		if (!file.isFile()) {
			System.out.println("error arg[0] not a file");
			return;
		}

		System.out.println(file.getName() + "BEGIN!");
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(new File("d:\\parsed.sql")), "utf-8"));

		BufferedReader br = new BufferedReader(new InputStreamReader(
				new FileInputStream(file), "utf-8"/* 指定源文件的字符集编码 */));

		String line = br.readLine();
		while (line != null) {
			if (line.contains(", c_dzd.voucher='"/* 匹配需要留下的SQL片段 */)) {
				System.out.println(line);
				bw.append(line + ";\n"/* 追加分号和换行 */);
			}
			line = br.readLine();
		}
		br.close();
		bw.close();
		System.out.println(file.getName() + "OK!");
	}

}