-
Notifications
You must be signed in to change notification settings - Fork 5
/
snapshots.js
executable file
·103 lines (93 loc) · 2.95 KB
/
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
93
94
95
96
97
98
99
100
101
102
103
var AWS = require('aws-sdk');
var ec2 = new AWS.EC2();
var deleteOn = new Date();
exports.handler = function(event, context) {
// create snapshots --------------------------------
var createParams = {
Filters: [
{ Name: 'tag-key', Values: ['Backup','backup'] }
]
};
ec2.describeInstances(createParams, function(err, data) {
if (err) console.log(err, err.stack);
else{
for(var r=0; r<data.Reservations.length; r++){
for(var i=0; i<data.Reservations[r].Instances.length; i++){
createSnapshots(data.Reservations[r].Instances[i]);
}
}
}
});
// delete snapshots --------------------------------
var deleteParams = {
Filters: [
{ Name: 'tag-key', Values: ['DeleteOn'] },
{ Name: 'tag-value', Values: [formatDate(deleteOn)] },
]
};
ec2.describeSnapshots(deleteParams, function(err, data) {
if (err) console.log(err, err.stack);
else{
for(var s=0; s<data.Snapshots.length; s++){
deleteSnapshot(data.Snapshots[s].SnapshotId);
}
}
});
};
function createSnapshots(instance){
var volumes = getInstanceVolumes(instance);
var retention = getTag(instance.Tags,'Retention');
for(var v=0; v<volumes.length; v++)
{
console.log('Creating snapshot for volumeId '+volumes[v]+' on instance name '+getTag(instance.Tags,'Name'));
var params = {
VolumeId: volumes[v],
Description: getTag(instance.Tags,'Name')+' node backup',
DryRun: false
};
ec2.createSnapshot(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else{
var date = new Date();
date.setDate(date.getDate() + parseInt(retention));
tagSnapshot(data.SnapshotId,formatDate(date));
}
});
}
}
function deleteSnapshot(snapshotId){
var params = {SnapshotId: snapshotId};
ec2.deleteSnapshot(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log('Deleted snapshot id '+snapshotId);
});
}
function tagSnapshot(snapshotId,deleteOn){
var params = {
Resources: [ snapshotId ],
Tags: [{
Key: 'DeleteOn',
Value: deleteOn
}]
};
ec2.createTags(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log('Add DeleteOn tag '+deleteOn+' to snapshot id: '+snapshotId );
});
}
function getInstanceVolumes(instance){
var volumes = [];
for(var i = 0; i < instance.BlockDeviceMappings.length; i++){
volumes.push(instance.BlockDeviceMappings[i].Ebs.VolumeId);
}
return volumes;
}
function getTag(tags,key){
for(var t=0;t<tags.length;t++){
if(tags[t].Key == key) return tags[t].Value;
}
return false
}
function formatDate(date){
return date.getFullYear() + '' + ('0' + (date.getMonth()+1)).slice(-2) + '' + ('0' + date.getDate()).slice(-2);
}