-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
133 lines (105 loc) · 3.77 KB
/
index.js
File metadata and controls
133 lines (105 loc) · 3.77 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
const RETENCAO_DIAS = parseInt(process.env.RETENCAO_DIAS);
var AWS = require('aws-sdk');
var ec2 = new AWS.EC2();
var erros = [];
exports.handler = (event, context, callback) => {
backupVolumes(function(){
removerVencidos(function(){
if(erros.length == 0){
callback(null, 'Fim');
}else{
callback(erros);
}
});
});
};
function backupVolumes(callback){
//pega a lista de volumes para fazer backup
var describeVolumesPromise = ec2.describeVolumes().promise();
describeVolumesPromise.then( function (data) {
if (data.Volumes.length > 0) {
var snapshotsFeitos = [];
data.Volumes.forEach(function (elem) {
//pega o nome do volume
var tagName = elem.Tags.reduce(function (prev, elem) {
if (elem.Key == 'Name') {
return elem.Value;
} else {
return prev;
}
}, '');
//prepara as tags do snapshot, coloca o mesmo nome do volume e a marcação para remover após o período de retenção
var TagsSnapshot = [
{ Key: 'Name', Value: tagName },
{ Key: 'Remover', Value: 'Sim' }
];
var params = {
VolumeId: elem.VolumeId,
Description: 'Bakcup automatico',
DryRun: false
};
//faz o backup
snapshotsFeitos.push(
ec2.createSnapshot(params).promise().then(function(data) {
var params = {
Resources: [data.SnapshotId],
Tags: TagsSnapshot
};
//coloca as tags
return ec2.createTags(params).promise();
})
);
Promise.all(snapshotsFeitos).then(values => {
callback();
}, reason => {
erros.push(reason);
callback();
});
});
}else{
callback();
}
},
function (err){
erros.push(err);
callback();
});
}
function removerVencidos(callback){
//query para petar os snapshots que foram criados por esse script
var params = {
Filters: [
{ Name: "tag:Remover", Values: ["Sim"]}
]
};
//pega todos os snapshots para verificar a validade deles
var describeSnapshotsPromise = ec2.describeSnapshots(params).promise();
describeSnapshotsPromise.then(
function(data) {
var snapshotsExcluidos = [];
//filtra os snapshots deixandos somente os vencidos
var arrSnapshotsExcluir = data.Snapshots.filter(function(elem){
return ( Math.round(Math.abs(Date.now() - Date.parse(elem.StartTime)) / (1000 * 60 * 60 * 24)) >= RETENCAO_DIAS );
});
//remove os vencidos
arrSnapshotsExcluir.forEach(function(elem){
var params = {
SnapshotId: elem.SnapshotId
};
snapshotsExcluidos.push(
ec2.deleteSnapshot(params).promise()
);
});
Promise.all(snapshotsExcluidos).then(values => {
callback();
}, reason => {
//erros.push(reason);
callback();
});
},
function(err){
erros.push(err);
callback();
}
);
}