Prerequisites:
=============
Step: 1. Install Java :
# yum -y install java-1.7.0-openjdk
# export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
# java -version
Step: 2. Download & Unzip Amazon EC2 CLI Tools :
# yum -y install wget zip unzip
# cd /tmp
# wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip
# unzip ec2-api-tools.zip
Step: 3. Install the Amazon EC2 CLI Tools :
# mkdir /usr/local/ec2
# mv ec2-api-tools-1.7.5.0 /usr/local/ec2/apitools/
Step: 4. Set variables :
# export EC2_HOME=/usr/local/ec2/apitools
# export PATH=$PATH:$EC2_HOME/bin
Step: 5. Add variables to Startup Script :
# cd etc/profile.d/
# vi aws.sh
export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
export EC2_HOME=/usr/local/ec2/apitools
export PATH=$PATH:$EC2_HOME/bin
-- Save & Quit (:wq)
# chmod +x aws.sh
# sounce aws.sh
Step: 6. Logged in into AWS Web Panel.
Step: 7. Go to IAM Panel.
-- Click on Users (tab)
-- Create New Users.
-- Give User Name.
-- Click on Create.
-- Download the Credential.
-- Close.
-- Click on Newly Created User.
-- Permission (tab)
-- Click on Attach Policy.
-- Search (AmazonEC2FullAccess) & Select it.
-- Attach Policy.
Access Key ID: Provide your Access Key Id
Secret Access Key: Provide your Secret Access Key
Step: 8. Finally Create AMI Auto Backup Script :
# vi /backups/scripts/aws-ami-backup.sh
#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/ec2/apitools/bin
# Please use env | grep EC2_HOME to find out your system's setting
EC2_HOME=/usr/local/ec2/apitools
# Please use env | grep JAVA_HOME to find out your system's setting
JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
date=`date +%d-%m-%Y_%H-%M-%S`
export EC2_HOME JAVA_HOME
export AWS_ACCESS_KEY=Provide your Access Key Id
export AWS_SECRET_KEY=Provide your Secret Access Key
# Regions reference: http://docs.aws.amazon.com/general/latest/gr/rande.html
region="ap-southeast-1"
# You can find your instance ID at AWS Manage Console
instanceID="i-c706e305"
# Your prefer AMI Name prefix
amiNamePrefix="SOUMYA-SOUMYATEST-AMI_$date"
# 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
if [ $routine = true ]; then
# 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
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 > /tmp/AMIBackup.txt
# Name Tag
amiid=`cat /tmp/AMIBackup.txt | cut -f2`
ec2addtag $amiid --tag Name=$amiName --region $region
-- Save & Quit (:wq)
# chmod 755 /backups/scripts/aws-ami-backup.sh
Step: 9. Schedule in Crontab :
# crontab -e
0 0 * * * /backups/scripts/aws-ami-backup.sh
-- Save & Quit (:wq)
Step: 10. Retstart the Cron Service :
# service crond restart
Done...!!!
Please share your ideas and opinions about this topic.
If you like this post, then please share with others.
Please subscribe on email for every updates on mail.
=============
Step: 1. Install Java :
# yum -y install java-1.7.0-openjdk
# export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
# java -version
Step: 2. Download & Unzip Amazon EC2 CLI Tools :
# yum -y install wget zip unzip
# cd /tmp
# wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip
# unzip ec2-api-tools.zip
Step: 3. Install the Amazon EC2 CLI Tools :
# mkdir /usr/local/ec2
# mv ec2-api-tools-1.7.5.0 /usr/local/ec2/apitools/
Step: 4. Set variables :
# export EC2_HOME=/usr/local/ec2/apitools
# export PATH=$PATH:$EC2_HOME/bin
Step: 5. Add variables to Startup Script :
# cd etc/profile.d/
# vi aws.sh
export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
export EC2_HOME=/usr/local/ec2/apitools
export PATH=$PATH:$EC2_HOME/bin
-- Save & Quit (:wq)
# chmod +x aws.sh
# sounce aws.sh
Step: 6. Logged in into AWS Web Panel.
Step: 7. Go to IAM Panel.
-- Click on Users (tab)
-- Create New Users.
-- Give User Name.
-- Click on Create.
-- Download the Credential.
-- Close.
-- Click on Newly Created User.
-- Permission (tab)
-- Click on Attach Policy.
-- Search (AmazonEC2FullAccess) & Select it.
-- Attach Policy.
Access Key ID: Provide your Access Key Id
Secret Access Key: Provide your Secret Access Key
Step: 8. Finally Create AMI Auto Backup Script :
# vi /backups/scripts/aws-ami-backup.sh
#!/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/ec2/apitools/bin
# Please use env | grep EC2_HOME to find out your system's setting
EC2_HOME=/usr/local/ec2/apitools
# Please use env | grep JAVA_HOME to find out your system's setting
JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
date=`date +%d-%m-%Y_%H-%M-%S`
export EC2_HOME JAVA_HOME
export AWS_ACCESS_KEY=Provide your Access Key Id
export AWS_SECRET_KEY=Provide your Secret Access Key
# Regions reference: http://docs.aws.amazon.com/general/latest/gr/rande.html
region="ap-southeast-1"
# You can find your instance ID at AWS Manage Console
instanceID="i-c706e305"
# Your prefer AMI Name prefix
amiNamePrefix="SOUMYA-SOUMYATEST-AMI_$date"
# 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
if [ $routine = true ]; then
# 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
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 > /tmp/AMIBackup.txt
# Name Tag
amiid=`cat /tmp/AMIBackup.txt | cut -f2`
ec2addtag $amiid --tag Name=$amiName --region $region
-- Save & Quit (:wq)
# chmod 755 /backups/scripts/aws-ami-backup.sh
Step: 9. Schedule in Crontab :
# crontab -e
0 0 * * * /backups/scripts/aws-ami-backup.sh
-- Save & Quit (:wq)
Step: 10. Retstart the Cron Service :
# service crond restart
Done...!!!
Please share your ideas and opinions about this topic.
If you like this post, then please share with others.
Please subscribe on email for every updates on mail.
Hey, if you are interested checkout my blog on C++
ReplyDeletehttps://bytesizeinfo.blogspot.com/?m=1