forked from tensult/aws-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enable_dynamodb_table_backup.js
71 lines (66 loc) · 2.61 KB
/
enable_dynamodb_table_backup.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
/**
* DynamoDB table backups provides disaster recovery for tables in case of accidental deletes
* so it is recommended to enable table backups for production tables.
* To know more, you can read this blog:
* https://medium.com/tensult/aws-dynamodb-point-in-time-recovery-e8711d6d04cb
*/
const awsConfigHelper = require('./util/awsConfigHelper');
const AWS = require('aws-sdk');
const cli = require('cli');
const cliArgs = cli.parse({
region: ['r', 'AWS region', 'string'],
tablePrefix: ['t', 'dynamodb table prefix', 'string'],
});
if (!cliArgs.region) {
cli.getUsage();
}
let isCompleted = false;
let nextToken = undefined;
async function enableTableBackup() {
await awsConfigHelper.updateConfig(cliArgs.region);
const dynamodb = new AWS.DynamoDB();
while (!isCompleted) {
try {
const response = await dynamodb.listTables({
ExclusiveStartTableName: nextToken
}).promise();
if (response.TableNames) {
for (i = 0; i < response.TableNames.length; i++) {
const tableName = response.TableNames[i];
if (cliArgs.tablePrefix && !tableName.startsWith(cliArgs.tablePrefix)) {
console.log("Skipping table", tableName);
continue;
}
const tableBackup = await dynamodb.describeContinuousBackups({
TableName: tableName
}).promise().then((tableResponse) => {
return tableResponse.ContinuousBackupsDescription;
});
if (tableBackup.ContinuousBackupsStatus === 'ENABLED') {
console.log("Skipping table", tableName, "as backup is already enabled");
continue;
}
console.log("Enabling continuous backup for", tableName);
await dynamodb.updateContinuousBackups({
TableName: tableName,
PointInTimeRecoverySpecification: {
PointInTimeRecoveryEnabled: true
}
}).promise();
await wait(500);
}
nextToken = response.LastEvaluatedTableName;
isCompleted = !nextToken;
} else {
isCompleted = true
}
} catch (error) {
if (error.code === 'ThrottlingException') {
await wait(2000);
} else {
throw error;
}
}
}
}
enableTableBackup();