#!/bin/bash

##########################################
# Amazon RDS instance restore script
##########################################

logfile="/path/to/logfile"
region="ap-northeast-1"

# rds instance name
dbinstancename=$1

# db snapshot name
snapshotname=$2


# new RDS instance name check.
#指定したのと同名のインスタンスが存在しないこと
rdsnamecheck=`aws rds describe-db-instances \
    • db-instance-identifier $dbinstancename \
    • query DBInstances[].[DBInstanceIdentifier] \
    • output text`

   echo "Error. same instance name is exist."  | tee -a $logfile
   exit 1
  else
   echo "OK. the new instance name is $dbinstancename."  | tee -a $logfile
fi

# リストア実行
# サブネットID, オプショングループなども可変にしたければ適宜対応。
aws rds restore-db-instance-from-db-snapshot \
   --db-instance-identifier $dbinstancename \
   --db-snapshot-identifier $snapshotname \
   --db-instance-class db.m3.xlarge \
   --db-subnet-group-name mysubnetid \
   --option-group-name optiongroupname \
   --storage-type gp2 \
   --multi-az \
   --no-auto-minor-version-upgrade \
   --no-publicly-accessible \
   --region $region


# 終了ステータスチェック
ret=$?
if [ $ret -eq 0 ]; then
     echo "OK. rthe command is success."  | tee -a $logfile
    else
     echo "Error. the command is failed."  | tee -a $logfile
     exit 1
fi

# インスタンスの状態チェック開始。ここでは30分間チェックする。
count=0

while $count -lt 30
do
dbstat=`aws rds describe-db-instances \
 --db-instance-identifier dbinstancename \
 --query DBInstances[].[DBInstanceStatus] \
 --region $region \
 --output text`

  if [[ $dbstat = available ]]; then
     echo "Restore is done." | tee -a $logfile
     break
    else
     echo "Checking..." | tee -a $logfile
     sleep 60
     count=`expr $count + 1`
  fi
done

if [ $rdsstate != available ]; then
  echo "Error. restore is failed."  | tee -a $logfile
  exit 1
fi

#セキュリティグループとDBパラメータグループの変更を実施
dbstat=`aws rds describe-db-instances \
--db-instance-identifier $dbinstancename \
--query DBInstances[].[DBInstanceStatus] \
--region $region \
--output text`

  echo "Bad status." | tee -a $logfile
  exit 1
else
  echo "Modify DB instance."
  aws rds modify-db-instance --db-instance-identifier $dbinstancename \
    --vpc-security-group-ids sg-xxxxxxxx \
    --db-parameter-group-name dbinstancenameparametergroup \
    --region $region \
    --apply-immediately
fi

ret=$?
if [ $ret -eq 0 ]; then
   echo "OK. the modify is success."  | tee -a $logfile
  else
   echo "Error. modify is failed."  | tee -a $logfile
   exit 1
fi

#変更を反映させるためRDSインスタンスリブート実施
aws rds reboot-db-instance \
    • db-instance-identifier $dbinstancename \
    • region $region

#リブート後のRDSインスタンス稼動状態チェックを最長5分まで行う

rebootcheck=0
while [ $rebootcheck -lt 20 ]
do
  rdsstate=`aws rds describe-db-instances \
  --db-instance-identifier $dbinstancename \
  --query DBInstances[].[DBInstanceStatus] \
  --output text`
  if [ $rdsstate = available ]; then
     echo "OK. booting rds is success. ($rdsstate)" | tee -a $logfile
     break
    else
      echo "checking the status...($rdsstate)" | tee -a $logfile
      sleep 15
      rebootcheck=`expr $rebootcheck + 1`
  fi
done

# ここでは5分後のステータスがavailableでなければ失敗とみなす。この辺は適宜調整。
if [ $rdsstate != available ]; then
  echo "Error. rds boot is failed." | tee -a $logfile
  exit 1
fi

タグ:

rds aws
最終更新:2021年07月29日 10:47