forked from henrychen95/AWS-AMI-Auto-Backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-ami-auto-backup.sh
69 lines (52 loc) · 1.73 KB
/
aws-ami-auto-backup.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
#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin
# Please use env | grep EC2_HOME to find out your system's setting
EC2_HOME=/opt/aws/apitools/ec2
# Please use env | grep JAVA_HOME to find out your system's setting
JAVA_HOME=/usr/lib/jvm/jre
export EC2_HOME JAVA_HOME
# Regions reference: http://docs.aws.amazon.com/general/latest/gr/rande.html
region="ap-northeast-1"
# You can find your instance ID at AWS Manage Console
instanceID="YOUR-INSTANCE-ID"
# Your prefer AMI Name prefix
amiNamePrefix="AMI_"
# Your prefer AMI Description
amiDescription="Daily AMI backup"
# If you want to keep 7 days AMI backups, please set routine true otherwise set it false
routine=true
# Variable for routine is true
weekday=$(date +%a)
if [ $routine = true ]; then
# Setup AMI Name
amiName=$amiNamePrefix$weekday
# Get AMI ID
amiIDs=$(ec2-describe-images --region $region | grep 'ami-[a-z0-9]' | grep "$amiName" |cut -f 2)
# Get Snapshot ID
if [[ ! -z $amiIDs ]]; then
snapshotIDs=$(ec2-describe-snapshots --region $region | grep $amiIDs | cut -f 2)
fi
else
# Setup AMI Name
amiName=$amiNamePrefix
# Get AMI ID
amiIDs=$(ec2-describe-images --region $region | grep 'ami-[a-z0-9]' | cut -f 2)
# Get Snapshot ID
if [[ ! -z $amiIDs ]]; then
snapshotIDs=$(ec2-describe-snapshots --region $region | cut -f 2)
fi
fi
if [[ ! -z $amiIDs ]]; then
# Deregister AMI
for amiID in $amiIDs
do
ec2-deregister --region $region $amiID
done
# Delete snapshot
for snapshotID in $snapshotIDs
do
ec2-delete-snapshot --region $region $snapshotID
done
fi
# Create AMI
ec2-create-image $instanceID --region $region --name "$amiName" -d "$amiDescription" --no-reboot