-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore-volume.py
57 lines (49 loc) · 1.33 KB
/
restore-volume.py
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
import boto3
from operator import itemgetter
ec2_client = boto3.client('ec2', region_name="eu-west-3")
ec2_resource = boto3.resource('ec2', region_name="eu-west-3")
instance_id = "i-04f01be7a765eaf7e"
volumes = ec2_client.describe_volumes(
Filters=[
{
'Name': 'attachment.instance-id',
'Values': [instance_id]
}
]
)
instance_volume = volumes['Volumes'][0]
snapshots = ec2_client.describe_snapshots(
OwnerIds=['self'],
Filters=[
{
'Name': 'volume-id',
'Values': [instance_volume['VolumeId']]
}
]
)
latest_snapshot = sorted(snapshots['Snapshots'], key=itemgetter('StartTime'), reverse=True)[0]
print(latest_snapshot['StartTime'])
new_volume = ec2_client.create_volume(
SnapshotId=latest_snapshot['SnapshotId'],
AvailabilityZone="eu-west-3b",
TagSpecifications=[
{
'ResourceType': 'volume',
'Tags': [
{
'Key': 'Name',
'Value': 'prod'
}
]
}
]
)
while True:
vol = ec2_resource.Volume(new_volume['VolumeId'])
print(vol.state)
if vol.state == 'available':
ec2_resource.Instance(instance_id).attach_volume(
VolumeId=new_volume['VolumeId'],
Device='/dev/xvdb'
)
break