-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws-ec2-ami-backup-REGION-replica.sh
213 lines (160 loc) · 9.78 KB
/
aws-ec2-ami-backup-REGION-replica.sh
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
#!/bin/bash
# Author: Antonello Cordella
# Updated: May 24th, 2021
# Webiste: https://github.com/antoweb/ or https://www.sistemistaitaliano.it
# This script born from aws-ec2-ami-backup-replica.sh but you can choose source and destination region in script parameter ($3 and $4)
# This script is a fork of following Author
# Author: Pablo Suarez
# Updated: October 28th, 2017
# Website: https://github.com/pavlops
# Usage: aws-ami-backup.sh <ec2-instance-id> <identifier> <retention days> <aws local profile>
#Initialize variables
currDate=$(date +%Y%m%d%H%M)
#instanceId="$1"
#instanceName="$2"
maxret=$1
profile=$2
#name="*$instanceName auto*"
sourceregion=$3
destregion=$4
#Empty temp files
> /tmp/instancetobackup_"$profile"_"$sourceregion"
> /tmp/instancestoppedtobackup_"$profile"_"$sourceregion"
> /tmp/amicreatedsingleline_"$profile"_"$sourceregion"
> /tmp/instancesrunningbackedup_"$profile"_"$sourceregion"
> /tmp/instancesstoppedbackedup_"$profile"_"$sourceregion"
> /tmp/amicreated_"$profile"_"$sourceregion"
echo "---------------------------"
echo "INIZIO BACKUP PER $profile"
echo "---------------------------"
#List all running instances for retriving Instance Name
echo "List all running instances for retriving Instance Name for $profile"
aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query "Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key=='Name']|[0].Value}" --output text --profile "$profile" --region "$sourceregion" > /tmp/instancesrunningbackedup_"$profile"_"$sourceregion"
cat /tmp/instancesrunningbackedup_"$profile"_"$sourceregion"
#List all existing image for running instances in source region
while IFS=$'\t' read -r -a myArray
do
echo "List all existing image for running instances in source region for $profile"
result=$(aws ec2 describe-images --filters "Name=name,Values=*${myArray[1]} auto*" --query 'Images[*].{CreationDate:CreationDate,ImageId:ImageId}' --output text --profile "$profile" --region "$sourceregion" | sort -r)
echo $result
#deleting old images of running instances in source region
while read line; do
echo "deleting old images of running instances in source region for $profile"
let i++
if [ "$i" -gt "$maxret" ]; then
#Get snapshots from AMIs
amiID=$(echo $line | awk -F ' ' '{print $2}')
snapshots=$(aws ec2 describe-images --image-ids "$amiID" --query 'Images[0].BlockDeviceMappings[*].Ebs.{SnapshotId:SnapshotId}' --output text --profile "$profile" --region "$sourceregion")
aws ec2 deregister-image --image-id "$amiID" --profile "$profile" --region "$sourceregion"
echo "$amiID deleted."
while read snapshotId; do
aws ec2 delete-snapshot --snapshot-id "$snapshotId" --profile "$profile" --region "$sourceregion"
echo "$snapshotId deleted."
done <<< "$snapshots"
fi
done <<< "$result"
unset i
done < /tmp/instancesrunningbackedup_"$profile"_"$sourceregion"
#List all existing image for running instances in destination region
while IFS=$'\t' read -r -a myArrayd
do
echo "List all existing image for running instances in destination region for $profile"
resultd=$(aws ec2 describe-images --filters "Name=name,Values=*${myArrayd[1]} auto*" --query 'Images[*].{CreationDate:CreationDate,ImageId:ImageId}' --output text --profile "$profile" --region "$destregion" | sort -r)
echo $resultd
#deleting old images of running instances in destination region
while read line; do
echo "deleting old images of running instances in destination region for $profile"
let id++
if [ "$id" -gt "$maxret" ]; then
#Get snapshots from AMIs
amiID=$(echo $line | awk -F ' ' '{print $2}')
snapshots=$(aws ec2 describe-images --image-ids "$amiID" --query 'Images[0].BlockDeviceMappings[*].Ebs.{SnapshotId:SnapshotId}' --output text --profile "$profile" --region "$destregion")
aws ec2 deregister-image --image-id "$amiID" --profile "$profile" --region "$destregion"
echo "$amiID deleted."
while read snapshotId; do
aws ec2 delete-snapshot --snapshot-id "$snapshotId" --profile "$profile" --region "$destregion"
echo "$snapshotId deleted."
done <<< "$snapshots"
fi
done <<< "$resultd"
unset id
done < /tmp/instancesrunningbackedup_"$profile"_"$sourceregion"
#List all stopped instances for retriving Instance Name
echo "List all stopped instances for retriving Instance Name for $profile"
aws ec2 describe-instances --filters "Name=instance-state-name,Values=stopped" "Name=tag:backup_if_stopped,Values=True,true" --query "Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key=='Name']|[0].Value}" --output text --profile "$profile" --region "$sourceregion" > /tmp/instancesstoppedbackedup_"$profile"_"$sourceregion"
#List all existing image for stopped instances in source region
while IFS=$'\t' read -r -a myArray1
do
echo "List all existing image for stopped instances in source region"
result1=$(aws ec2 describe-images --filters "Name=name,Values=*${myArray1[1]} stopped auto*" --query 'Images[*].{CreationDate:CreationDate,ImageId:ImageId}' --output text --profile "$profile" --region "$sourceregion" | sort -r)
echo $result1
#deleting old images of stopped instances in source region
while read line; do
echo "deleting old images of stopped instances in source region for $profile"
let i1++
if [ "$i1" -gt "$maxret" ]; then
#Get snapshots from AMIs
amiID=$(echo $line | awk -F ' ' '{print $2}')
snapshots=$(aws ec2 describe-images --image-ids "$amiID" --query 'Images[0].BlockDeviceMappings[*].Ebs.{SnapshotId:SnapshotId}' --output text --profile "$profile" --region "$sourceregion")
aws ec2 deregister-image --image-id "$amiID" --profile "$profile" --region "$sourceregion"
echo "$amiID deleted."
while read snapshotId; do
aws ec2 delete-snapshot --snapshot-id "$snapshotId" --profile "$profile" --region "$sourceregion"
echo "$snapshotId deleted."
done <<< "$snapshots"
fi
done <<< "$result1"
unset i1
done < /tmp/instancesstoppedbackedup_"$profile"_"$sourceregion"
#List all existing image for stopped instances in destination region
while IFS=$'\t' read -r -a myArray1d
do
echo "List all existing image for stopped instances in destination region for $profile"
result1d=$(aws ec2 describe-images --filters "Name=name,Values=*${myArray1d[1]} stopped auto*" --query 'Images[*].{CreationDate:CreationDate,ImageId:ImageId}' --output text --profile "$profile" --region "$destregion" | sort -r)
echo $result1d
#deleting old images of stopped instances in destination region
while read line; do
echo "deleting old images of stopped instances in destination region for $profile"
let i1d++
if [ "$i1d" -gt "$maxret" ]; then
#Get snapshots from AMIs
amiID=$(echo $line | awk -F ' ' '{print $2}')
snapshots=$(aws ec2 describe-images --image-ids "$amiID" --query 'Images[0].BlockDeviceMappings[*].Ebs.{SnapshotId:SnapshotId}' --output text --profile "$profile" --region "$destregion")
aws ec2 deregister-image --image-id "$amiID" --profile "$profile" --region "$destregion"
echo "$amiID deleted."
while read snapshotId; do
aws ec2 delete-snapshot --snapshot-id "$snapshotId" --profile "$profile" --region "$destregion"
echo "$snapshotId deleted."
done <<< "$snapshots"
fi
done <<< "$result1d"
unset i1d
done < /tmp/instancesstoppedbackedup_"$profile"_"$sourceregion"
#Create new AMI from the instance running
#running=$(aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query "Reservations[*].Instances[*].InstanceId" --output text --profile "$profile")
echo "Create new AMI from the instance running for $profile"
aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query "Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key=='Name']|[0].Value}" --output text --profile "$profile" --region "$sourceregion" > /tmp/instancetobackup_"$profile"_"$sourceregion"
cat /tmp/instancetobackup_"$profile"_"$sourceregion"
while IFS=$'\t' read -r -a myArray
do
aws ec2 create-image --instance-id ${myArray[0]} --name "$currDate ${myArray[1]} auto" --no-reboot --profile "$profile" --region "$sourceregion" >> /tmp/amicreated_"$profile"_"$sourceregion" 2>&1
done < /tmp/instancetobackup_"$profile"_"$sourceregion"
#Create new AMI for instances stopped and with tag backup_if_stopped=True
echo "Create new AMI for instances stopped and with tag backup_if_stopped=True for $profile"
aws ec2 describe-instances --filters "Name=instance-state-name,Values=stopped" "Name=tag:backup_if_stopped,Values=True,true" --query "Reservations[*].Instances[*].{Instance:InstanceId,Name:Tags[?Key=='Name']|[0].Value}" --output text --profile "$profile" --region "$sourceregion" > /tmp/instancestoppedtobackup_"$profile"_"$sourceregion"
while IFS=$'\t' read -r -a myArray
do
aws ec2 create-image --instance-id ${myArray[0]} --name "$currDate ${myArray[1]} stopped auto" --no-reboot --profile "$profile" --region "$sourceregion" >> /tmp/amicreated_"$profile"_"$sourceregion" 2>&1
done < /tmp/instancestoppedtobackup_"$profile"_"$sourceregion"
cat /tmp/instancestoppedtobackup_"$profile"_"$sourceregion"
echo "Ho creato le seguenti AMI di istanze running e stopped con tag"
cat /tmp/amicreated_"$profile"_"$sourceregion"
#Copy ami to another region
echo "Copy ami to another region"
jq -r '.ImageId' /tmp/amicreated_"$profile"_"$sourceregion" > /tmp/amicreatedsingleline_"$profile"_"$sourceregion"
while read p; do
imagename=$(aws ec2 describe-images --image-ids $p --query 'Images[*].[Name]' --output text --profile "$profile" --region "$sourceregion")
aws ec2 copy-image --source-image-id "$p" --source-region "$sourceregion" --region "$destregion" --name "$imagename" --profile "$profile"
done < /tmp/amicreatedsingleline_"$profile"_"$sourceregion"
echo "Ho copiato a Stoccolma le seguenti AMI"
cat /tmp/amicreatedsingleline_"$profile"_"$sourceregion"