Created
May 16, 2023 23:27
-
-
Save ahmedrowaihi/5d61e0afe5461eee3411489ec5b88d93 to your computer and use it in GitHub Desktop.
MySQL binlog filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
const binlogFile = "allbinglog.sql"; // bin log file name | |
const db = ""; // db name | |
const table = ""; // table | |
const outputFile = "output.sql"; | |
const pattern = new RegExp( | |
`### (UPDATE|INSERT INTO|DELETE FROM) ${db}.${table}`, // activites | |
"g" | |
); | |
const input = fs.createReadStream(binlogFile); | |
const output = fs.createWriteStream(outputFile); | |
let currentEvent = ""; | |
let streak = false; | |
input.on("data", (data) => { | |
const lines = data.toString().split("\n"); | |
lines.forEach((line) => { | |
if (pattern.test(line)) { | |
currentEvent = line + "\n"; | |
streak = true; | |
} else if (streak && /^### (WHERE|\s*@|SET)/.test(line)) { | |
currentEvent += `${line}\n`; | |
} else { | |
output.write(currentEvent); | |
currentEvent = ""; | |
streak = false; | |
} | |
}); | |
}); | |
input.on("end", () => { | |
if (currentEvent !== "") output.write(currentEvent); | |
output.end(); | |
}); | |
input.on("error", (err) => | |
console.error(`Error reading from input file: ${err}`) | |
); | |
output.on("error", (err) => | |
console.error(`Error writing to output file: ${err}`) | |
); | |
output.on("finish", () => | |
console.log(`Finished writing to output file: ${outputFile}`) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment