-
-
Save vmdao/651668cebbdcfc37c724223b6d75bd73 to your computer and use it in GitHub Desktop.
Redis to MySQL dump
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
// modules | |
var redis = require('redis'); | |
var Step = require('step'); | |
// config file | |
var config = require('config'); | |
// mysql client connect | |
var mysql = require('mysql'); | |
var connection = mysql.createConnection({ | |
host : config.mysql.host, | |
port : config.mysql.port, | |
database : config.mysql.db, | |
user : config.mysql.user, | |
password : config.mysql.pass, | |
insecureAuth : true, | |
multipleStatements: true | |
}); | |
function handleDisconnect() { | |
connection.on('error', function(err) { | |
if (!err.fatal) { | |
return; | |
} | |
if (err.code !== 'PROTOCOL_CONNECTION_LOST') { | |
throw err; | |
} | |
connection = mysql.createConnection(connection.config); | |
handleDisconnect(); | |
connection.connect(); | |
}); | |
} | |
handleDisconnect(); | |
// redis client connect | |
client = redis.createClient(config.redis.port, config.redis.host); | |
client.on("error", function (err) { | |
console.log("Error " + err); | |
}); | |
var timeOut = 10; | |
function dumpMysql() { | |
Step( | |
function checkWorkingVars() { | |
client.multi() | |
.exists('data_tmp') | |
.exists('data') | |
.exec(this); | |
}, | |
function setWorkingVars(err, replies) { | |
existsTmp = replies[0]; | |
existsData = replies[1]; | |
if(!existsData && !existsTmp) { | |
setTimeout(dumpMysql, timeOut*1000); | |
} else { | |
if(existsData && !existsTmp) { | |
client.rename('data', 'data_tmp'); | |
} | |
client.smembers('data_tmp', this); | |
} | |
}, | |
function readData(err, data) { | |
for(var attr in data) { | |
connection.query('SELECT 1'); // db ping | |
var query = 'INSERT IGNORE INTO data (log) '; | |
query += 'VALUES ("'+data[attr]+'")'; | |
connection.query(query); | |
} | |
}, | |
function cleanUp(err, r) { | |
client.del('data_tmp', this); | |
}, | |
function restart(err, r) { | |
setTimeout(dumpMysql, timeOut*1000); | |
} | |
); | |
}; | |
setTimeout(dumpMysql, 1000); | |
// stdout log started | |
console.log('DUMP Mysql running...'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment