mirror of
https://github.com/hcengineering/platform.git
synced 2025-05-28 19:08:01 +00:00
Fix Account/Backup (#2642)
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
parent
a17d24c965
commit
54e62d9547
@ -283,10 +283,17 @@ export function devTool (
|
||||
|
||||
program
|
||||
.command('backup-restore <dirName> <workspace> [date]')
|
||||
.option('-m, --merge', 'Enable merge of remote and backup content.', false)
|
||||
.description('dump workspace transactions and minio resources')
|
||||
.action(async (dirName: string, workspace: string, date, cmd) => {
|
||||
.action(async (dirName: string, workspace: string, date, cmd: { merge: boolean }) => {
|
||||
const storage = await createFileBackupStorage(dirName)
|
||||
return await restore(transactorUrl, getWorkspaceId(workspace, productId), storage, parseInt(date ?? '-1'))
|
||||
return await restore(
|
||||
transactorUrl,
|
||||
getWorkspaceId(workspace, productId),
|
||||
storage,
|
||||
parseInt(date ?? '-1'),
|
||||
cmd.merge
|
||||
)
|
||||
})
|
||||
|
||||
program
|
||||
|
@ -25,8 +25,6 @@ interface Config {
|
||||
MinioEndpoint: string
|
||||
MinioAccessKey: string
|
||||
MinioSecretKey: string
|
||||
|
||||
ProductId: string
|
||||
}
|
||||
|
||||
const envMap: { [key in keyof Config]: string } = {
|
||||
@ -38,8 +36,7 @@ const envMap: { [key in keyof Config]: string } = {
|
||||
Interval: 'INTERVAL',
|
||||
MinioEndpoint: 'MINIO_ENDPOINT',
|
||||
MinioAccessKey: 'MINIO_ACCESS_KEY',
|
||||
MinioSecretKey: 'MINIO_SECRET_KEY',
|
||||
ProductId: 'PRODUCT_ID'
|
||||
MinioSecretKey: 'MINIO_SECRET_KEY'
|
||||
}
|
||||
|
||||
const required: Array<keyof Config> = [
|
||||
@ -63,8 +60,7 @@ const config: Config = (() => {
|
||||
Interval: parseInt(process.env[envMap.Interval] ?? '3600'),
|
||||
MinioEndpoint: process.env[envMap.MinioEndpoint],
|
||||
MinioAccessKey: process.env[envMap.MinioAccessKey],
|
||||
MinioSecretKey: process.env[envMap.MinioSecretKey],
|
||||
ProductId: process.env[envMap.ProductId] ?? ''
|
||||
MinioSecretKey: process.env[envMap.MinioSecretKey]
|
||||
}
|
||||
|
||||
const missingEnv = required.filter((key) => params[key] === undefined).map((key) => envMap[key])
|
||||
|
@ -19,9 +19,21 @@ import { setMetadata } from '@hcengineering/platform'
|
||||
import { backup, createMinioBackupStorage } from '@hcengineering/server-backup'
|
||||
import serverToken from '@hcengineering/server-token'
|
||||
import got from 'got'
|
||||
import { ObjectId } from 'mongodb'
|
||||
import config from './config'
|
||||
|
||||
async function getWorkspaces (): Promise<string[]> {
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export interface Workspace {
|
||||
_id: ObjectId
|
||||
workspace: string
|
||||
organisation: string
|
||||
accounts: ObjectId[]
|
||||
productId: string
|
||||
}
|
||||
|
||||
async function getWorkspaces (): Promise<Workspace[]> {
|
||||
const { body }: { body: { error?: string, result?: any[] } } = await got.post(config.AccountsURL, {
|
||||
json: {
|
||||
method: 'listWorkspaces',
|
||||
@ -34,7 +46,7 @@ async function getWorkspaces (): Promise<string[]> {
|
||||
throw Error(body.error)
|
||||
}
|
||||
|
||||
return (body.result ?? []).map((x) => x.workspace)
|
||||
return (body.result as Workspace[]) ?? []
|
||||
}
|
||||
|
||||
export class PlatformWorker {
|
||||
@ -79,8 +91,12 @@ export class PlatformWorker {
|
||||
for (const ws of workspaces) {
|
||||
console.log('\n\nBACKUP WORKSPACE ', ws)
|
||||
try {
|
||||
const storage = await createMinioBackupStorage(this.minio, getWorkspaceId('backups', config.ProductId), ws)
|
||||
await backup(config.TransactorURL, getWorkspaceId(ws, config.ProductId), storage)
|
||||
const storage = await createMinioBackupStorage(
|
||||
this.minio,
|
||||
getWorkspaceId('backups', ws.productId),
|
||||
ws.workspace
|
||||
)
|
||||
await backup(config.TransactorURL, getWorkspaceId(ws.workspace, ws.productId), storage)
|
||||
} catch (err: any) {
|
||||
console.error('\n\nFAILED to BACKUP', ws, err)
|
||||
}
|
||||
|
@ -377,7 +377,9 @@ export async function createAccount (
|
||||
* @public
|
||||
*/
|
||||
export async function listWorkspaces (db: Db, productId: string): Promise<Workspace[]> {
|
||||
return await db.collection<Workspace>(WORKSPACE_COLLECTION).find(withProductId(productId, {})).toArray()
|
||||
return (await db.collection<Workspace>(WORKSPACE_COLLECTION).find(withProductId(productId, {})).toArray()).map(
|
||||
(it) => ({ ...it, productId })
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -326,7 +326,8 @@ export async function restore (
|
||||
transactorUrl: string,
|
||||
workspaceId: WorkspaceId,
|
||||
storage: BackupStorage,
|
||||
date: number
|
||||
date: number,
|
||||
merge?: boolean
|
||||
): Promise<void> {
|
||||
const infoFile = 'backup.json.gz'
|
||||
|
||||
@ -357,6 +358,7 @@ export async function restore (
|
||||
mode: 'backup',
|
||||
model: 'upgrade'
|
||||
})) as unknown as CoreClient & BackupClient
|
||||
|
||||
try {
|
||||
for (const c of domains) {
|
||||
console.log('loading server changeset for', c)
|
||||
@ -514,7 +516,7 @@ export async function restore (
|
||||
}
|
||||
|
||||
await sendChunk(undefined, 0)
|
||||
if (docsToRemove.length > 0) {
|
||||
if (docsToRemove.length > 0 && merge !== true) {
|
||||
console.log('cleanup', docsToRemove.length)
|
||||
await connection.clean(c, docsToRemove)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user