-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomated_redshift_snapshots.js
92 lines (80 loc) · 3.08 KB
/
automated_redshift_snapshots.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
#!/usr/bin/env node
var aws = require('aws-sdk');
var _ = require('underscore');
var moment = require('moment');
var winston = require('winston');
winston.remove(winston.transports.Console);
winston.add(winston.transports.File, { filename: '/var/tmp/automated_redshift_snapshots.log' });
aws.config.update({
region: 'us-east-1',
//logger: process.stdout
})
var now = moment().format("YYYY-MM-DD-HH");
winston.info('START')
var redshift = new aws.Redshift();
var redshift_clusters = [
{
name: 'slice-dwh-poc',
frequency: 'daily',
keep_days: '14'
}
]
function delete_snapshots(snapshots) {
_.each(snapshots.Snapshots, function (snapshot) {
var params = { SnapshotIdentifier: snapshot.SnapshotIdentifier, SnapshotClusterIdentifier: snapshot.ClusterIdentifier };
redshift.deleteClusterSnapshot(params, function(err, data) {
if (err) winston.info(err, err.stack);
else { winston.info('Deleting expired snapshot: ', snapshot.SnapshotIdentifier) }
});
})
}
function delete_expired_snapshots(cluster) {
var expire_time = moment().subtract(cluster.keep_days, 'days').toISOString()
var params = { ClusterIdentifier: cluster.name, EndTime: expire_time, SnapshotType: 'manual' }; // config to find expired snapshots
redshift.describeClusterSnapshots(
params,
function(err, snapshots) {
if (err) winston.info(err, err.stack);
else delete_snapshots(snapshots)
}
);
}
function create_snapshot(cluster) {
var params = { ClusterIdentifier: cluster.name, SnapshotIdentifier: cluster.name + '-' + moment().format("YYYY-MM-DD-HH") };
redshift.createClusterSnapshot(params, function(err, data) {
if (err) winston.info(err, err.stack);
else { winston.info(data.Snapshot.SnapshotIdentifier); }
});
}
function create_daily_snapshot(cluster) {
// find snapshots less than 1 day old, and if none exist, create new, daily snapshot
creation_date = moment().subtract(1, 'days').toISOString()
var params = { ClusterIdentifier: cluster.name, StartTime: creation_date, SnapshotType: 'manual' };
redshift.describeClusterSnapshots(
params,
function(err, snapshots) {
if (err) winston.info(err, err.stack);
else {
// no daily backup, so make one
if ( snapshots.Snapshots.length == 0 ) {
winston.info('No daily backup')
create_snapshot(cluster)
// daily backup exists, so list
} else {
winston.info('Found daily backup')
_.each(snapshots.Snapshots, function (snapshot) {
winston.info('id: ' + snapshot.SnapshotIdentifier + ', created: ' + moment(snapshot.SnapshotCreateTime).format('YYYY-MM-DD HH:mm'))
})
}
}
}
);
}
/* find and delete expired snapshots */
_.each(
redshift_clusters,
function (cluster) {
delete_expired_snapshots(cluster)
create_daily_snapshot(cluster)
}
)