Skip to content

Instantly share code, notes, and snippets.

@inokappa
Created January 22, 2017 01:50
Show Gist options
  • Save inokappa/a4d71df99284902385bdf518959d4dd4 to your computer and use it in GitHub Desktop.
Save inokappa/a4d71df99284902385bdf518959d4dd4 to your computer and use it in GitHub Desktop.
EBS が二つぶら下がった EC2 のスナップショットを取得するシェルスクリプトの例
#!/usr/bin/env bash
_INSTANCE_ID="${YOUR_INSTANCE_ID}"
_ROOT_DEVICE="/dev/sda1"
_CHECK_INTERVAL=60
echo "処理を続行しますか? [Y/n]"
read ANSWER
case ${ANSWER} in
[yY]) echo "処理を続行します..."
;;
[nN]) echo "$(date '+%Y-%m-%d %H:%M:%S') 処理を終了します..."
exit 0 ;;
*) echo "$(date '+%Y-%m-%d %H:%M:%S') 処理を終了します..."
exit 0 ;;
esac
if [ -f tmp/snapshot_ids ];then
rm tmp/snapshot_ids
fi
for ebs in $(aws ec2 describe-instances --filters Name=instance-id,Values=${_INSTANCE_ID} --query 'Reservations[].Instances[].BlockDeviceMappings[].Ebs.VolumeId' --output text);
do
_SNAPSHOT_ID=$(aws ec2 create-snapshot --volume-id ${ebs} | jq -r .SnapshotId)
echo ${_SNAPSHOT_ID} >> tmp/snapshot_ids
_DEVICE=$(aws ec2 describe-volumes --volume-ids ${ebs} --query Volumes[].Attachments[].Device --output text)
if [ "${_DEVICE}" == "${_ROOT_DEVICE}" ];then
aws ec2 create-tags --resources ${_SNAPSHOT_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="OS"
else
aws ec2 create-tags --resources ${_SNAPSHOT_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="DATA"
fi
done
if [ -f tmp/snapshot_ids -a $(wc -l tmp/snapshot_ids | awk '{print $1}') == 2 ];then
echo ${_SLACK_CHANNEL} "スナップショットの作成を開始しました..."
else
echo ${_SLACK_CHANNEL} "スナップショットの作成に失敗しました.処理を中断します."
exit 1
fi
aws ec2 wait snapshot-completed --snapshot-ids $(cat tmp/snapshot_ids)
if [ $? == 0 ]; then
echo "スナップショットの作成が完了しました."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment