'use strict'; const request = require('superagent') const moment = require('moment'); function getBackupsList(opts) { return async function (ctx, next) { const models = ctx.fs.dc.models; const { page, limit, name } = ctx.query; const Op = ctx.fs.dc.ORM.Op; let errMsg = { message: '获取数据备份失败' } try { let searchWhere = { } let option = { where: searchWhere, order: [["id", "desc"]], attributes: { exclude: ['password'] }, } if (name) { searchWhere.note = { $like: '%' + name + '%' }; } option.where = searchWhere let limit_ = limit || 10; let page_ = page || 1; let offset = (page_ - 1) * limit_; if (limit && page) { option.limit = limit_ option.offset = offset } const res = await models.Backups.findAndCount(option); ctx.status = 200; ctx.body = res; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = errMsg } } } // 新增数据备份 function addBackups(opts) { return async function (ctx, next) { const { backupsUrl } = opts; const models = ctx.fs.dc.models; try { let rslt = ctx.request.body; const { database, host, password, port, user } = ctx.request.body.databases let backup = await models.Backups.create(Object.assign({}, rslt)) //调用后端备份接口 // const url = '10.8.30.160:8085/dumpDB?dbHost=10.8.30.75&dbPort=5432&user=postgres&password=1234&dbName=Anxinyun0916'//测试使用 const url = backupsUrl + `/dumpDB?dbHost=${host}&dbPort=${port}&user=${user}&password=${password}&dbName=${database}`; const res = await request.post(url) const { fileInfo: { name, size }, message } = res.body await models.Backups.update({ size, source: name, state: message, completeTime: moment() }, { where: { id: backup.id } }) ctx.status = 204; ctx.body = { message: '新建数据备份成功' } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '新建数据备份失败' } } } } // 修改数据备份 function editBackups(opts) { return async function (ctx, next) { try { const models = ctx.fs.dc.models; const { id } = ctx.params; const body = ctx.request.body; await models.Backups.update( body, { where: { id: id, } } ) ctx.status = 204; ctx.body = { message: '修改数据备份成功' } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '修改数据备份失败' } } } } // 删除数据备份 function deleteBackups(opts) { return async function (ctx, next) { try { const models = ctx.fs.dc.models; const { id } = ctx.params; await models.Backups.destroy({ where: { id: id } }) ctx.status = 204; ctx.body = { message: '删除数据备份成功' } } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: '删除数据备份失败' } } } } module.exports = { getBackupsList, addBackups, editBackups, deleteBackups, }