From 6d8ca19039b5e387fe88c3ef3e86fdc9428d3337 Mon Sep 17 00:00:00 2001 From: "peng.peng" Date: Mon, 19 Jun 2023 14:36:35 +0800 Subject: [PATCH] =?UTF-8?q?(*)=E6=95=B0=E6=8D=AE=E5=A4=87=E4=BB=BD?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=81=94=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/.vscode/launch.json | 1 + api/app/lib/controllers/backups/index.js | 16 ++++++++++-- api/app/lib/models/backups.js | 2 +- api/config.js | 7 ++++-- scripts/0.0.7/03_create_backups.sql | 25 +++++++++++++++++++ web/client/assets/files/backups/1.sql | 4 +++ .../sections/backups/containers/backupTask.js | 7 +++++- 7 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 scripts/0.0.7/03_create_backups.sql create mode 100644 web/client/assets/files/backups/1.sql diff --git a/api/.vscode/launch.json b/api/.vscode/launch.json index c33af0c..2217519 100644 --- a/api/.vscode/launch.json +++ b/api/.vscode/launch.json @@ -16,6 +16,7 @@ "-p 4400", // 研发 "-g postgres://FashionAdmin:123456@10.8.30.39:5432/GovernmentDataResourceCenter", + "-b http://10.8.30.160:8085" ] }, { diff --git a/api/app/lib/controllers/backups/index.js b/api/app/lib/controllers/backups/index.js index 7cb1c4b..f148301 100644 --- a/api/app/lib/controllers/backups/index.js +++ b/api/app/lib/controllers/backups/index.js @@ -1,4 +1,6 @@ 'use strict'; +const request = require('superagent') +const moment = require('moment'); function getBackupsList(opts) { return async function (ctx, next) { @@ -44,11 +46,21 @@ function getBackupsList(opts) { // 新增数据备份 function addBackups(opts) { return async function (ctx, next) { - + const { backupsUrl } = opts; const models = ctx.fs.dc.models; try { let rslt = ctx.request.body; - await models.Backups.create(Object.assign({}, rslt)) + 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) { diff --git a/api/app/lib/models/backups.js b/api/app/lib/models/backups.js index bd7b52a..0100b2b 100644 --- a/api/app/lib/models/backups.js +++ b/api/app/lib/models/backups.js @@ -35,7 +35,7 @@ module.exports = dc => { autoIncrement: false }, size: { - type: DataTypes.INTEGER, + type: DataTypes.STRING, allowNull: true, defaultValue: null, comment: null, diff --git a/api/config.js b/api/config.js index 668fadd..9e14778 100644 --- a/api/config.js +++ b/api/config.js @@ -10,6 +10,7 @@ const dev = process.env.NODE_ENV == 'development'; // 启动参数 args.option(['p', 'port'], '启动端口'); args.option(['g', 'pg'], 'postgre 服务 URL'); +args.option(['b', 'backups'], '后端数据库备份恢复接口地址'); const flags = args.parse(process.argv); @@ -21,7 +22,8 @@ const QINIU_BUCKET_RESOURCE = process.env.ANXINCLOUD_QINIU_BUCKET_RESOURCE || fl const QINIU_AK = process.env.ANXINCLOUD_QINIU_ACCESSKEY || flags.qnak; const QINIU_SK = process.env.ANXINCLOUD_QINIU_SECRETKEY || flags.qnsk; -if (!DB) { +const BACKUPS_URL = process.env.BACKUPS_URL || flags.backups; +if (!DB || !BACKUPS_URL) { console.log('缺少启动参数,异常退出'); args.showHelp(); process.exit(-1); @@ -65,7 +67,8 @@ const product = { password: 'Fs2689' } }, - pssaRequest: [] + pssaRequest: [], + backupsUrl: BACKUPS_URL } } ], diff --git a/scripts/0.0.7/03_create_backups.sql b/scripts/0.0.7/03_create_backups.sql new file mode 100644 index 0000000..a0d23e2 --- /dev/null +++ b/scripts/0.0.7/03_create_backups.sql @@ -0,0 +1,25 @@ +create table backups +( + id serial + constraint backups_pk + primary key, + note varchar(255) not null, + databases jsonb, + size varchar(255), + create_time timestamp with time zone, + complete_time timestamp with time zone, + state enum_task_state, + source text +); + +comment on table backups is '备份任务表'; + +comment on column backups.note is '备注信息'; + +comment on column backups.complete_time is '备份完成时间'; + +comment on column backups.source is '备份文件路径'; + +create unique index backups_id_uindex + on backups (id); + diff --git a/web/client/assets/files/backups/1.sql b/web/client/assets/files/backups/1.sql new file mode 100644 index 0000000..9d22149 --- /dev/null +++ b/web/client/assets/files/backups/1.sql @@ -0,0 +1,4 @@ +alter table t_resource_consumption + add resource_id int; + +comment on column t_resource_consumption.resource_id is '元数据id'; \ No newline at end of file diff --git a/web/client/src/sections/backups/containers/backupTask.js b/web/client/src/sections/backups/containers/backupTask.js index 8243bc1..93a1392 100644 --- a/web/client/src/sections/backups/containers/backupTask.js +++ b/web/client/src/sections/backups/containers/backupTask.js @@ -49,6 +49,11 @@ function Member(props) { dataIndex: 'createTime', render: (text, record) => { return moment(record?.createTime).format('YYYY-MM-DD HH:mm:ss') } }, + { + title: '备份完成时间', + dataIndex: 'completeTime', + render: (text, record) => { return record?.completeTime ? moment(record?.completeTime).format('YYYY-MM-DD HH:mm:ss') : '-' } + }, { title: '状态', dataIndex: 'state', @@ -74,7 +79,7 @@ function Member(props) { > 恢复 ) - options.push(下载) + options.push( { window.open('/assets/files/backups/1.sql') }}>下载) options.push(