Created
October 12, 2017 05:53
-
-
Save ravituvar/a86ed6814403f910af6142e3b3a385f4 to your computer and use it in GitHub Desktop.
DailyAMI Backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin | |
EC2_HOME=/opt/aws/apitools/ec2-1.7.3.0/ | |
JAVA_HOME=/usr/lib/jvm/jre | |
export EC2_HOME JAVA_HOME | |
instanceID=`curl http://169.254.169.254/latest/meta-data/instance-id` | |
echo "$instanceID" | |
region=`curl http://169.254.169.254/latest/dynamic/instance-identity/document | grep region | awk -F\" '{print $4}'` | |
echo "$region" | |
amiNamePrefix="AMI_" | |
# Your prefer AMI Description | |
amiDescription="Daily AMI backup" | |
# Setup AMI Name | |
amiName=$amiNamePrefix | |
# 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 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment