145 lines
3.9 KiB
JavaScript
145 lines
3.9 KiB
JavaScript
import fs from "fs";
|
|
import dayjs from "dayjs";
|
|
import _ from "lodash";
|
|
import CURDHelper from "./CURDHelper.mjs";
|
|
import DBHelper from "./DBHelper.mjs";
|
|
import child_process from "child_process";
|
|
import sleep from "./sleep.mjs";
|
|
import ConfigHelper from "./ConfigHelper.mjs";
|
|
|
|
/**
|
|
* 资源文件备份帮助类
|
|
*/
|
|
export default class BackupHelper {
|
|
/**
|
|
* 获取文件名称 fileName.ext
|
|
* @param {*} path
|
|
* @returns
|
|
*/
|
|
getFileName(path) {
|
|
const arr = path.split('\/');
|
|
if (arr.length >= 2) {
|
|
return arr.reverse()[0];
|
|
} else {
|
|
return path;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取文件后缀和文件名称
|
|
* @param {*} path
|
|
* @returns
|
|
*/
|
|
getFileExt(path) {
|
|
let arr = path.split('.');
|
|
if (arr.length >= 2) {
|
|
arr = arr.reverse();
|
|
return {
|
|
name: arr[1],
|
|
ext: "." + arr[0]
|
|
};
|
|
} else {
|
|
return {
|
|
name: path,
|
|
ext: ""
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建文件夹(指定文件夹路径不存在时才创建)
|
|
* @param {*} filePath
|
|
*/
|
|
mkDir(filePath) {
|
|
const isExists = fs.existsSync(filePath);
|
|
|
|
if (!isExists) {
|
|
fs.mkdirSync(filePath)
|
|
}
|
|
}
|
|
|
|
addBackUPRecord = async (data) => {
|
|
return CURDHelper.create({
|
|
db: await DBHelper.getBackupDb(),
|
|
name: 'backupHistory',
|
|
data
|
|
})
|
|
}
|
|
|
|
updateBackUPRecord = async (data) => {
|
|
return CURDHelper.update({
|
|
db: await DBHelper.getBackupDb(),
|
|
name: 'backupHistory',
|
|
data
|
|
})
|
|
}
|
|
|
|
// backupToBaiduYunPan = () => {
|
|
// let cmd = 'python';
|
|
// let args = ('F:\\sourceCode\\2021\\test-baiduPan\\python\\getInfo.py').split(' ');
|
|
// let task = child_process.spawnSync(cmd, args);
|
|
// console.log("用户信息:", task.stdout.toString());
|
|
// }
|
|
|
|
/**
|
|
* 根据配置备份文件
|
|
* @param {*} data:[{original:string,dest:string}]
|
|
*/
|
|
async bakFile(data) {
|
|
for (const item of (data || [])) {
|
|
const isExists = fs.existsSync(item.original);
|
|
if (isExists) {
|
|
this.mkDir(item.dest);
|
|
const fileName = this.getFileName(item.original);
|
|
const fileInfo = this.getFileExt(fileName);
|
|
const timeSuffix = dayjs().format('YYYYMMDDHHmmss');
|
|
const fullFileName = fileInfo.name + timeSuffix + fileInfo.ext;
|
|
// 新增备份记录
|
|
const {
|
|
data: newRecord,
|
|
code
|
|
} = await this.addBackUPRecord({
|
|
name: fullFileName,
|
|
category: item.category
|
|
});
|
|
if (code == 0) {
|
|
// 备份文件到本地其它路径
|
|
fs.writeFileSync(item.dest + "\/" + fullFileName, fs.readFileSync(item.original));
|
|
|
|
// 更新备份记录状态
|
|
await this.updateBackUPRecord({
|
|
id: newRecord.id,
|
|
status: "1"
|
|
});
|
|
|
|
// await sleep(3 * 1000);
|
|
// this.backupToBaiduYunPan();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
const startWebServer = () => {
|
|
let cmd = 'pm2';
|
|
let args = ('start computer-cms-server').split(' ');
|
|
let task = child_process.spawnSync(cmd, args);
|
|
console.log("pm2 log:", task);
|
|
}
|
|
|
|
const stopWebServer = () => {
|
|
let cmd = 'pm2';
|
|
let args = ('stop computer-cms-server').split(' ');
|
|
let task = child_process.spawnSync(cmd, args);
|
|
console.log("pm2 log:", task);
|
|
}
|
|
|
|
const bH = new BackupHelper();
|
|
(async () => {
|
|
ConfigHelper.initConfig();
|
|
await DBHelper.init();
|
|
|
|
stopWebServer();
|
|
await bH.bakFile(ConfigHelper.config.dbBak);
|
|
startWebServer();
|
|
})() |