diff --git a/mysqldump b/mysqldump deleted file mode 100644 index 66cf5e8..0000000 Binary files a/mysqldump and /dev/null differ diff --git a/mysqldump.exe b/mysqldump.exe deleted file mode 100644 index 8c525af..0000000 Binary files a/mysqldump.exe and /dev/null differ diff --git a/package.json b/package.json index 4f47593..b7f5266 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "koa": "^2.14.2", "koa-body": "^6.0.1", "koa-router": "^12.0.0", - "moment": "^2.29.4" + "moment": "^2.29.4", + "mysqldump": "^3.2.0" } } diff --git a/server.js b/server.js index 673647b..a8d5c1e 100644 --- a/server.js +++ b/server.js @@ -9,6 +9,7 @@ const router = require('koa-router')() const moment = require('moment') const { DATABASELIST, SERVER_PORT, SERVERHOST} = require('./config') +const mysqldump = require("mysqldump"); // 备份间隔时间 const mintues = 60 * 24;// minute @@ -48,18 +49,16 @@ function back(){ * 数据库备份程序 * */ function backSQL(user, passwd, database){ - return new Promise(rec => { - const cout = os.platform() == 'win32' ? '-r' : '>' - , filename = `back/BACKUP_${ database }_${ moment().format("YYYY_MM_DD_HH_mm_ss") }.sql` - console.log(cout, filename) - const backProcess = spawn('mysqldump',['-h' + SERVERHOST, database, cout, filename]); - backProcess.stdout.on('data', data => console.log('Data : ', out(data))) - backProcess.stderr.on('data', data => console.log('Error: ', out(data))) - backProcess.on('close', code => { - console.log('Exit :', code.toString()) - rec(filename) - }) - }) + const filename = `./back/${ database }_${ moment().format("YYYY_MM_DD_HH_mm_ss") }.sql` + mysqldump({ + connection: { + host: SERVERHOST, + user: user, + password: passwd, + database: database, + }, + dumpToFile: filename, + }); } // 开始备份数据库 @@ -160,5 +159,5 @@ function back(){ startBackSQL() } -server() +// server() back() \ No newline at end of file diff --git a/test.js b/test.js index 62d0f6e..e69de29 100644 --- a/test.js +++ b/test.js @@ -1,33 +0,0 @@ - -function getT(){ - const temore = new Date().getTime() + 1000 * 60 * 60 * 24 - const now = new Date(temore) - let year = now.getFullYear(); //得到年份 - let month = now.getMonth();//得到月份 - let date = now.getDate();//得到日期 - return new Date(year + "-" + month + "-" + date).getTime() -} -function transformT(t){ - let now = new Date(t); - let year = now.getFullYear(); //得到年份 - let month = now.getMonth();//得到月份 - let date = now.getDate();//得到日期 - let day = now.getDay();//得到周几 - let hour = now.getHours();//得到小时 - let minu = now.getMinutes();//得到分钟 - let sec = now.getSeconds();//得到秒 - let MS = now.getMilliseconds();//获取毫秒 - let week; - month = month + 1; - if (month < 10) month = "0" + month; - if (date < 10) date = "0" + date; - if (hour < 10) hour = "0" + hour; - if (minu < 10) minu = "0" + minu; - if (sec < 10) sec = "0" + sec; - if (MS < 100)MS = "0" + MS; - let time = ""; - time = year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec; - return time -} - -console.log(transformT(getT())) \ No newline at end of file