Skip to content

Commit

Permalink
feat(cli): add mongo-dump task
Browse files Browse the repository at this point in the history
  • Loading branch information
juanrgm committed Jan 25, 2024
1 parent 0ab9b90 commit 0577a06
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-pots-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@datatruck/cli": minor
---

Add `mongo-dump` task (only backup)
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ RUN apk add --no-cache \
mariadb-client \
mariadb-backup \
postgresql-client \
mongodb-tools \
python3 \
tar \
pigz
Expand Down
71 changes: 71 additions & 0 deletions packages/cli/src/tasks/MongoDumpTask.ts
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");
}
}
10 changes: 10 additions & 0 deletions packages/cli/src/utils/datatruck/config-task-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import type {
MariadbTaskConfig,
mariadbTaskName,
} from "../../tasks/MariadbTask";
import {
MongoDumpTaskConfig,
mongodumpTaskName,
} from "../../tasks/MongoDumpTask";
import type { MssqlTaskConfig, mssqlTaskName } from "../../tasks/MssqlTask";
import type {
MysqlDumpTaskConfig,
Expand Down Expand Up @@ -39,6 +43,11 @@ export type PostgresqlDumpTaskConfigItem = {
config: PostgresqlDumpTaskConfig;
};

export type MongodumpTaskConfigItem = {
name: typeof mongodumpTaskName;
config: MongoDumpTaskConfig;
};

export type ScriptTaskConfigItem = {
name: typeof scriptTaskName;
config: ScriptTaskConfig;
Expand All @@ -50,4 +59,5 @@ export type TaskConfig =
| MssqlTaskConfigItem
| MysqlDumpTaskConfigItem
| PostgresqlDumpTaskConfigItem
| MongodumpTaskConfigItem
| ScriptTaskConfigItem;
3 changes: 3 additions & 0 deletions packages/cli/src/utils/datatruck/task.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GitTask, gitTaskName } from "../../tasks/GitTask";
import { MariadbTask, mariadbTaskName } from "../../tasks/MariadbTask";
import { MongoDumpTask, mongodumpTaskName } from "../../tasks/MongoDumpTask";
import { MssqlTask, mssqlTaskName } from "../../tasks/MssqlTask";
import { MysqlDumpTask, mysqlDumpTaskName } from "../../tasks/MysqlDumpTask";
import {
Expand All @@ -22,6 +23,8 @@ export function createTask(task: TaskConfig): TaskAbstract {
return new PostgresqlDumpTask(task.config ?? {});
} else if (task.name === mssqlTaskName) {
return new MssqlTask(task.config ?? {});
} else if (task.name === mongodumpTaskName) {
return new MongoDumpTask(task.config ?? {});
} else if (task.name === scriptTaskName) {
return new ScriptTask(task.config ?? {});
} else {
Expand Down

0 comments on commit 0577a06

Please sign in to comment.