@datatruck/cli 0.0.6 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,6 +26,7 @@ class BackupAction {
26
26
  });
27
27
  packages = (0, config_util_1.resolvePackages)(packages, {
28
28
  snapshotId: snapshot.id,
29
+ snapshotDate: snapshot.date,
29
30
  action: "backup",
30
31
  });
31
32
  for (const pkg of packages) {
@@ -206,6 +206,7 @@ class RestoreAction {
206
206
  let packages = (0, config_util_1.filterPackages)(this.config, this.options);
207
207
  packages = (0, config_util_1.resolvePackages)(packages, {
208
208
  snapshotId: this.options.snapshotId,
209
+ snapshotDate: snapshots[0].date,
209
210
  action: "restore",
210
211
  });
211
212
  const snapshotAndConfigs = this.assocConfigs(packages, snapshots);
@@ -19,6 +19,7 @@ export declare type SnapshotsActionOptionsType = {
19
19
  groupBy?: SnapshotGroupByType[];
20
20
  };
21
21
  export declare type SnapshotExtendedType = {
22
+ shortId: string;
22
23
  repositoryName: string;
23
24
  repositoryType: string;
24
25
  } & SnapshotResultType;
@@ -23,6 +23,7 @@ class SnapshotsAction {
23
23
  });
24
24
  const extentedItems = snapshots.map((item) => ({
25
25
  ...item,
26
+ shortId: item.id.slice(0, 8),
26
27
  repositoryName: repo.name,
27
28
  repositoryType: repo.type,
28
29
  }));
package/CHANGELOG.md CHANGED
@@ -1,5 +1,41 @@
1
1
  # @datatruck/cli
2
2
 
3
+ ## 0.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`d63fd25`](https://github.com/swordev/datatruck/commit/d63fd25ffa8d2e539d2125dfd6a3f55020086804) Thanks [@juanrgm](https://github.com/juanrgm)! - Add `snapshotDate` param
8
+
9
+ * [`486ef4a`](https://github.com/swordev/datatruck/commit/486ef4add27ae1dbfd166b16c257522f43537ecd) Thanks [@juanrgm](https://github.com/juanrgm)! - Resolve params in `include` and `exclude`
10
+
11
+ - [`617dae2`](https://github.com/swordev/datatruck/commit/617dae2c8ed90e6e65e8109f03cfad0e64bd7c02) Thanks [@juanrgm](https://github.com/juanrgm)! - Add `script` task
12
+
13
+ ### Patch Changes
14
+
15
+ - [`d1b3ea9`](https://github.com/swordev/datatruck/commit/d1b3ea9c9540d30898c00490963523a4fbc68193) Thanks [@juanrgm](https://github.com/juanrgm)! - Avoid use gitignore if is not necessary in restic repository
16
+
17
+ ## 0.2.0
18
+
19
+ ### Minor Changes
20
+
21
+ - [`120460c`](https://github.com/swordev/datatruck/commit/120460c8824cef4184e43f571a4cc0798b899b66) Thanks [@juanrgm](https://github.com/juanrgm)! - Enable `include` option in restic repository
22
+
23
+ ### Patch Changes
24
+
25
+ - [`e30ede3`](https://github.com/swordev/datatruck/commit/e30ede371bc7ab3fc1cd47758fdac7a28e8e2705) Thanks [@juanrgm](https://github.com/juanrgm)! - Resolve `RESTIC_PASSWORD_FILE` path
26
+
27
+ * [`8539d28`](https://github.com/swordev/datatruck/commit/8539d285b2c51d700aa811cd772d573fa0d613eb) Thanks [@juanrgm](https://github.com/juanrgm)! - Allow empty backup in restic repository
28
+
29
+ ## 0.1.0
30
+
31
+ ### Minor Changes
32
+
33
+ - [`88d46cd`](https://github.com/swordev/datatruck/commit/88d46cd56293df4c6fc21a9ad61d6236ac91f325) Thanks [@juanrgm](https://github.com/juanrgm)! - Add `custom` output format
34
+
35
+ ### Patch Changes
36
+
37
+ - [`24a1e5e`](https://github.com/swordev/datatruck/commit/24a1e5e86336e7a92556287e49548dc542f0e579) Thanks [@juanrgm](https://github.com/juanrgm)! - Update dependencies
38
+
3
39
  ## 0.0.6
4
40
 
5
41
  ### Patch Changes
@@ -8,15 +8,12 @@ const string_util_1 = require("../util/string-util");
8
8
  const CommandAbstract_1 = require("./CommandAbstract");
9
9
  class SnapshotsCommand extends CommandAbstract_1.CommandAbstract {
10
10
  onOptions() {
11
+ const groupByValues = ["packageName", "repositoryName", "repositoryType"];
11
12
  return this.returnsOptions({
12
13
  groupBy: {
13
14
  option: "-g,--group-by <values>",
14
- description: "Group by values",
15
- parser: (v) => (0, string_util_1.parseStringList)(v, [
16
- "packageName",
17
- "repositoryName",
18
- "repositoryType",
19
- ]),
15
+ description: `Group by values (${groupByValues.join(", ")})`,
16
+ parser: (v) => (0, string_util_1.parseStringList)(v, groupByValues),
20
17
  },
21
18
  id: {
22
19
  option: "-i,--id <ids>",
package/Config/Config.js CHANGED
@@ -7,6 +7,7 @@ exports.configDefinition = {
7
7
  required: ["repositories", "packages"],
8
8
  additionalProperties: false,
9
9
  properties: {
10
+ $schema: { type: "string" },
10
11
  repositories: {
11
12
  type: "array",
12
13
  items: (0, DefinitionEnum_1.makeRef)(DefinitionEnum_1.DefinitionEnum.repository),
@@ -3,6 +3,7 @@ import { MariadbTaskConfigType, mariadbTaskName } from "../Task/MariadbTask";
3
3
  import { MssqlTaskConfigType, mssqlTaskName } from "../Task/MssqlTask";
4
4
  import { MysqlDumpTaskConfigType, mysqlDumpTaskName } from "../Task/MysqlDumpTask";
5
5
  import { PostgresqlDumpTaskConfigType, postgresqlDumpTaskName } from "../Task/PostgresqlDumpTask";
6
+ import { ScriptTaskConfigType, scriptTaskName } from "../Task/ScriptTask";
6
7
  import { JSONSchema7 } from "json-schema";
7
8
  export declare const taskConfigDefinition: JSONSchema7;
8
9
  export declare type TaskConfigType = {
@@ -20,4 +21,7 @@ export declare type TaskConfigType = {
20
21
  } | {
21
22
  name: typeof postgresqlDumpTaskName;
22
23
  config: PostgresqlDumpTaskConfigType;
24
+ } | {
25
+ name: typeof scriptTaskName;
26
+ config: ScriptTaskConfigType;
23
27
  };
@@ -7,12 +7,14 @@ const MariadbTask_1 = require("../Task/MariadbTask");
7
7
  const MssqlTask_1 = require("../Task/MssqlTask");
8
8
  const MysqlDumpTask_1 = require("../Task/MysqlDumpTask");
9
9
  const PostgresqlDumpTask_1 = require("../Task/PostgresqlDumpTask");
10
+ const ScriptTask_1 = require("../Task/ScriptTask");
10
11
  const names = {
11
12
  [GitTask_1.gitTaskName]: DefinitionEnum_1.DefinitionEnum.gitTask,
12
13
  [MariadbTask_1.mariadbTaskName]: DefinitionEnum_1.DefinitionEnum.mariadbTask,
13
14
  [MssqlTask_1.mssqlTaskName]: DefinitionEnum_1.DefinitionEnum.mssqlTask,
14
15
  [MysqlDumpTask_1.mysqlDumpTaskName]: DefinitionEnum_1.DefinitionEnum.mysqlDumpTask,
15
16
  [PostgresqlDumpTask_1.postgresqlDumpTaskName]: DefinitionEnum_1.DefinitionEnum.postgresqlDumpTask,
17
+ [ScriptTask_1.scriptTaskName]: DefinitionEnum_1.DefinitionEnum.scriptTask,
16
18
  };
17
19
  exports.taskConfigDefinition = {
18
20
  type: "object",
@@ -7,6 +7,7 @@ const MariadbTask_1 = require("../Task/MariadbTask");
7
7
  const MssqlTask_1 = require("../Task/MssqlTask");
8
8
  const MysqlDumpTask_1 = require("../Task/MysqlDumpTask");
9
9
  const PostgresqlDumpTask_1 = require("../Task/PostgresqlDumpTask");
10
+ const ScriptTask_1 = require("../Task/ScriptTask");
10
11
  function TaskFactory(task) {
11
12
  if (task.name === GitTask_1.gitTaskName) {
12
13
  return new GitTask_1.GitTask(task.config ?? {});
@@ -23,6 +24,9 @@ function TaskFactory(task) {
23
24
  else if (task.name === MssqlTask_1.mssqlTaskName) {
24
25
  return new MssqlTask_1.MssqlTask(task.config ?? {});
25
26
  }
27
+ else if (task.name === ScriptTask_1.scriptTaskName) {
28
+ return new ScriptTask_1.ScriptTask(task.config ?? {});
29
+ }
26
30
  else {
27
31
  throw new AppError_1.AppError(`Invalid task name: ${task["name"]}`);
28
32
  }
@@ -11,6 +11,7 @@ export declare enum DefinitionEnum {
11
11
  gitRepository = "git-repository",
12
12
  gitPackageRepository = "git-package-repository",
13
13
  gitTask = "git-task",
14
+ scriptTask = "script-task",
14
15
  mariadbTask = "mariadb-task",
15
16
  mssqlTask = "mssql-task",
16
17
  mysqlDumpTask = "mysql-dump-task",
@@ -20,6 +21,6 @@ export declare enum DefinitionEnum {
20
21
  prunePolicy = "prune-policy",
21
22
  pathsObject = "paths-object"
22
23
  }
23
- export declare function makeRef(type: DefinitionEnum): {
24
+ export declare function makeRef(type: DefinitionEnum, subType?: string): {
24
25
  $ref: string;
25
26
  };
@@ -15,6 +15,7 @@ var DefinitionEnum;
15
15
  DefinitionEnum["gitRepository"] = "git-repository";
16
16
  DefinitionEnum["gitPackageRepository"] = "git-package-repository";
17
17
  DefinitionEnum["gitTask"] = "git-task";
18
+ DefinitionEnum["scriptTask"] = "script-task";
18
19
  DefinitionEnum["mariadbTask"] = "mariadb-task";
19
20
  DefinitionEnum["mssqlTask"] = "mssql-task";
20
21
  DefinitionEnum["mysqlDumpTask"] = "mysql-dump-task";
@@ -24,9 +25,9 @@ var DefinitionEnum;
24
25
  DefinitionEnum["prunePolicy"] = "prune-policy";
25
26
  DefinitionEnum["pathsObject"] = "paths-object";
26
27
  })(DefinitionEnum = exports.DefinitionEnum || (exports.DefinitionEnum = {}));
27
- function makeRef(type) {
28
+ function makeRef(type, subType) {
28
29
  return {
29
- $ref: `#/definitions/${type}`,
30
+ $ref: `#/definitions/${type}` + (subType ? `_${subType}` : ""),
30
31
  };
31
32
  }
32
33
  exports.makeRef = makeRef;
@@ -15,6 +15,7 @@ const MariadbTask_1 = require("../Task/MariadbTask");
15
15
  const MssqlTask_1 = require("../Task/MssqlTask");
16
16
  const MysqlDumpTask_1 = require("../Task/MysqlDumpTask");
17
17
  const PostgresqlDumpTask_1 = require("../Task/PostgresqlDumpTask");
18
+ const ScriptTask_1 = require("../Task/ScriptTask");
18
19
  const SqlDumpTaskAbstract_1 = require("../Task/SqlDumpTaskAbstract");
19
20
  const DefinitionEnum_1 = require("./DefinitionEnum");
20
21
  exports.definitions = {
@@ -35,6 +36,7 @@ exports.definitions = {
35
36
  [DefinitionEnum_1.DefinitionEnum.resticRepository]: ResticRepository_1.resticRepositoryDefinition,
36
37
  [DefinitionEnum_1.DefinitionEnum.resticPackageRepository]: ResticRepository_1.resticPackageRepositoryDefinition,
37
38
  [DefinitionEnum_1.DefinitionEnum.gitTask]: GitTask_1.gitTaskDefinition,
39
+ [DefinitionEnum_1.DefinitionEnum.scriptTask]: ScriptTask_1.scriptTaskDefinition,
38
40
  [DefinitionEnum_1.DefinitionEnum.sqlDumpTask]: SqlDumpTaskAbstract_1.sqlDumpTaskDefinition,
39
41
  [DefinitionEnum_1.DefinitionEnum.mariadbTask]: MariadbTask_1.mariadbTaskDefinition,
40
42
  [DefinitionEnum_1.DefinitionEnum.mssqlTask]: MssqlTask_1.mssqlTaskDefinition,
@@ -44,6 +46,18 @@ exports.definitions = {
44
46
  [DefinitionEnum_1.DefinitionEnum.prunePolicy]: PrunePolicyConfig_1.prunePolicyConfigDefinition,
45
47
  [DefinitionEnum_1.DefinitionEnum.pathsObject]: PackageConfig_1.pathsObjectDefinition,
46
48
  };
49
+ for (const key in exports.definitions) {
50
+ const schemaKey = key;
51
+ const schema = exports.definitions[schemaKey];
52
+ for (const defName in schema.definitions || {}) {
53
+ exports.definitions[`${schemaKey}_${defName}`] =
54
+ schema.definitions[defName];
55
+ }
56
+ exports.definitions[schemaKey] = {
57
+ ...schema,
58
+ };
59
+ delete exports.definitions[schemaKey].definitions;
60
+ }
47
61
  exports.schema = {
48
62
  definitions: exports.definitions,
49
63
  ...(0, DefinitionEnum_1.makeRef)(DefinitionEnum_1.DefinitionEnum.config),
@@ -205,6 +205,8 @@ class LocalRepository extends RepositoryAbstract_1.RepositoryAbstract {
205
205
  paths: stream,
206
206
  packs: compress?.packs,
207
207
  });
208
+ if (data.options.verbose)
209
+ (0, cli_util_1.logExec)(`Path lists: ${pathLists.path}`);
208
210
  let currentFiles = 0;
209
211
  if (compress?.packs) {
210
212
  let packIndex = 0;
@@ -1,13 +1,18 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.ResticRepository = exports.resticPackageRepositoryDefinition = exports.resticRepositoryDefinition = exports.resticRepositoryName = void 0;
4
7
  const AppError_1 = require("../Error/AppError");
5
8
  const ResticUtil_1 = require("../util/ResticUtil");
9
+ const cli_util_1 = require("../util/cli-util");
6
10
  const paths_util_1 = require("../util/datatruck/paths-util");
7
11
  const fs_util_1 = require("../util/fs-util");
8
12
  const string_util_1 = require("../util/string-util");
9
13
  const RepositoryAbstract_1 = require("./RepositoryAbstract");
10
14
  const assert_1 = require("assert");
15
+ const fast_glob_1 = __importDefault(require("fast-glob"));
11
16
  const micromatch_1 = require("micromatch");
12
17
  const path_1 = require("path");
13
18
  exports.resticRepositoryName = "restic";
@@ -54,7 +59,7 @@ class ResticRepository extends RepositoryAbstract_1.RepositoryAbstract {
54
59
  if (this.env)
55
60
  return this.env;
56
61
  return (this.env = {
57
- RESTIC_PASSWORD_FILE: this.config.passwordFile,
62
+ RESTIC_PASSWORD_FILE: (0, path_1.resolve)(this.config.passwordFile),
58
63
  RESTIC_REPOSITORY: await ResticUtil_1.ResticUtil.formatRepository(this.config.repository),
59
64
  });
60
65
  }
@@ -150,14 +155,31 @@ class ResticRepository extends RepositoryAbstract_1.RepositoryAbstract {
150
155
  const pkg = data.package;
151
156
  const sourcePath = data.targetPath ?? data.package.path;
152
157
  (0, assert_1.ok)(sourcePath);
153
- const include = (await (0, paths_util_1.parsePaths)(pkg.include ?? [], {
154
- cwd: sourcePath,
155
- verbose: data.options.verbose,
156
- })).map(path_1.normalize);
157
- // https://github.com/restic/restic/issues/233
158
- // https://github.com/restic/restic/pull/2311
159
- if (include.length)
160
- throw new AppError_1.AppError(`Include is not supported`);
158
+ let gitignorePath;
159
+ if (pkg.include || pkg.exclude) {
160
+ const include = await (0, paths_util_1.parsePaths)(pkg.include ?? ["**"], {
161
+ cwd: sourcePath,
162
+ verbose: data.options.verbose,
163
+ });
164
+ const exclude = pkg.exclude
165
+ ? await (0, paths_util_1.parsePaths)(pkg.exclude, {
166
+ cwd: sourcePath,
167
+ verbose: data.options.verbose,
168
+ })
169
+ : undefined;
170
+ const stream = fast_glob_1.default.stream(include, {
171
+ cwd: sourcePath,
172
+ ignore: exclude,
173
+ dot: true,
174
+ onlyFiles: true,
175
+ markDirectories: true,
176
+ });
177
+ if (data.options.verbose)
178
+ (0, cli_util_1.logExec)(`Writing paths lists`);
179
+ gitignorePath = await (0, fs_util_1.writeGitIgnoreList)({
180
+ paths: stream,
181
+ });
182
+ }
161
183
  if (data.options.tags?.some((tag) => tag.startsWith(ResticRepository.refPrefix)))
162
184
  throw new AppError_1.AppError(`Tag prefix is not allowed`);
163
185
  const packageTag = ResticRepository.buildSnapshotTag(RepositoryAbstract_1.SnapshotTagEnum.PACKAGE, data.package.name);
@@ -170,15 +192,13 @@ class ResticRepository extends RepositoryAbstract_1.RepositoryAbstract {
170
192
  await restic.backup({
171
193
  cwd: sourcePath,
172
194
  paths: ["."],
195
+ allowEmptySnapshot: true,
196
+ excludeFile: gitignorePath ? [gitignorePath] : undefined,
173
197
  parent: lastSnapshot?.id,
174
198
  // https://github.com/restic/restic/pull/3200
175
199
  ...((await restic.checkBackupSetPathSupport()) && {
176
200
  setPaths: [`/datatruck/${data.package.name}`],
177
201
  }),
178
- exclude: (await (0, paths_util_1.parsePaths)(pkg.exclude ?? [], {
179
- cwd: sourcePath,
180
- verbose: data.options.verbose,
181
- })).map(path_1.normalize),
182
202
  tags: [
183
203
  ResticRepository.buildSnapshotTag(RepositoryAbstract_1.SnapshotTagEnum.ID, data.snapshot.id),
184
204
  ResticRepository.buildSnapshotTag(RepositoryAbstract_1.SnapshotTagEnum.SHORT_ID, data.snapshot.id.slice(0, 8)),
package/Task/MssqlTask.js CHANGED
@@ -96,6 +96,7 @@ class MssqlTask extends TaskAbstract_1.TaskAbstract {
96
96
  database: databaseName,
97
97
  packageName: data.package.name,
98
98
  snapshotId: data.options.snapshotId,
99
+ snapshotDate: data.snapshot.date,
99
100
  });
100
101
  const databasePath = (0, path_1.join)(restorePath, file);
101
102
  const exists = await this.fetchDatabaseNames(databaseName);
@@ -0,0 +1,42 @@
1
+ import { BackupDataType, RestoreDataType, TaskAbstract } from "./TaskAbstract";
2
+ import { JSONSchema7 } from "json-schema";
3
+ export declare type ProcessStepConfig = {
4
+ command: string;
5
+ env?: Record<string, string>;
6
+ args?: string[];
7
+ };
8
+ export declare type NodeStepConfig = {
9
+ env?: Record<string, string>;
10
+ code: string | string[];
11
+ };
12
+ export declare type Step = {
13
+ type: "process";
14
+ config: ProcessStepConfig;
15
+ } | {
16
+ type: "node";
17
+ config: NodeStepConfig;
18
+ };
19
+ export declare type ScriptTaskConfigType = {
20
+ env?: Record<string, string | undefined>;
21
+ backupSteps: Step[];
22
+ restoreSteps: Step[];
23
+ };
24
+ export declare const scriptTaskName = "script";
25
+ export declare const scriptTaskDefinition: JSONSchema7;
26
+ export declare class ScriptTask extends TaskAbstract<ScriptTaskConfigType> {
27
+ protected verbose?: boolean;
28
+ onBeforeBackup(): Promise<{
29
+ targetPath: string;
30
+ }>;
31
+ protected getVars(data: BackupDataType | RestoreDataType): Record<string, string | undefined>;
32
+ static processSteps(input: Step[] | Step, options: {
33
+ env?: Record<string, string | undefined>;
34
+ vars: Record<string, string | undefined>;
35
+ verbose?: boolean;
36
+ }): Promise<void>;
37
+ onBackup(data: BackupDataType): Promise<void>;
38
+ onBeforeRestore(): Promise<{
39
+ targetPath: string;
40
+ }>;
41
+ onRestore(data: RestoreDataType): Promise<void>;
42
+ }
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ScriptTask = exports.scriptTaskDefinition = exports.scriptTaskName = void 0;
4
+ const DefinitionEnum_1 = require("../JsonSchema/DefinitionEnum");
5
+ const fs_util_1 = require("../util/fs-util");
6
+ const process_util_1 = require("../util/process-util");
7
+ const string_util_1 = require("../util/string-util");
8
+ const TaskAbstract_1 = require("./TaskAbstract");
9
+ const assert_1 = require("assert");
10
+ const promises_1 = require("fs/promises");
11
+ const path_1 = require("path");
12
+ var ScriptTaskDefinitionEnum;
13
+ (function (ScriptTaskDefinitionEnum) {
14
+ ScriptTaskDefinitionEnum["step"] = "step";
15
+ ScriptTaskDefinitionEnum["processStepConfig"] = "processStepConfig";
16
+ ScriptTaskDefinitionEnum["nodeStepConfig"] = "nodeStepConfig";
17
+ })(ScriptTaskDefinitionEnum || (ScriptTaskDefinitionEnum = {}));
18
+ const stepTypes = {
19
+ process: ScriptTaskDefinitionEnum.processStepConfig,
20
+ node: ScriptTaskDefinitionEnum.nodeStepConfig,
21
+ };
22
+ exports.scriptTaskName = "script";
23
+ exports.scriptTaskDefinition = {
24
+ definitions: {
25
+ step: {
26
+ type: "object",
27
+ required: ["type"],
28
+ properties: {
29
+ type: { enum: Object.keys(stepTypes) },
30
+ config: {},
31
+ },
32
+ anyOf: Object.keys(stepTypes).map((name) => ({
33
+ if: {
34
+ type: "object",
35
+ properties: {
36
+ type: { const: name },
37
+ },
38
+ },
39
+ then: {
40
+ type: "object",
41
+ properties: {
42
+ config: (0, DefinitionEnum_1.makeRef)(DefinitionEnum_1.DefinitionEnum.scriptTask, stepTypes[name]),
43
+ },
44
+ },
45
+ else: false,
46
+ })),
47
+ },
48
+ processStepConfig: {
49
+ type: "object",
50
+ required: ["command"],
51
+ properties: {
52
+ command: { type: "string" },
53
+ env: {
54
+ type: "object",
55
+ patternProperties: { ".+": { type: "string" } },
56
+ },
57
+ args: (0, DefinitionEnum_1.makeRef)(DefinitionEnum_1.DefinitionEnum.stringListUtil),
58
+ },
59
+ },
60
+ nodeStepConfig: {
61
+ type: "object",
62
+ required: ["code"],
63
+ properties: {
64
+ code: {
65
+ anyOf: [{ type: "string" }, (0, DefinitionEnum_1.makeRef)(DefinitionEnum_1.DefinitionEnum.stringListUtil)],
66
+ },
67
+ env: {
68
+ type: "object",
69
+ patternProperties: { ".+": { type: "string" } },
70
+ },
71
+ },
72
+ },
73
+ },
74
+ type: "object",
75
+ additionalProperties: false,
76
+ required: ["backupSteps", "restoreSteps"],
77
+ properties: {
78
+ env: {
79
+ type: "object",
80
+ patternProperties: {
81
+ ".+": { type: "string" },
82
+ },
83
+ },
84
+ backupSteps: {
85
+ type: "array",
86
+ items: (0, DefinitionEnum_1.makeRef)(DefinitionEnum_1.DefinitionEnum.scriptTask, ScriptTaskDefinitionEnum.step),
87
+ },
88
+ restoreSteps: {
89
+ type: "array",
90
+ items: (0, DefinitionEnum_1.makeRef)(DefinitionEnum_1.DefinitionEnum.scriptTask, ScriptTaskDefinitionEnum.step),
91
+ },
92
+ },
93
+ };
94
+ class ScriptTask extends TaskAbstract_1.TaskAbstract {
95
+ async onBeforeBackup() {
96
+ return {
97
+ targetPath: await (0, fs_util_1.mkTmpDir)(ScriptTask.name),
98
+ };
99
+ }
100
+ getVars(data) {
101
+ return {
102
+ DTT_SNAPSHOT_ID: data.snapshot.id,
103
+ DTT_SNAPSHOT_DATE: data.snapshot.date,
104
+ DTT_PACKAGE_NAME: data.package.name,
105
+ DTT_PACKAGE_PATH: data.package.path,
106
+ DTT_TARGET_PATH: data.targetPath,
107
+ };
108
+ }
109
+ static async processSteps(input, options) {
110
+ const steps = Array.isArray(input) ? input : [input];
111
+ for (const step of steps) {
112
+ if (step.type === "process") {
113
+ await (0, process_util_1.exec)(step.config.command, (step.config.args || []).map((v) => (0, string_util_1.render)(v, options.vars)), {
114
+ env: {
115
+ ...process.env,
116
+ ...options.vars,
117
+ ...options.env,
118
+ ...step.config.env,
119
+ },
120
+ }, {
121
+ log: options.verbose,
122
+ });
123
+ }
124
+ else if (step.type === "node") {
125
+ const tempDir = await (0, fs_util_1.mkTmpDir)("script-task-node-step");
126
+ const scriptPath = (0, path_1.join)(tempDir, "script.js");
127
+ await (0, promises_1.writeFile)(scriptPath, Array.isArray(step.config.code)
128
+ ? step.config.code.join("\n")
129
+ : step.config.code);
130
+ await (0, process_util_1.exec)("node", [scriptPath], {
131
+ env: {
132
+ ...process.env,
133
+ ...options.vars,
134
+ ...options.env,
135
+ ...step.config.env,
136
+ },
137
+ }, {
138
+ log: options.verbose,
139
+ });
140
+ }
141
+ else {
142
+ throw new Error(`Invalid step type: ${step.type}`);
143
+ }
144
+ }
145
+ }
146
+ async onBackup(data) {
147
+ this.verbose = data.options.verbose;
148
+ const config = this.config;
149
+ const path = data.package.path;
150
+ const targetPath = data.targetPath;
151
+ (0, assert_1.ok)(typeof path === "string");
152
+ (0, assert_1.ok)(typeof targetPath === "string");
153
+ await ScriptTask.processSteps(config.backupSteps, {
154
+ env: config.env,
155
+ vars: this.getVars(data),
156
+ verbose: this.verbose,
157
+ });
158
+ }
159
+ async onBeforeRestore() {
160
+ return {
161
+ targetPath: await (0, fs_util_1.mkTmpDir)(ScriptTask.name),
162
+ };
163
+ }
164
+ async onRestore(data) {
165
+ this.verbose = data.options.verbose;
166
+ const config = this.config;
167
+ const restorePath = data.package.restorePath;
168
+ const targetPath = data.targetPath;
169
+ (0, assert_1.ok)(typeof restorePath === "string");
170
+ (0, assert_1.ok)(typeof targetPath === "string");
171
+ await (0, fs_util_1.mkdirIfNotExists)(restorePath);
172
+ await (0, fs_util_1.ensureEmptyDir)(restorePath);
173
+ await ScriptTask.processSteps(config.restoreSteps, {
174
+ env: config.env,
175
+ vars: this.getVars(data),
176
+ verbose: this.verbose,
177
+ });
178
+ }
179
+ }
180
+ exports.ScriptTask = ScriptTask;
@@ -111,6 +111,7 @@ class SqlDumpTaskAbstract extends TaskAbstract_1.TaskAbstract {
111
111
  name: (0, config_util_1.resolveDatabaseName)(this.config.database, {
112
112
  packageName: data.package.name,
113
113
  snapshotId: data.options.snapshotId,
114
+ snapshotDate: data.snapshot.date,
114
115
  action: "restore",
115
116
  database: undefined,
116
117
  }),
@@ -119,6 +120,7 @@ class SqlDumpTaskAbstract extends TaskAbstract_1.TaskAbstract {
119
120
  database.name = (0, config_util_1.resolveDatabaseName)(this.config.targetDatabase.name, {
120
121
  packageName: data.package.name,
121
122
  snapshotId: data.options.snapshotId,
123
+ snapshotDate: data.snapshot.date,
122
124
  action: "restore",
123
125
  database: database.name,
124
126
  });
package/cli.js CHANGED
@@ -63,7 +63,7 @@ program.version(version);
63
63
  program.description(description);
64
64
  program.option("-v,--verbose", "Verbose", (_, previous) => previous + 1, 0);
65
65
  program.option("-c,--config <path>", "Config path", process.env["DATATRUCK_CONFIG"] ?? (cwd.endsWith(path_1.sep) ? cwd : `${cwd}${path_1.sep}`));
66
- program.option("-o,--output-format <format>", "Output format (json, pjson, yaml, table)", "table");
66
+ program.option("-o,--output-format <format>", "Output format (json, pjson, yaml, table, custom=$)", "table");
67
67
  makeCommand(CommandFactory_1.CommandEnum.config).alias("c");
68
68
  makeCommand(CommandFactory_1.CommandEnum.init).alias("i");
69
69
  makeCommand(CommandFactory_1.CommandEnum.snapshots).alias("s");