Created
March 31, 2014 04:39
-
-
Save arukoh/9885421 to your computer and use it in GitHub Desktop.
EC2インスタンスのスナップショット作成スクリプト。
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/sh | |
[ -z "$JAVA_HOME" ] && export JAVA_HOME=/usr/lib/jvm/jre | |
[ -z "$EC2_HOME" ] && export EC2_HOME=/opt/aws/apitools/ec2 | |
[ -z "$AWS_PATH" ] && export AWS_PATH=/opt/aws && export PATH="${PATH}:${AWS_PATH}/bin" | |
AWS_REGION=`curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed -e 's/[a-z]$//'` | |
INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id` | |
KERNEL_ID=`curl -s http://169.254.169.254/latest/meta-data/kernel-id` | |
OPTS="--aws-access-key ${AWS_ACCESS_KEY_ID} --aws-secret-key ${AWS_SECRET_ACCESS_KEY} --region ${AWS_REGION}" | |
INSTANCE_NAME=`ec2-describe-instances ${OPTS} | grep ${INSTANCE_ID} | grep TAG | grep Name | cut -f5` | |
EBS_VOLUME_ID=`ec2-describe-volumes ${OPTS} | grep ${INSTANCE_ID} | cut -f2` | |
SNAPSHOT_DESCRIPTION="Daily Backup for ${INSTANCE_NAME}(${KERNEL_ID})" | |
SNAPSHOT_GENERATION=7 | |
ec2-create-snapshot ${OPTS} --description "${SNAPSHOT_DESCRIPTION}" ${EBS_VOLUME_ID} | |
SNAPSHOTS=`ec2-describe-snapshots ${OPTS} | grep ${EBS_VOLUME_ID} | grep "${SNAPSHOT_DESCRIPTION}" | sort -k5 -r | awk '{print $2}'` | |
COUNT=1 | |
for SNAPSHOT in ${SNAPSHOTS}; do | |
if [ ${COUNT} -gt ${SNAPSHOT_GENERATION} ]; then | |
echo "deleting ${SNAPSHOT}"; | |
ec2-delete-snapshot ${OPTS} ${SNAPSHOT} | |
fi | |
COUNT=`expr ${COUNT} + 1` | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment