forked from mifi/dynamodump
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·389 lines (317 loc) · 11 KB
/
cli.js
File metadata and controls
executable file
·389 lines (317 loc) · 11 KB
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
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const AWS = require('aws-sdk');
const bluebird = require('bluebird');
const fs = require('fs');
const sanitizeFilename = require('sanitize-filename');
const promisePoller = require('promise-poller').default;
const JSONStream = require('JSONStream');
const streamToPromise = require('stream-to-promise');
const debug = require('debug')('dynamodump');
const _ = require('lodash');
bluebird.promisifyAll(fs);
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
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
`);
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
};
if (cli.flags.maxRetries !== undefined) AWS.config.maxRetries = cli.flags.maxRetries;
const method = methods[cli.input[0]] || cli.showHelp();
if (cli.flags.profile) {
AWS.config.credentials = new AWS.SharedIniFileCredentials({profile: cli.flags.profile});
}
bluebird.resolve(method.call(undefined, cli))
.catch(err => {
console.error(err.stack);
process.exit(1);
});
function listTablesCli(cli) {
const region = cli.flags.region;
return listTables(region)
.then(tables => console.log(tables.join(' ')));
}
function listTables(region) {
const dynamoDb = new AWS.DynamoDB({ region });
const params = {};
let tables = [];
const listTablesPaged = () => {
return dynamoDb.listTables(params).promise()
.then(data => {
tables = tables.concat(data.TableNames);
if (data.LastEvaluatedTableName !== undefined) {
params.ExclusiveStartTableName = data.LastEvaluatedTableName;
return listTablesPaged();
}
return tables;
});
};
return listTablesPaged();
}
function exportSchemaCli(cli) {
const tableName = cli.flags.table;
if (!tableName) {
console.error('--table is requred')
cli.showHelp();
}
return exportSchema(tableName, cli.flags.file, cli.flags.region)
}
function exportAllSchemaCli(cli) {
const region = cli.flags.region;
return bluebird.map(listTables(region), tableName => {
console.error(`Exporting ${tableName}`);
return exportSchema(tableName, null, region);
}, { concurrency: 1 });
}
function exportSchema(tableName, file, region) {
const dynamoDb = new AWS.DynamoDB({ region });
return dynamoDb.describeTable({ TableName: tableName }).promise()
.then(data => {
const table = data.Table;
const file2 = file || sanitizeFilename(tableName + '.dynamoschema');
return fs.writeFileAsync(file2, JSON.stringify(table, null, 2))
});
}
function importSchemaCli(cli) {
const tableName = cli.flags.table;
const file = cli.flags.file;
const region = cli.flags.region;
const waitForActive = cli.flags.waitForActive;
if (!file) {
console.error('--file is requred')
cli.showHelp();
}
const dynamoDb = new AWS.DynamoDB({ region });
const doWaitForActive = () => promisePoller({
taskFn: () => {
return dynamoDb.describeTable({ TableName: tableName }).promise()
.then(data => {
if (data.Table.TableStatus !== 'ACTIVE') throw new Error();
});
},
interval: 1000,
retries: 60
});
fs.readFileAsync(file)
.then(data => JSON.parse(data))
.then(json => {
if (tableName) json.TableName = tableName;
filterTable(json);
return dynamoDb.createTable(json).promise()
.then(() => {
if (waitForActive !== undefined) {
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;
(table.LocalSecondaryIndexes || []).forEach(index => {
delete index.IndexSizeBytes;
delete index.ItemCount;
delete index.IndexArn;
});
(table.GlobalSecondaryIndexes || []).forEach(index => {
delete index.IndexStatus;
delete index.IndexSizeBytes;
delete index.ItemCount;
delete index.IndexArn;
delete index.ProvisionedThroughput.NumberOfDecreasesToday;
});
}
function importDataCli(cli) {
const tableName = cli.flags.table;
const file = cli.flags.file;
const region = cli.flags.region;
if (!tableName) {
console.error('--table is requred')
cli.showHelp();
}
if (!file) {
console.error('--file is requred')
cli.showHelp();
}
let throughput = 1;
if (cli.flags.throughput !== undefined) {
if (Number.isInteger(cli.flags.throughput) && cli.flags.throughput > 0) {
throughput = cli.flags.throughput;
} else {
console.error('--throughput must be a positive integer');
cli.showHelp();
return;
}
}
const dynamoDb = new AWS.DynamoDB({ region });
const readStream = fs.createReadStream(file);
const parseStream = JSONStream.parse('*');
let n = 0;
const logProgress = () => console.error('Imported', n, 'items');
const logThrottled = _.throttle(logProgress, 5000, { trailing: false });
readStream.pipe(parseStream)
.on('data', data => {
debug('data');
n++;
logThrottled();
if (n >= throughput) {
parseStream.pause();
}
dynamoDb.putItem({ TableName: tableName, Item: data }).promise()
.then(() => parseStream.resume())
.catch(err => parseStream.emit('error', err));
});
return new Promise((resolve, reject) => {
parseStream.on('end', resolve);
parseStream.on('error', reject);
})
.then(() => logProgress());
}
function exportDataCli(cli) {
const tableName = cli.flags.table;
if (!tableName) {
console.error('--table is requred')
cli.showHelp();
}
return exportData(tableName, cli.flags.file, cli.flags.region);
}
function exportAllDataCli(cli) {
const region = cli.flags.region;
return bluebird.map(listTables(region), tableName => {
console.error(`Exporting ${tableName}`);
return exportData(tableName, null, region);
}, { concurrency: 1 });
}
function exportData(tableName, file, region) {
const dynamoDb = new AWS.DynamoDB({ region });
const file2 = file || sanitizeFilename(tableName + '.dynamodata');
const writeStream = fs.createWriteStream(file2);
const stringify = JSONStream.stringify();
stringify.pipe(writeStream);
let n = 0;
const params = { TableName: tableName };
const scanPage = () => {
return bluebird.resolve(dynamoDb.scan(params).promise())
.then(data => {
data.Items.forEach(item => stringify.write(item));
n += data.Items.length;
console.error('Exported', n, 'items');
if (data.LastEvaluatedKey !== undefined) {
params.ExclusiveStartKey = data.LastEvaluatedKey;
return scanPage();
}
});
}
return scanPage()
.finally(() => {
stringify.end();
return streamToPromise(stringify);
})
.finally(() => writeStream.end());
}
function exportAllCli(cli) {
const region = cli.flags.region;
return bluebird.map(listTables(region), tableName => {
console.error(`Exporting ${tableName}`);
return exportSchema(tableName, null, region)
.then(() => exportData(tableName, null, region))
}, { concurrency: 1 });
}
function wipeDataCli(cli) {
const tableName = cli.flags.table;
if (!tableName) {
console.error('--table is requred')
cli.showHelp();
}
let throughput = 10;
if (cli.flags.throughput !== undefined) {
if (Number.isInteger(cli.flags.throughput) && cli.flags.throughput > 0) {
throughput = cli.flags.throughput;
} else {
console.error('--throughput must be a positive integer');
cli.showHelp();
}
}
return wipeData(tableName, cli.flags.region, throughput);
}
function wipeData(tableName, region, throughput) {
const dynamoDb = new AWS.DynamoDB({ region });
let n = 0;
const params = {
TableName: tableName,
Limit: throughput
};
const scanPage = (keyFields) => {
return bluebird.resolve(dynamoDb.scan(params).promise())
.then(data => {
return bluebird.map(data.Items, item => {
const delParams = {
TableName: tableName,
Key: _.pick(item, keyFields)
};
return dynamoDb.deleteItem(delParams).promise();
}).then(() => {
n += data.Items.length;
console.error('Wiped', n, 'items');
if (data.LastEvaluatedKey !== undefined) {
params.ExclusiveStartKey = data.LastEvaluatedKey;
return scanPage(keyFields);
}
});
});
}
return dynamoDb.describeTable({ TableName: tableName }).promise()
.then((table) => {
const hashKeyElement = _.filter(table.Table.KeySchema, entry => entry.KeyType === 'HASH');
const rangeKeyElement = _.filter(table.Table.KeySchema, entry => entry.KeyType === 'RANGE');
const keyFields = [];
keyFields.push(hashKeyElement[0].AttributeName);
if (rangeKeyElement && rangeKeyElement.length > 0) {
keyFields.push(rangeKeyElement[0].AttributeName);
}
return scanPage(keyFields);
});
}