first commit

main
expressgy 1 year ago
commit 8532b02da4
  1. 37
      .gitignore
  2. 8
      config.js
  3. 19
      package.json
  4. 165
      server.js
  5. 33
      test.js

37
.gitignore vendored

@ -0,0 +1,37 @@
# compiled output
/dist
/node_modules
/Log/*
# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
pnpm-lock.yaml
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

@ -0,0 +1,8 @@
const SERVER_PORT = 5000;
// 数据库名称
const DATABASELIST = ['procatch-cloud', 'procatch-config', 'procatch-flowable'];
export default {
SERVER_PORT,
DATABASELIST
}

@ -0,0 +1,19 @@
{
"name": "yananjiasheng",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"iconv-lite": "^0.6.3",
"koa": "^2.14.2",
"koa-body": "^6.0.1",
"koa-router": "^12.0.0",
"moment": "^2.29.4"
}
}

@ -0,0 +1,165 @@
const {koaBody} = require('koa-body');
const Koa = require('koa');
const fs = require('fs');
const path = require('path');
const { spawn } = require('child_process')
const router = require('koa-router')()
, os = require('os')
, iconv = require("iconv-lite")
const moment = require('moment')
const { DATABASELIST, SERVER_PORT} = require('./config')
// 备份间隔时间
const mintues = 60 * 24;// minute
// web服务
function server(){
const app = new Koa();
app.use(koaBody({
multipart: true, // 支持文件上传
formidable: {
maxFieldsSize: 10 * 1024 * 1024, // 最大文件为2兆
multipart: true // 是否支持 multipart-formdate 的表单
}
}));
router.post('/putfile', async ctx => {
const fileList = await getFileList()
console.log('请求备份', fileList)
if(fileList.length == 0){
ctx.body = [{state:false,filename:'无备份文件'}]
}else{
const bunneryList = await readFile(fileList)
ctx.body = bunneryList
deleteFile(fileList)
}
});
app.use(router.routes(), router.allowedMethods())
app.listen(SERVER_PORT, () => {
console.log('=> 备份服务已启动! http://127.0.0.1:' + SERVER_PORT)
});
}
// 本分服务
function back(){
/**
* 数据库备份程序
* */
function backSQL(host, 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`
const backProcess = spawn('mysqldump',['-u'+user, '-p'+passwd, 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)
})
})
}
// 开始备份数据库
function startBackSQL(){
// 数据库信息
let host = "localhost"
, user = "root"
, passwd = "root"
, database = "develop"
backSQL(host, user, passwd, database)
const outTime = getT() - new Date().getTime()
DATABASELIST.forEach(i => {
database = i;
setTimeout(() => {
setInterval(() => {
backSQL(host, user, passwd, database)
}, 1000 * 60 * mintues)
},outTime)
})
}
/**
* 根据系统类型读取编码格式
* */
function out(data){
if(os.platform() == 'win32'){
return iconv.decode(data, 'GBK')
}else{
return data
}
}
// 读取文件目录
function getFileList(){
return new Promise((rec, res) => {
fs.readdir(__dirname + '/back', (err, fileList) => {
if(err){
res(err)
}else{
rec(fileList)
}
})
})
}
// 读取文件
function readFile(fileList){
return new Promise(rec => {
const list = []
for(let i in fileList){
fs.readFile(__dirname + '/back/' + fileList[i],'utf8',function(err,datastr){
if(err){
list.push({
state:false,
filename:fileList[i]
})
}else{
list.push({
state:true,
filename:fileList[i],
file:datastr
})
}
if(i == fileList.length -1){
rec(list)
}
})
}
})
}
// 删除文件
function deleteFile(filelist){
for(let filename of filelist){
fs.unlink(__dirname + '/back/' + filename, (e, data) => {
if(e){
console.log('删除文件失败:', filename)
}else{
console.log('删除文件成功:', filename)
}
})
}
}
// 获取明天凌晨时间
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()
}
// 启动本地备份
startBackSQL()
}
server()
back()

@ -0,0 +1,33 @@
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()))
Loading…
Cancel
Save