본문 바로가기
기술스택을 쌓아보자/AWS

[RDS] aws cli로 스냅샷으로 똑같은 DB를 다른 VPC에 만들기 (production db와 staging db 값 맞추기)

by 소리331 2023. 9. 1.
반응형

DB 스냅샷 복원의 특징

- 사실은 스냅샷을 바탕으로 복구하는 것이 아니라, 스냅샷을 바탕으로한 DB를 새로 똑같이 만드는 것이다.

(그래서 실제로 restore 명령어를 쓰다보면, 이미 있는 db는 이름을 사용할 수 없게 나옴!)

 

요구사항

  • 실시간으로 prod와 stg의 값이 동일할 필요는 없음
    • DMS 등의 서비스를 사용할 필요가 없음.

 

최종 cmd

# 1 기존 db 삭제
/usr/bin/aws rds delete-db-instance --db-instance-identifier {{staging db}} --skip-final-snapshot

# 2 db 삭제 상태 확인 => 그냥 gateway로 15분 대기 
aws rds describe-db-instances --db-instance-identifier {{staging db}} --query "DBInstances[0].DBInstanceStatus" --output text
deleting

# 2-1 삭제가 완료된 후에는 not found 에러가 뜬다.
aws rds describe-db-instances --db-instance-identifier {{staging db}} --query "DBInstances[0].DBInstanceStatus" --output text
An error occurred (DBInstanceNotFound) when calling the DescribeDBInstances operation: DBInstance {{staging db}} not found.

# 2 db 재생성
/usr/bin/aws rds restore-db-instance-from-db-snapshot \
    --db-instance-class db.t3.micro \ # t 계열 머신 할당(개발계이므로) 
    --db-subnet-group-name {{STAGING VPC의 DB 서브넷}} \
    --vpc-security-group-ids {{대상 보안그룹}} \
    --option-group-name {{새롭게 만들어서 할당한 옵션그룹}} \
    --db-instance-identifier {{내가 생성할 DB}} \
    --db-snapshot-identifier $( \ # PROD DB의 가장 마지막 스냅샷만 가져옴.
      aws rds describe-db-snapshots \
        --query "DBSnapshots[?SnapshotType=='automated' && \
          DBInstanceIdentifier=='{{복사원천 PROD DB}}'] \
          | sort_by(@, &SnapshotCreateTime) \
          | [-1].DBSnapshotId

 

 

디버깅기

An error occurred (DBInstanceAlreadyExists) when calling the RestoreDBInstanceFromDBSnapshot operation: DB instance already exists

=> 무작정 DB 를 복원하는 것이 아니라 사전에 지워주는 작업이 필요하다. 지우고 재생성 시 이름만 같으면 엔드포인트는 유지된다!

An error occurred (InvalidParameterCombination) when calling the RestoreDBInstanceFromDBSnapshot operation: The option group {{옵션그룹명}} is associated with a different VPC than the request.

=> PROD VPC에 있던 옵션 그룹을 디폴트로 사용한다. 이럴때에는 별도로 옵션그룹을 생성한뒤 cmd에서 할당해준다. 

 

반응형

댓글