Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. _INSTANCE_ID="${YOUR_INSTANCE_ID}"
  4. _ROOT_DEVICE="/dev/sda1"
  5. _CHECK_INTERVAL=60
  6.  
  7. echo "処理を続行しますか? [Y/n]"
  8. read ANSWER
  9. case ${ANSWER} in
  10. [yY]) echo "処理を続行します..."
  11. ;;
  12. [nN]) echo "$(date '+%Y-%m-%d %H:%M:%S') 処理を終了します..."
  13. exit 0 ;;
  14. *) echo "$(date '+%Y-%m-%d %H:%M:%S') 処理を終了します..."
  15. exit 0 ;;
  16. esac
  17.  
  18. if [ -f tmp/snapshot_ids ];then
  19. rm tmp/snapshot_ids
  20. fi
  21.  
  22. for ebs in $(aws ec2 describe-instances --filters Name=instance-id,Values=${_INSTANCE_ID} --query 'Reservations[].Instances[].BlockDeviceMappings[].Ebs.VolumeId' --output text);
  23. do
  24. _SNAPSHOT_ID=$(aws ec2 create-snapshot --volume-id ${ebs} | jq -r .SnapshotId)
  25. echo ${_SNAPSHOT_ID} >> tmp/snapshot_ids
  26. _DEVICE=$(aws ec2 describe-volumes --volume-ids ${ebs} --query Volumes[].Attachments[].Device --output text)
  27. if [ "${_DEVICE}" == "${_ROOT_DEVICE}" ];then
  28. aws ec2 create-tags --resources ${_SNAPSHOT_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="OS"
  29. else
  30. aws ec2 create-tags --resources ${_SNAPSHOT_ID} --tags Key=device,Value=${_DEVICE} Key=Name,Value="DATA"
  31. fi
  32. done
  33.  
  34. if [ -f tmp/snapshot_ids -a $(wc -l tmp/snapshot_ids | awk '{print $1}') == 2 ];then
  35. echo ${_SLACK_CHANNEL} "スナップショットの作成を開始しました..."
  36. else
  37. echo ${_SLACK_CHANNEL} "スナップショットの作成に失敗しました.処理を中断します."
  38. exit 1
  39. fi
  40.  
  41. aws ec2 wait snapshot-completed --snapshot-ids $(cat tmp/snapshot_ids)
  42. if [ $? == 0 ]; then
  43. echo "スナップショットの作成が完了しました."
  44. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement