-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
cli.js
executable file
·478 lines (387 loc) · 13.1 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/env node
'use strict';
import meow from 'meow';
import AWS from 'aws-sdk';
import pMap from 'p-map';
import { readFile, writeFile } from 'fs/promises';
import { createReadStream, createWriteStream } from 'fs';
import sanitizeFilename from 'sanitize-filename';
import JSONStream from 'JSONStream';
import { pipeline as pipelineCb } from 'stream';
import { promisify } from 'util';
import Debug from 'debug';
import throttle from 'lodash/throttle.js';
import pick from 'lodash/pick.js';
import https from 'https';
const pipeline = promisify(pipelineCb);
const debug = Debug('dynamodump');
const cli = meow(`
Usage
$ dynamodump list-tables <options> List tables, separated by space
$ dynamodump export-schema <options> Export schema of a table
$ dynamodump import-schema <options> Import schema of a table (creates the table)
$ dynamodump export-all-schema <options> Export schema of all tables
$ dynamodump export-data <options> Export all data of a table
$ dynamodump import-data <options> Import all data into a table
$ dynamodump export-all-data <options> Export data from all tables
$ dynamodump export-all <options> Export data and schema from all tables
$ dynamodump wipe-data <options> Wipe all data from a table
AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
is specified in env variables or ~/.aws/credentials
Options
--region AWS region
--file File name to export to or import from (defaults to table_name.dynamoschema and table_name.dynamodata)
--table Table to export. When importing, this will override the TableName from the schema dump file
--wait-for-active Wait for table to become active when importing schema
--profile utilize named profile from .aws/credentials file
--throughput How many rows to delete in parallel (wipe-data)
--max-retries Set AWS maxRetries
--marshall Converts JSON to/from DynamoDB record on import/export
--endpoint Endpoint URL for DynamoDB Local
--ca-file Set SSL certificate authority file
--stack-trace Log stack trace upon error
--dry-run Report the actions that would be made without actually runnning them.
--log-level Set log level: debug, info, warn, error
Examples
dynamodump export-schema --region=eu-west-1 --table=your-table --file=your-schema-dump
dynamodump import-schema --region=eu-west-1 --file=your-schema-dump --table=your-table --wait-for-active
dynamodump export-all-data --region=eu-west-1
dynamodump import-data --region=eu-west-1 --table=mikael-test --file=mikael-test.dynamodata
dynamodump wipe-data --region=eu-west-1 --table=mikael-test --throughput=10
`,
{
importMeta: import.meta,
flags: {
stackTrace: {
type: 'boolean',
},
waitForActive: {
type: 'boolean',
},
dryRun: {
type: 'boolean',
},
}
});
const logger = (() => {
const levels = [
'debug',
'info',
'warn',
'error',
];
const currentLogLevelIndex = levels.indexOf(cli.flags.logLevel || 'info');
const log = (level, ...args) => {
if (levels.indexOf(level) < currentLogLevelIndex) return;
if (cli.flags.quiet) return;
if (level === 'error') return console.error(...args);
if (level === 'warn') return console.warn(...args);
console.log(...args);
}
return {
error: (...args) => log('error', ...args),
info: (...args) => log('info', ...args),
warn: (...args) => log('warn', ...args),
debug: (...args) => log('debug', ...args),
}
})();
if (cli.flags.maxRetries != null) AWS.config.maxRetries = cli.flags.maxRetries;
if (cli.flags.profile) {
AWS.config.credentials = new AWS.SharedIniFileCredentials({profile: cli.flags.profile});
}
if (cli.flags.caFile) {
logger.info('Using self signed cert', cli.flags.caFile);
const ca = await readFile(cli.flags.caFile);
AWS.config.update({
httpOptions: { agent: new https.Agent({ ca }) }
});
}
const { dryRun, table: tableName, region, endpoint } = cli.flags;
function createDynamoDb() {
const dynamoDbParams = { region };
if (endpoint) dynamoDbParams.endpoint = endpoint;
return new AWS.DynamoDB(dynamoDbParams);
}
async function listTablesCli() {
const tables = await listTables();
if (tables.length === 0) {
logger.info('No tables found');
return;
}
logger.info(tables.join(' '));
}
function listTables() {
const dynamoDb = createDynamoDb();
const params = {};
let tables = [];
async function listTablesPaged() {
const data = await dynamoDb.listTables(params).promise();
tables = tables.concat(data.TableNames);
if (data.LastEvaluatedTableName !== undefined) {
params.ExclusiveStartTableName = data.LastEvaluatedTableName;
return listTablesPaged();
}
return tables;
};
return listTablesPaged();
}
async function exportSchemaCli() {
logger.info('Exporting schema for table', tableName);
if (dryRun) return;
if (!tableName) {
logger.error('--table is requred')
cli.showHelp();
}
return exportSchema(tableName, cli.flags.file)
}
async function exportAllSchemaCli() {
return pMap(await listTables(), async (tableName) => {
logger.info('Exporting schema for table', tableName);
if (dryRun) return;
await exportSchema(tableName, null);
}, { concurrency: 1 });
}
async function exportSchema(tableName, file) {
const dynamoDb = createDynamoDb();
const data = await dynamoDb.describeTable({ TableName: tableName }).promise();
const table = data.Table;
const file2 = file || sanitizeFilename(tableName + '.dynamoschema');
return writeFile(file2, JSON.stringify(table, null, 2))
}
async function importSchemaCli() {
const file = cli.flags.file;
const waitForActive = cli.flags.waitForActive;
if (!file) {
logger.error('--file is requred')
cli.showHelp();
}
logger.info('Importing schema for table', tableName, 'from file', file);
const dynamoDb = createDynamoDb();
async function doWaitForActive() {
const retries = 60;
for (let i = 0; i < retries; i += 1) {
const data = await dynamoDb.describeTable({ TableName: tableName }).promise();
if (data.Table.TableStatus === 'ACTIVE') return;
await new Promise((resolve) => setTimeout(resolve, 1000));
}
throw new Error('Timed out');
}
const json = await readFile(file)
const data = JSON.parse(json)
if (tableName) data.TableName = tableName;
filterTable(data);
if (dryRun) return;
await dynamoDb.createTable(data).promise()
if (waitForActive) return doWaitForActive();
}
function filterTable(table) {
delete table.TableStatus;
delete table.CreationDateTime;
delete table.ProvisionedThroughput.LastIncreaseDateTime;
delete table.ProvisionedThroughput.LastDecreaseDateTime;
delete table.ProvisionedThroughput.NumberOfDecreasesToday;
delete table.TableSizeBytes;
delete table.ItemCount;
delete table.TableArn;
delete table.LatestStreamLabel;
delete table.LatestStreamArn;
delete table.TableId;
delete table.SSEDescription;
if (table.BillingModeSummary) {
table.BillingMode = table.BillingModeSummary.BillingMode;
}
delete table.BillingModeSummary;
// See https://github.com/mifi/dynamodump/pull/12/files
if (table.BillingMode === 'PAY_PER_REQUEST') {
delete table.ProvisionedThroughput;
}
function handleIndex(index) {
// See https://github.com/mifi/dynamodump/pull/12/files
if (table.BillingMode === 'PAY_PER_REQUEST') {
delete index.ProvisionedThroughput;
} else if (index.ProvisionedThroughput) { // https://github.com/mifi/dynamodump/issues/26
delete index.ProvisionedThroughput.LastIncreaseDateTime;
delete index.ProvisionedThroughput.LastDecreaseDateTime;
delete index.ProvisionedThroughput.NumberOfDecreasesToday;
}
}
(table.LocalSecondaryIndexes || []).forEach(index => {
delete index.IndexSizeBytes;
delete index.ItemCount;
delete index.IndexArn;
handleIndex(index);
});
(table.GlobalSecondaryIndexes || []).forEach(index => {
delete index.IndexStatus;
delete index.IndexSizeBytes;
delete index.ItemCount;
delete index.IndexArn;
handleIndex(index);
});
}
function getThroughput(defaultThroughput) {
if (cli.flags.throughput == null) return defaultThroughput;
if (Number.isInteger(cli.flags.throughput) && cli.flags.throughput > 0) {
return cli.flags.throughput;
} else {
logger.error('--throughput must be a positive integer');
cli.showHelp();
}
}
async function importDataCli() {
const file = cli.flags.file;
if (!tableName) {
logger.error('--table is required')
cli.showHelp();
}
if (!file) {
logger.error('--file is required')
cli.showHelp();
}
const throughput = getThroughput(1);
const dynamoDb = createDynamoDb();
logger.info('Importing data for table', tableName, 'from file', file);
if (dryRun) return;
const readStream = createReadStream(file);
const parseStream = JSONStream.parse('*');
let n = 0;
const logProgress = () => logger.debug('Imported', n, 'items');
const logThrottled = throttle(logProgress, 5000, { trailing: false });
readStream.pipe(parseStream)
.on('data', async (data) => {
debug('data');
if (cli.flags.marshall) {
data = AWS.DynamoDB.Converter.marshall(data);
}
n++;
if (n >= throughput) {
parseStream.pause();
}
try {
await dynamoDb.putItem({ TableName: tableName, Item: data }).promise();
logThrottled();
parseStream.resume();
} catch (err) {
parseStream.emit('error', err);
}
});
await new Promise((resolve, reject) => {
parseStream.on('end', resolve);
parseStream.on('error', reject);
})
}
async function exportDataCli() {
if (!tableName) {
logger.error('--table is required')
cli.showHelp();
}
logger.info('Exporting data for table', tableName);
if (dryRun) return;
return exportData(tableName, cli.flags.file);
}
async function exportAllDataCli() {
return pMap(await listTables(), async (tableName) => {
logger.info('Exporting data for table', tableName);
if (dryRun) return;
await exportData(tableName, null);
}, { concurrency: 1 });
}
async function exportData(tableName, file) {
const dynamoDb = createDynamoDb();
const file2 = file || sanitizeFilename(tableName + '.dynamodata');
const writeStream = createWriteStream(file2);
const stringify = JSONStream.stringify();
let n = 0;
const params = { TableName: tableName };
async function scanPage() {
const data = await dynamoDb.scan(params).promise();
data.Items.forEach((item) => {
if (cli.flags.marshall) {
item = AWS.DynamoDB.Converter.unmarshall(item);
}
return stringify.write(item)
});
n += data.Items.length;
logger.debug('Exported', n, 'items');
if (data.LastEvaluatedKey !== undefined) {
params.ExclusiveStartKey = data.LastEvaluatedKey;
return scanPage();
} else {
stringify.end();
}
}
scanPage()
await pipeline(stringify, writeStream);
}
async function exportAllCli() {
return pMap(await listTables(), async (tableName) => {
logger.info('Exporting schema and data for table', tableName);
if (dryRun) return;
await exportSchema(tableName, null);
await exportData(tableName, null);
}, { concurrency: 1 });
}
async function wipeDataCli() {
if (!tableName) {
logger.error('--table is required')
cli.showHelp();
}
logger.info('Wiping data for table', tableName);
if (dryRun) return;
return wipeData(tableName, getThroughput(10));
}
async function wipeData(tableName, throughput) {
const dynamoDb = createDynamoDb();
let n = 0;
const params = {
TableName: tableName,
Limit: throughput
};
async function scanPage(keyFields) {
const data = await dynamoDb.scan(params).promise()
await pMap(data.Items, (item) => {
const delParams = {
TableName: tableName,
Key: pick(item, keyFields)
};
return dynamoDb.deleteItem(delParams).promise();
}, { concurrency: 10 })
n += data.Items.length;
logger.debug('Wiped', n, 'items');
if (data.LastEvaluatedKey !== undefined) {
params.ExclusiveStartKey = data.LastEvaluatedKey;
return scanPage(keyFields);
}
}
const table = await dynamoDb.describeTable({ TableName: tableName }).promise();
const hashKeyElement = table.Table.KeySchema.filter((entry) => entry.KeyType === 'HASH');
const rangeKeyElement = table.Table.KeySchema.filter((entry) => entry.KeyType === 'RANGE');
const keyFields = [];
keyFields.push(hashKeyElement[0].AttributeName);
if (rangeKeyElement && rangeKeyElement.length > 0) {
keyFields.push(rangeKeyElement[0].AttributeName);
}
return scanPage(keyFields);
}
const methods = {
'export-schema': exportSchemaCli,
'import-schema': importSchemaCli,
'list-tables': listTablesCli,
'export-all-schema': exportAllSchemaCli,
'export-data': exportDataCli,
'export-all-data': exportAllDataCli,
'export-all': exportAllCli,
'import-data': importDataCli,
'wipe-data': wipeDataCli
};
const method = methods[cli.input[0]] || cli.showHelp();
try {
await method()
} catch (err) {
if (cli.flags.stackTrace) {
logger.error('Error:', err);
} else {
logger.error('Error:', err.message);
}
process.exitCode = 1;
}