Finishing import processor - download backup, delete dev DB and then import over the top of this. Also includes a rollback feature if the backup fails to restore for whatever reason.

This commit is contained in:
mike12345567 2022-10-18 19:43:19 +01:00
parent ed35a8281f
commit 0cf5bf26a0
1 changed files with 34 additions and 3 deletions

View File

@ -1,17 +1,48 @@
import { backups } from "@budibase/pro"
import { objectStore, tenancy } from "@budibase/backend-core"
import { objectStore, tenancy, db as dbCore } from "@budibase/backend-core"
import { AppBackupQueueData } from "@budibase/types"
import { exportApp } from "./exports"
import { importApp } from "./imports"
import { Job } from "bull"
import fs from "fs"
import env from "../../../environment"
async function removeExistingApp(devId: string) {
const devDb = dbCore.dangerousGetDB(devId, { skip_setup: true })
await devDb.destroy()
}
async function importProcessor(job: Job) {
const data: AppBackupQueueData = job.data
const appId = data.appId,
backupId = data.import!.backupId
const { path, metadata } = await backups.downloadAppBackup(backupId)
// start by removing app database and contents of bucket - which will be updated
const tenantId = tenancy.getTenantIDFromAppID(appId)
tenancy.doInTenant(tenantId, async () => {
const devAppId = dbCore.getDevAppID(appId)
const performImport = async (path: string) => {
await importApp(devAppId, dbCore.dangerousGetDB(devAppId), {
file: {
type: "application/gzip",
path,
},
key: path,
})
}
// initially export the current state to disk - incase something goes wrong
const backupTarPath = await exportApp(devAppId, { tar: true })
// get the backup ready on disk
const { path } = await backups.downloadAppBackup(backupId)
// start by removing app database and contents of bucket - which will be updated
await removeExistingApp(devAppId)
try {
await performImport(path)
} catch (err) {
// rollback - clear up failed import and re-import the pre-backup
await removeExistingApp(devAppId)
await performImport(backupTarPath)
}
fs.rmSync(backupTarPath)
})
}
async function exportProcessor(job: Job) {