如何在特定时间自动进行所有工作EC2服务器备份?
自动删除旧备份并每天进行新备份
答案 0 :(得分:1)
您可以使用CloudWatch Events创建自动EC2备份。
您将在AWS控制台中定义调度程序在CloudWatch中运行的速率。
(来自AWS)
创建一个按时间表拍摄快照的规则。您可以使用比率表达式或cron表达式来指定计划。有关更多信息,请参见规则的计划表达式。
创建规则
在创建EC2实例的自动备份中,我已经成功地使用了这一精确过程。
答案 1 :(得分:0)
AWS Lambda自动AMI备份脚本,其中CloudWatch日志跨所有区域
2用于创建和删除的lambda函数,您必须为其制定新的策略和角色
creat_backup
var aws = require('aws-sdk');
Region = ['ap-south-1','eu-central-1','us-east-1'];
var now = new Date();
date = now.toISOString().substring(0, 10)
hours = now.getHours()
minutes = now.getMinutes()
exports.handler = function (event, context)
{
var instanceparams = {
Filters: [{
Name: 'tag:Backup',
Values: [
'yes'
]
}]
}
region(0);
async function region(h){
if(h>=Region.length)
return;
console.log("H Value Test",h);
aws.config.region = Region[h];
var ec2 = new aws.EC2();
console.log("SELECTED REGION",Region[h])
return await ec2.describeInstances(instanceparams, function(err, data) {
if (err) console.log(err, err.stack);
else {
for (var i in data.Reservations) {
var ec1 = new aws.EC2();
for (var j in data.Reservations[i].Instances) {
console.log("instance is ",data.Reservations[i].Instances[j].InstanceId);
instanceid = data.Reservations[i].Instances[j].InstanceId;
nametag = data.Reservations[i].Instances[j].Tags
for (var k in data.Reservations[i].Instances[j].Tags) {
if (data.Reservations[i].Instances[j].Tags[k].Key == 'Name') {
name = data.Reservations[i].Instances[j].Tags[k].Value;
}
}
console.log("Creating AMIs of the Instance: ", name);
var imageparams = {
InstanceId: instanceid,
Name: name + "_" + date + "_" + hours + "-" + minutes,
NoReboot: true
}
ec1.createImage(imageparams, function(err, data) {
if (err) console.log(err, err.stack);
else {
image = data.ImageId;
console.log("image",image);
var tagparams = {
Resources: [image],
Tags: [{
Key: 'DeleteOn',
Value: 'yes'
}]
};
ec1.createTags(tagparams, function(err, data) {
console.log("Tags added to the created AMIs");
});
}ec1=null;
});
}
}
}
aws.config.region = null;
ec2 = null;
h+=1
region(h)
});
}
}
删除功能
var aws = require('aws-sdk');
Region = ['ap-south-1','eu-central-1','us-east-1'];
var d = new Date();
var x = 1; /* ------Retention Days------- */
d.setDate(d.getDate() - x);
reqdate = d.toISOString().substring(0, 10);
exports.handler = function(event, context) {
var instanceparams = {
Owners: [
'self'
],
Filters: [{
Name: 'tag:DeleteOn',
Values: [
'yes'
]
}]
}
region(0);
function region(h){
if(h>=Region.length)
return;
console.log("H Value Test",h);
aws.config.region = Region[h];
var ec2 = new aws.EC2();
console.log("SELECTED REGION",Region[h]);
ec2.describeImages(instanceparams, function(err, data) {
if (err) console.log(err, err.stack);
else {
for (var j in data.Images) {
imagename = data.Images[j].Name
imageid = data.Images[j].ImageId
//if (imagename.indexOf(reqdate) > -1) {
console.log("image that is going to be deregistered: ", imagename);
console.log("image id: ", imageid);
var deregisterparams = {
ImageId: imageid
};
ec2.deregisterImage(deregisterparams, function(err, data01) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log("Image Deregistered");
}
});
//}
}
setTimeout(function() {
for (var j in data.Images) {
imagename = data.Images[j].Name
// if (imagename.indexOf(reqdate) > -1) {
for (var k in data.Images[j].BlockDeviceMappings) {
snap = data.Images[j].BlockDeviceMappings[k].Ebs.SnapshotId;
console.log(snap);
var snapparams = {
SnapshotId: snap
};
ec2.deleteSnapshot(snapparams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log("Snapshot Deleted"); // successful response
});
}
//}
}
}, 30000);
}
aws.config.region = null;
h+=1
region(h);
});
}
}
有关更多信息,请访问 https://github.com/harsh4870/AWS-auto-ami-backup-across-all-region