Browse Source

(*)数据备份接口联调

master
peng.peng 2 years ago
parent
commit
6d8ca19039
  1. 1
      api/.vscode/launch.json
  2. 16
      api/app/lib/controllers/backups/index.js
  3. 2
      api/app/lib/models/backups.js
  4. 7
      api/config.js
  5. 25
      scripts/0.0.7/03_create_backups.sql
  6. 4
      web/client/assets/files/backups/1.sql
  7. 7
      web/client/src/sections/backups/containers/backupTask.js

1
api/.vscode/launch.json

@ -16,6 +16,7 @@
"-p 4400", "-p 4400",
// //
"-g postgres://FashionAdmin:123456@10.8.30.39:5432/GovernmentDataResourceCenter", "-g postgres://FashionAdmin:123456@10.8.30.39:5432/GovernmentDataResourceCenter",
"-b http://10.8.30.160:8085"
] ]
}, },
{ {

16
api/app/lib/controllers/backups/index.js

@ -1,4 +1,6 @@
'use strict'; 'use strict';
const request = require('superagent')
const moment = require('moment');
function getBackupsList(opts) { function getBackupsList(opts) {
return async function (ctx, next) { return async function (ctx, next) {
@ -44,11 +46,21 @@ function getBackupsList(opts) {
// 新增数据备份 // 新增数据备份
function addBackups(opts) { function addBackups(opts) {
return async function (ctx, next) { return async function (ctx, next) {
const { backupsUrl } = opts;
const models = ctx.fs.dc.models; const models = ctx.fs.dc.models;
try { try {
let rslt = ctx.request.body; 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.status = 204;
ctx.body = { message: '新建数据备份成功' } ctx.body = { message: '新建数据备份成功' }
} catch (error) { } catch (error) {

2
api/app/lib/models/backups.js

@ -35,7 +35,7 @@ module.exports = dc => {
autoIncrement: false autoIncrement: false
}, },
size: { size: {
type: DataTypes.INTEGER, type: DataTypes.STRING,
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
comment: null, comment: null,

7
api/config.js

@ -10,6 +10,7 @@ const dev = process.env.NODE_ENV == 'development';
// 启动参数 // 启动参数
args.option(['p', 'port'], '启动端口'); args.option(['p', 'port'], '启动端口');
args.option(['g', 'pg'], 'postgre 服务 URL'); args.option(['g', 'pg'], 'postgre 服务 URL');
args.option(['b', 'backups'], '后端数据库备份恢复接口地址');
const flags = args.parse(process.argv); 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_AK = process.env.ANXINCLOUD_QINIU_ACCESSKEY || flags.qnak;
const QINIU_SK = process.env.ANXINCLOUD_QINIU_SECRETKEY || flags.qnsk; 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('缺少启动参数,异常退出'); console.log('缺少启动参数,异常退出');
args.showHelp(); args.showHelp();
process.exit(-1); process.exit(-1);
@ -65,7 +67,8 @@ const product = {
password: 'Fs2689' password: 'Fs2689'
} }
}, },
pssaRequest: [] pssaRequest: [],
backupsUrl: BACKUPS_URL
} }
} }
], ],

25
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);

4
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';

7
web/client/src/sections/backups/containers/backupTask.js

@ -49,6 +49,11 @@ function Member(props) {
dataIndex: 'createTime', dataIndex: 'createTime',
render: (text, record) => { return moment(record?.createTime).format('YYYY-MM-DD HH:mm:ss') } 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: '状态', title: '状态',
dataIndex: 'state', dataIndex: 'state',
@ -74,7 +79,7 @@ function Member(props) {
> >
<a>恢复</a> <a>恢复</a>
</Popconfirm>) </Popconfirm>)
options.push(<a>下载</a>) options.push(<a onClick={() => { window.open('/assets/files/backups/1.sql') }}>下载</a>)
options.push( options.push(
<Popconfirm <Popconfirm

Loading…
Cancel
Save