Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 3635d26

Browse files
author
Chris Machler
committed
Adding functionality to skip volumes from being backed up. #8
1 parent fecb4a2 commit 3635d26

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Type "help", "copyright", "credits" or "license" for more information.
8686
```
8787
**We will copy the encoded value and add it as the Lambda environment variable "aws_regions". When copying the encoded value please omit the single quotes in the output (ie. dXMtd2VzdC0yLHVzLWVhc3QtMg==).**
8888

89-
## Add the SNS Topics ARN you want publish as a Lambda environmenet variable "aws_sns_arn"
89+
## Add the SNS Topics ARN you want publish as a Lambda environment variable "aws_sns_arn"
9090

9191
This is optional environment variable if you want publish any topic, so you might receive email notification
9292
once backing up was executed.
@@ -95,6 +95,16 @@ once backing up was executed.
9595

9696
Create two functions in Lambda using the Python 2.7 runtime, one for the backup script and one for the cleanup script. I recommend just using the 128 MB memory setting, and adjust the timeout to 10 seconds (longer in a larger environment). Set the event source to be "CloudWatch Events - Schedule" and set the Schedule expression to be a cron expression of your liking i.e. "cron(0 6 * * ? *)" if you want the job to be kicked off at 06:00 UTC, set the cleanup job to run a few minutes later.
9797

98+
## Tagging your EC2 instances to backup
99+
100+
You will need to tag your instances in order for them to be backed up, below are the tags that will be used by the Lambda function:
101+
102+
| Tag Key | Tag Value | Notes |
103+
| ------------- |:-------------: | -----:|
104+
| Backup | | Value Not Needed |
105+
| Retention | *Number of Days to Retain Snapshot* | Default is 7 Days|
106+
|Skip_Backup_Volumes|*volume id(s) in CSV string* | List either a single volume-id, or multiple volumes-ids in a Comma Separated Value String |
107+
98108
## More Info
99109

100110
[Again if you need more details on setting this up please check my blog post.] (http://www.evergreenitco.com/evergreenit-blog/2016/4/19/aws-ebs-backup-job-run-by-lambda)

lambda-ebs-backup.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import base64
55
import os
66
import json
7+
import itertools
78

89
base64_region = os.environ['aws_regions']
910
aws_sns_arn = os.getenv('aws_sns_arn', None)
@@ -61,10 +62,23 @@ def lambda_handler(event, context):
6162
except IndexError:
6263
retention_days = 7
6364

65+
try:
66+
skip_volumes = [
67+
str(t.get('Value')).split(',') for t in instance['Tags']
68+
if t['Key'] == 'Skip_Backup_Volumes']
69+
except Exception:
70+
pass
71+
72+
from itertools import chain
73+
skip_volumes_list = list(chain.from_iterable(skip_volumes))
74+
6475
for dev in instance['BlockDeviceMappings']:
6576
if dev.get('Ebs', None) is None:
6677
continue
6778
vol_id = dev['Ebs']['VolumeId']
79+
if vol_id in skip_volumes_list:
80+
print "Volume %s is set to be skipped, not backing up" % (vol_id)
81+
continue
6882
dev_attachment = dev['DeviceName']
6983
print "Found EBS volume %s on instance %s attached to %s" % (
7084
vol_id, instance['InstanceId'], dev_attachment)

0 commit comments

Comments
 (0)