Created
November 4, 2016 05:51
-
-
Save brendanmckenzie/610707a0cb6fc418797c16ea0b4a4b46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AWS = require('aws-sdk'); | |
AWS.config.region = 'ap-southeast-2'; | |
exports.handler = function () { | |
var ec2 = new AWS.EC2(); | |
var date = new Date().toISOString().substr(0, 10); | |
ec2.describeSnapshots({ Filters: [ { Name: 'tag-key', Values: [ 'Expires' ] }, { Name: 'tag-value', Values: [ date ] } ] }, function (err, data) { | |
if (err) { | |
console.log(err, err.stack); | |
} | |
else { | |
data.Snapshots.forEach(function (snap) { | |
ec2.deleteSnapshot({ SnapshotId: snap.SnapshotId }, function (err, data) { | |
if (err) { | |
console.log('Failed to delete snapshot: ' + snap.SnapshotId, err, err.stack); | |
} | |
else { | |
console.log('Snapshot deleted: ' + snap.SnapshotId); | |
} | |
}); | |
}); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AWS = require('aws-sdk'); | |
AWS.config.region = 'ap-southeast-2'; | |
exports.handler = function () { | |
var ec2 = new AWS.EC2(); | |
var backupSets = ['daily']; | |
if (new Date().getDay() === 1) { | |
// Monday: run weekly backups | |
console.log('Running daily and weekly snapshots'); | |
backupSets.push('weekly'); | |
} | |
else { | |
console.log('Running daily snapshots'); | |
} | |
ec2.describeVolumes({ Filters: [ { Name: 'tag-key', Values: [ 'Backup' ] }, { Name: 'tag-value', Values: backupSets } ] }, function (err, data) { | |
if (err) { | |
console.log(err, err.stack); | |
} | |
else { | |
var volumes = data.Volumes.map(function (ent) { | |
var backupType = ent.Tags.filter(function (tag) { return tag.Key === 'Backup' })[0].Value; | |
var expires = new Date(new Date().setDate(new Date().getDate() + (backupType === 'daily' ? 3 : 14))); | |
return { | |
volume: ent.VolumeId, | |
type: backupType, | |
expires: expires.toISOString().substr(0, 10) | |
}; | |
}); | |
volumes.forEach(function (vol) { | |
console.log('Snapshotting ' + vol.volume + ' expiring: ' + vol.expires); | |
var params = { | |
VolumeId: vol.volume, | |
Description: 'Automated ' + vol.type + ' snapshot' | |
}; | |
ec2.createSnapshot(params, function (err, data) { | |
if (err) { | |
console.log('Snapshot failed for ' + vol.volume, err, err.stack); | |
} | |
else { | |
var snapshot = data.SnapshotId; | |
console.log('Snapshot ' + snapshot + ' created for volume ' + vol.volume); | |
ec2.createTags({ Resources: [ snapshot ], Tags: [ { Key: 'Expires', Value: vol.expires } ] }, function (err, data) { | |
if (err) { | |
console.log('Adding tags failed for ' + vol.volume, err, err.stack); | |
} | |
else { | |
console.log('Snapshot ' + snapshot + ' successfully tagged'); | |
} | |
}); | |
} | |
}); | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment