현재는 EC2의 Lifecycle manager 의 등장으로 인해 오래된 snap-shot 의 삭제가 자동으로 이루어 지기 때문에 크게 필요가 없는 스크립트 이다.
물론 Lifecycle manager 에 비해 기능이 좀더 많다.
AWSCLI 를 사용하기 때문에 설치가 필요로 하다 ‘ㅅ’a
기능적으로는 아래와 같다.
1. AWS_REGION_ARR 에 선언된 리전을 타겟으로 한다.
2. RETENTION_LIMIT 에 지정된 일자(7개월) 만큼 매월 1일 데이터를 보관한다.
3. RETENTION_DAILY 에 지정된 일자(1개월) 만큼 매일 데이터를 보관한다.
4. BACKUP=ON 이 선언될 경우 작동중인 인스턴스의 모든 EBS 볼륨의 snap-shot을 생성한다.
5. SC_MODE=TEST 가 실제 작동이 아닌 어떤 작동을을 하게 될지 echo로 출력한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
#!/bin/bash ############################################################################################## ### EDITABLE variable ### https://docs.aws.amazon.com/general/latest/gr/rande.html#ec2_region AWS_REGION_ARR=( us-west-2 ap-northeast-2 ) ## Array ec2 locate region RETENTION_LIMIT="215" ## first day of months retention Limit (ex: 2018-01-01, 2018-02-01 ... ) RETENTION_DAILY="31" ## every day snap-shot retention SC_MODE="TEST" ## TEST MODE - if want OFF just Remark(#). #BACKUP="ON" ## Make snap-shot Function. - if want OFF just Remark(#). ### Using AWS Security Credentials ### #export AWS_ACCESS_KEY_ID=AAAAAAAAAAAAAAAAAAAA #export AWS_SECRET_ACCESS_KEY=BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB ### FIXED variable ### TODAY=$(date +%Y-%m-%d) AWSCLI=$(which aws) SCRIPTPATH=$(cd "$(dirname "$0")" && pwd) VLIST="$SCRIPTPATH/volume_list" SLIST="$SCRIPTPATH/snapshot_list" ### AWS - Volumes Check ### rm -f $VLIST && touch $VLIST for a in ${AWS_REGION_ARR[*]} do ## running instance check ## RUNNNING_INSTANCE=$($AWSCLI ec2 describe-instances --output text --region $a \ --query 'Reservations[*].Instances[*].[InstanceId]' \ --filters 'Name=instance-state-name,Values=running' | paste -s -d",") ## Get Attached Volumes List ## if [ ! -z "$RUNNNING_INSTANCE" ];then $AWSCLI ec2 describe-volumes --output text --region $a \ --query 'Volumes[*].[VolumeId, Attachments[0].InstanceId, AvailabilityZone, State, Tags[0].Value]' \ --filters "Name=attachment.instance-id,Values=$RUNNNING_INSTANCE" >> $VLIST fi unset RUNNNING_INSTANCE done ### stop script when Volume List are empty ### if [ $(wc -l $VLIST | awk '{print $1}') -eq 0 ];then echo "make sure ~]# aws configure" exit 1 fi ### aws snap-shot control function ### delete_aws_snapshot() { if [[ "$SC_MODE" == "TEST" ]];then echo -e "\e[31;1mDELETING $SNAPID $BDATE \e[0m" else $AWSCLI ec2 delete-snapshot --output text --region $REGION --snapshot-id $SNAPID fi } skip_aws_snapshot() { if [[ "$SC_MODE" == "TEST" ]];then echo -e "\e[32;1mKEEP $SNAPID $BDATE \e[0m" fi } ### AWS - snap-shots Check ### while read b do VOLUMEID=$(echo $b | cut -d" " -f1) REGIONx=$(echo $b | cut -d" " -f3) REGION=${REGIONx:0:$((${#REGIONx}-1))} rm -f $SLIST $AWSCLI ec2 describe-snapshots --output text --region $REGION \ --query 'Snapshots[*].[SnapshotId,VolumeId,Description,StartTime]' \ --filters "Name=status,Values=completed" "Name=volume-id,Values=$VOLUMEID" | \ grep -v "Created by CreateImage" > $SLIST ## AWS - Snap-Shots Delete ## while read c do SNAPID=$(echo $c | cut -d" " -f1) BDATE=$(echo $c | awk '{print $NF}' | cut -d"T" -f1) RETENTION=$(( $(( $(date -d "$TODAY" "+%s") - $(date -d "$BDATE" "+%s") )) / 86400 )) if [ -z "$(echo $BDATE|grep "\-01$")" ];then if [ $RETENTION -gt $RETENTION_DAILY ];then delete_aws_snapshot; else skip_aws_snapshot; fi else if [ $RETENTION -gt $RETENTION_LIMIT ];then delete_aws_snapshot; else skip_aws_snapshot; fi;fi unset RETENTION BDATE SNAPID done < $SLIST rm -f $SLIST unset VOLUMEID REGION REGIONx done < $VLIST ### AWS - Make EBS Snap-Shots from Volume if backup are "ON" ### if [[ "$BACKUP" == "ON" ]];then while read d do VOLUMEID=$(echo $d | cut -d" " -f1) REGIONx=$(echo $d | cut -d" " -f3) REGION=${REGIONx:0:$((${#REGIONx}-1))} VOLUMENAME=$(echo $d | cut -d" " -f5) if [ -z "$VOLUMENAME" ];then DESCRIPTION="$(date +"%Y-%m-%d_%H:%M:%S")_$VOLUMEID" else DESCRIPTION="$(date +"%Y-%m-%d_%H:%M:%S")_$VOLUMENAME" fi if [[ "$SC_MODE" == "TEST" ]];then echo -e "\e[32;1mMAKE SNAP-SHOT $VOLUMEID\e[0m" else $AWSCLI ec2 create-snapshot --region $REGION --volume-id $VOLUMEID --description "$DESCRIPTION" > /dev/null fi unset DESCRIPTION VOLUMENAME REGION REGIONx VOLUMEID done < $VLIST fi rm -f $VLIST unset skip_aws_snapshot delete_aws_snapshot unset SC_MODE AWS_REGION_ARR RETENTION_LIMIT RETENTION_DAILY TODAY SCRIPTPATH VLIST SLIST a b c exit 0 |