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:
parent
b52f413aa5
commit
bdc4e29b2d
|
@ -1,17 +1,48 @@
|
||||||
import { backups } from "@budibase/pro"
|
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 { AppBackupQueueData } from "@budibase/types"
|
||||||
import { exportApp } from "./exports"
|
import { exportApp } from "./exports"
|
||||||
|
import { importApp } from "./imports"
|
||||||
import { Job } from "bull"
|
import { Job } from "bull"
|
||||||
import fs from "fs"
|
import fs from "fs"
|
||||||
import env from "../../../environment"
|
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) {
|
async function importProcessor(job: Job) {
|
||||||
const data: AppBackupQueueData = job.data
|
const data: AppBackupQueueData = job.data
|
||||||
const appId = data.appId,
|
const appId = data.appId,
|
||||||
backupId = data.import!.backupId
|
backupId = data.import!.backupId
|
||||||
const { path, metadata } = await backups.downloadAppBackup(backupId)
|
const tenantId = tenancy.getTenantIDFromAppID(appId)
|
||||||
// start by removing app database and contents of bucket - which will be updated
|
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) {
|
async function exportProcessor(job: Job) {
|
||||||
|
|
Loading…
Reference in New Issue