系统设置增加数据恢复功能

This commit is contained in:
whyour
2023-07-16 22:02:30 +08:00
parent 88b87de391
commit 93e94ea94c
5 changed files with 169 additions and 16 deletions
+30 -1
View File
@@ -14,8 +14,18 @@ import {
promiseExec,
} from '../config/util';
import dayjs from 'dayjs';
import multer from 'multer';
const route = Router();
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, config.tmpPath);
},
filename: function (req, file, cb) {
cb(null, 'data.tgz');
},
});
const upload = multer({ storage: storage });
export default (app: Router) => {
app.use('/system', route);
@@ -118,11 +128,16 @@ export default (app: Router) => {
route.put(
'/reload',
celebrate({
body: Joi.object({
type: Joi.string().required(),
}),
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const systemService = Container.get(SystemService);
const result = await systemService.reloadSystem();
const result = await systemService.reloadSystem(req.body.type);
res.send(result);
} catch (e) {
return next(e);
@@ -221,4 +236,18 @@ export default (app: Router) => {
}
},
);
route.put(
'/data/import',
upload.single('data'),
async (req: Request, res: Response, next: NextFunction) => {
try {
const systemService = Container.get(SystemService);
const result = await systemService.importData();
res.send(result);
} catch (e) {
return next(e);
}
},
);
};
+14 -2
View File
@@ -20,11 +20,13 @@ import {
killTask,
parseContentVersion,
parseVersion,
promiseExec,
} from '../config/util';
import { TASK_COMMAND } from '../config/const';
import taskLimit from '../shared/pLimit';
import tar from 'tar';
import fs from 'fs';
import path from 'path';
@Service()
export default class SystemService {
@@ -182,8 +184,8 @@ export default class SystemService {
return { code: 200 };
}
public async reloadSystem() {
const cp = spawn('ql -l reload', { shell: '/bin/bash' });
public async reloadSystem(target: 'system' | 'data') {
const cp = spawn(`ql -l reload ${target || ''}`, { shell: '/bin/bash' });
cp.stdout.on('data', (data) => {
this.sockService.sendMessage({
@@ -266,4 +268,14 @@ export default class SystemService {
return res.send({ code: 400, message: error.message });
}
}
public async importData() {
try {
await promiseExec(`rm -rf ${path.join(config.tmpPath, 'data')}`);
await tar.x({ file: config.dataTgzFile, cwd: config.tmpPath });
return { code: 200 };
} catch (error: any) {
return { code: 400, message: error.message };
}
}
}