-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@datatruck/cli": minor | ||
--- | ||
|
||
Add `mongo-dump` task (only backup) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { AsyncProcess } from "../utils/async-process"; | ||
import { ensureEmptyDir, fetchData, mkdirIfNotExists } from "../utils/fs"; | ||
import { mkTmpDir } from "../utils/temp"; | ||
import { TaskBackupData, TaskRestoreData, TaskAbstract } from "./TaskAbstract"; | ||
|
||
export type MongoDumpTaskConfig = { | ||
command?: string; | ||
hostname?: string; | ||
port?: number; | ||
username?: string; | ||
password?: string | { path: string }; | ||
compress?: boolean; | ||
concurrency?: number; | ||
}; | ||
|
||
export const mongodumpTaskName = "mongo-dump"; | ||
|
||
export class MongoDumpTask extends TaskAbstract<MongoDumpTaskConfig> { | ||
protected verbose?: boolean; | ||
private get command() { | ||
return this.config.command ?? "mongodump"; | ||
} | ||
|
||
override async backup(data: TaskBackupData) { | ||
this.verbose = data.options.verbose; | ||
|
||
const snapshotPath = | ||
data.package.path ?? | ||
(await mkTmpDir(mongodumpTaskName, "task", "backup", "snapshot")); | ||
|
||
await mkdirIfNotExists(snapshotPath); | ||
await ensureEmptyDir(snapshotPath); | ||
|
||
const p = new AsyncProcess( | ||
this.command, | ||
[ | ||
...(this.config.hostname ? ["/h", this.config.hostname] : []), | ||
...(this.config.port ? ["/p", this.config.port] : []), | ||
...(this.config.username ? ["/u", this.config.username] : []), | ||
...(this.config.compress ? ["/gzip"] : []), | ||
...(this.config.concurrency ? ["/j", this.config.concurrency] : []), | ||
"/o", | ||
snapshotPath, | ||
], | ||
{ | ||
$log: this.verbose, | ||
}, | ||
); | ||
|
||
const password = | ||
this.config.password !== undefined | ||
? (await fetchData(this.config.password, (p) => p.path)) ?? "" | ||
: ""; | ||
|
||
p.stdin.writable.write(`${password}\n`); | ||
|
||
await p.stderr.parseLines((line) => { | ||
data.onProgress({ | ||
absolute: { | ||
description: line.slice(0, 255), | ||
}, | ||
}); | ||
}); | ||
|
||
return { snapshotPath }; | ||
} | ||
|
||
override async restore(data: TaskRestoreData) { | ||
throw new Error("Not implemented"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters