Created
August 23, 2013 08:27
-
-
Save ppillip/6316860 to your computer and use it in GitHub Desktop.
마리아 디비 백업 해보자
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
var exec = require('child_process').exec; | |
var util = require('util'); | |
var inspect = require('util').inspect; | |
var Client = require('mariasql'); | |
require('date-utils'); | |
var tables = []; //["KD_BRCH","KD_CUST","KD_HLDY","KD_ODAT","KD_TMPR","tblloader","tbltemperature"]; | |
var date = new Date(); | |
var folder = "/home/kdhec/ntels/backup/" + date.toFormat("YYYY.MM.DD.HHMISS") + "/"; | |
var mariaInfo = { | |
host: '192.168.1.128', | |
user: 'kdhec', | |
password: 'ntels2013!', | |
db: 'kdhec_db' | |
}; | |
var dumpOption = "--no-create-db --set-charset --default-character-set=utf8 --lock-tables=0"; | |
console.log(folder); | |
var c = new Client(); | |
c.connect(mariaInfo); | |
c.on('connect', function() { | |
console.log('Client connected'); | |
}) | |
.on('error', function(err) { | |
console.log('Client error: ' + err); | |
}) | |
.on('close', function(hadError) { | |
console.log('Client closed'); | |
}); | |
var pq = c.prepare('show tables'); | |
c.query(pq({})) | |
.on('result', function(res) { | |
res.on('row', function(row) { | |
tables.push(row.Tables_in_kdhec_db); | |
}) | |
.on('error', function(err) { | |
console.log('Result error: ' + inspect(err)); | |
}) | |
.on('end', function(info) { | |
console.log(tables); | |
exec("mkdir " + folder,function(){ | |
for (i = 0; i < tables.length; i++) { | |
(function(table){ | |
var dumpStr = "mysqldump -h " + mariaInfo.host | |
+ " -u" + mariaInfo.user | |
+ " -p" + mariaInfo.password | |
+ " " + dumpOption + " " + mariaInfo.db ; | |
dumpStr = dumpStr + table + "> " + folder + "/" + table + ".sql"; | |
console.log(table + " started"); | |
exec(dumpStr,function(error, stdout, stderr){ | |
//console.log('stdout: ' + stdout); | |
//console.log('stderr: ' + stderr); | |
if (error !== null) { | |
console.log('exec error: ' + error); | |
} | |
console.log(table + " 완료"); | |
}); | |
})(tables[i]); | |
} | |
}); | |
}); | |
}) | |
.on('end', function() { | |
console.log('Done with all results'); | |
}); | |
c.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment