-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaws-snapshot
executable file
·225 lines (197 loc) · 6.63 KB
/
aws-snapshot
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
# src: https://github.com/nbutler19/scripts/blob/master/python/snapshot-ebs.py
import boto
import argparse
import time, datetime, sys, logging, socket, re
from dateutil.relativedelta import relativedelta
def get_creds():
# By default empty, boto will look for environment variables
# AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
ACCESSKEY=None
SECRETKEY=None
return ACCESSKEY, SECRETKEY
def get_args():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(
dest='subparser_name',
help='sub-command help'
)
parser_device = subparsers.add_parser(
'device',
help='Snapshot a specific device'
)
parser_device.add_argument(
'-d',
'--device',
dest='device',
required=True,
help='The filesystem device name, e.g /dev/sdj',
)
parser_device.add_argument(
'-i',
'--instance-id',
dest='instanceid',
required=True,
help='The AWS InstanceId, e.g i-1223456',
)
parser_instance = subparsers.add_parser(
'instance',
help=
"""
Snapshot all attached ebs volumes on a specific instance
"""
)
parser_instance.add_argument(
'-i',
'--instance-id',
dest='instanceid',
required=True,
help='The AWS InstanceId, e.g i-1223456',
)
parser_volume = subparsers.add_parser(
'volume',
help='Snapshot a specific volume',
)
parser_volume.add_argument(
'-v',
'--volumeid',
dest='volumeid',
required=True,
help='The AWS VolumeId, e.g. vol-1223456',
)
parser.add_argument(
'-r',
'--region',
dest='region',
default='ap-southeast-2',
help='The AWS Region to use, e.g. us-east-1/us-west-2',
)
parser.add_argument(
'-n',
'--name',
dest='name',
default=socket.gethostname(),
help=
"""
The name of the ebs volume or instance. Defaults to the current
host.
"""
)
parser.add_argument(
'-k',
'--keep',
dest='keep',
action='store_true',
default=False,
help="Flag the backup to be kept (Backup:Retain)"
)
parser.add_argument(
'-w',
'--wait',
dest='wait',
action='store_true',
default=False,
help='Wait for EBS backup to complete before returning'
)
parser.add_argument(
'-l',
'--log',
dest='loglevel',
default='ERROR',
choices=['CRITICAL','FATAL','ERROR','WARN','WARNING','INFO','DEBUG','NOTSET'],
help="the loglevel sets the amount of output you want",
)
return parser.parse_args()
def get_numeric_loglevel(loglevel):
return getattr(logging, loglevel.upper())
def convert_to_utf8(string):
return string.encode('utf8')
def get_conn(args):
region = get_region(args)
conn = boto.connect_ec2(args.accesskey, args.secretkey, region=region)
return conn
def get_region(args):
import boto.ec2
regions = boto.ec2.regions(aws_access_key_id=args.accesskey,aws_secret_access_key=args.secretkey)
for region in regions:
if args.region == region.name:
return region
def get_instance(conn, instanceid):
return conn.get_all_instances(instanceid)[0].instances[0]
def get_volume(conn, volumeid):
return conn.get_all_volumes(volumeid)[0]
def get_volumes_attached_to_instance(conn, instance):
volumes = []
block_devices = instance.block_device_mapping
for dev, vol in block_devices.items():
# We don't care about root ebs devices
if not dev == '/dev/sda1':
volume = get_volume(conn, convert_to_utf8(vol.volume_id))
volumes.append(volume)
return volumes
def get_meta_data(conn, volume, name):
dev = volume.attach_data.device
if dev is None:
dev = 'Unattached'
desc = "%s-snap-%s-%s" % (name, volume.id, dev)
return name, volume.id, dev, desc
def create_snapshot(conn, volume, desc):
logging.info("Creating snapshot for %s with description %s" % (repr(volume), desc))
return volume.create_snapshot(description=desc)
def create_tags(conn, snapshot, volume, name, dev, retain):
logging.info("Creating tag Backup:Device=%s" % dev)
snapshot.add_tag('Backup:Device', value=dev)
if retain:
logging.info("Creating tag Backup:Retain=%s" % retain)
snapshot.add_tag('Backup:Expires', value=retain)
for n,v in volume.tags.items():
if n.startswith('aws:'):
logging.info("Creating tag Backup:%s=%s" % (n,v))
snapshot.add_tag('Backup:'+n, value=v)
else:
logging.info("Creating tag %s=%s" % (n,v))
snapshot.add_tag(n, value=v)
def wait_for_snapshot(snapshot):
while snapshot.status != 'completed':
snapshot.update()
time.sleep(1)
def run():
(accesskey, secretkey) = get_creds()
args = get_args()
args.accesskey = accesskey
args.secretkey = secretkey
numeric_level = get_numeric_loglevel(args.loglevel)
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=numeric_level)
conn = get_conn(args)
if args.subparser_name == 'instance':
instance = get_instance(conn, args.instanceid)
volumes = get_volumes_attached_to_instance(conn, instance)
for volume in volumes:
name, id, dev, desc = get_meta_data(conn, volume, args.name)
snapshot = create_snapshot(conn, volume, desc)
logging.info("%s" % repr(snapshot))
create_tags(conn, snapshot, volume, name, dev, args.keep)
if args.wait: wait_for_snapshot(snapshot)
if args.subparser_name == 'device':
instance = get_instance(conn, args.instanceid)
try:
blk_dev = instance.block_device_mapping[args.device]
volume = get_volume(conn, convert_to_utf8(blk_dev.volume_id))
except KeyError:
logging.error("No volume attached to device %s on instance %s" % (args.device, args.instanceid))
sys.exit(1)
name, id, dev, desc = get_meta_data(conn, volume, args.name)
snapshot = create_snapshot(conn, volume, desc)
logging.info("%s" % repr(snapshot))
create_tags(conn, snapshot, volume, name, dev, args.keep)
if args.wait: wait_for_snapshot(snapshot)
if args.subparser_name == 'volume':
volume = get_volume(conn, args.volumeid)
name, id, dev, desc = get_meta_data(conn, volume, args.name)
snapshot = create_snapshot(conn, volume, desc)
logging.info("%s" % repr(snapshot))
create_tags(conn, snapshot, volume, name, dev, args.keep)
if args.wait: wait_for_snapshot(snapshot)
sys.exit()
if __name__ == '__main__':
run()