{DocumentDB}手動クラスタースナップショットの作成

-- 1. Amazon DocumentDB クラスターの作成

aws docdb create-db-cluster \
--db-cluster-identifier cluster01 \
--engine docdb \
--engine-version 4.0.0 \
--port 27017 \
--master-username test \
--master-user-password 'password' \
--no-storage-encrypted \
--no-deletion-protection


aws docdb create-db-instance \
--db-instance-identifier instance01 \
--db-instance-class db.t3.medium \
--engine docdb \
--no-auto-minor-version-upgrade \
--db-cluster-identifier cluster01

aws docdb create-db-cluster \
--db-cluster-identifier cluster02 \
--engine docdb \
--engine-version 4.0.0 \
--port 27017 \
--master-username test \
--master-user-password 'password' \
--storage-encrypted \
--no-deletion-protection


aws docdb create-db-instance \
--db-instance-identifier instance02 \
--db-instance-class db.t3.medium \
--engine docdb \
--no-auto-minor-version-upgrade \
--db-cluster-identifier cluster02


aws docdb describe-db-clusters \
--filter Name=engine,Values=docdb


aws docdb describe-db-instances \
--filter Name=engine,Values=docdb

 

-- 2. テストデータ作成
echo -e "[mongodb-org-4.0] \nname=MongoDB Repository\nbaseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/4.0/x86_64/\ngpgcheck=1 \nenabled=1 \ngpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc" | sudo tee /etc/yum.repos.d/mongodb-org-4.0.repo
sudo yum install -y mongodb-org-shell

wget https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
mongo --ssl --host cluster01.cluster-xxxxxxxxxxxx.ap-northeast-1.docdb.amazonaws.com:27017 --sslCAFile rds-combined-ca-bundle.pem --username test
mongo --ssl --host cluster02.cluster-xxxxxxxxxxxx.ap-northeast-1.docdb.amazonaws.com:27017 --sslCAFile rds-combined-ca-bundle.pem --username test

--コレクション作成とデータ追加
use test

db.tab1.insert({_id:1,col2:"val12",col3:13});
db.tab1.insert({_id:2,col2:"val22",col3:23});

db.tab1.find()

show dbs
show collections

-- 3. クラスタースナップショットを作成する

aws docdb create-db-cluster-snapshot \
--db-cluster-identifier cluster01 \
--db-cluster-snapshot-identifier snap01


aws docdb create-db-cluster-snapshot \
--db-cluster-identifier cluster02 \
--db-cluster-snapshot-identifier snap02

aws docdb describe-db-cluster-snapshots

 

-- 4. クラスタースナップショットのコピー

-- ①Copy an unencrypted snapshot to the same Region

aws docdb copy-db-cluster-snapshot \
--source-db-cluster-snapshot-identifier snap01 \
--target-db-cluster-snapshot-identifier snap011


-- ②Copy an unencrypted snapshot across AWS Regions

export AWS_DEFAULT_REGION=ap-southeast-1
aws docdb copy-db-cluster-snapshot \
--source-db-cluster-snapshot-identifier arn:aws:rds:ap-northeast-1:999999999999:cluster-snapshot:snap01 \
--target-db-cluster-snapshot-identifier snap012

export AWS_DEFAULT_REGION=ap-northeast-1

-- ③Copy an encrypted snapshot to the same Region

aws docdb copy-db-cluster-snapshot \
--source-db-cluster-snapshot-identifier snap02 \
--target-db-cluster-snapshot-identifier snap021


-- ④Copy an encrypted snapshot across AWS Regions

export AWS_DEFAULT_REGION=ap-southeast-1

aws docdb copy-db-cluster-snapshot \
--source-db-cluster-snapshot-identifier arn:aws:rds:ap-northeast-1:999999999999:cluster-snapshot:snap02 \
--target-db-cluster-snapshot-identifier snap022 \
--source-region ap-northeast-1 \
--kms-key-id arn:aws:kms:ap-southeast-1:999999999999:key/11111111-2222-3333-4444-555555555555

export AWS_DEFAULT_REGION=ap-northeast-1

-- 5. クラスタースナップショットからの復元

aws docdb restore-db-cluster-from-snapshot \
--db-cluster-identifier cluster011 \
--snapshot-identifier snap011 \
--engine docdb

aws docdb create-db-instance \
--db-instance-identifier instance011 \
--db-instance-class db.t3.medium \
--engine docdb \
--no-auto-minor-version-upgrade \
--db-cluster-identifier cluster011


aws docdb restore-db-cluster-from-snapshot \
--db-cluster-identifier cluster021 \
--snapshot-identifier snap021 \
--engine docdb

aws docdb create-db-instance \
--db-instance-identifier instance021 \
--db-instance-class db.t3.medium \
--engine docdb \
--no-auto-minor-version-upgrade \
--db-cluster-identifier cluster021


export AWS_DEFAULT_REGION=ap-southeast-1

aws docdb restore-db-cluster-from-snapshot \
--db-cluster-identifier cluster012 \
--snapshot-identifier snap012 \
--engine docdb

aws docdb create-db-instance \
--db-instance-identifier instance012 \
--db-instance-class db.t3.medium \
--engine docdb \
--no-auto-minor-version-upgrade \
--db-cluster-identifier cluster012


aws docdb restore-db-cluster-from-snapshot \
--db-cluster-identifier cluster022 \
--snapshot-identifier snap022 \
--engine docdb

aws docdb create-db-instance \
--db-instance-identifier instance022 \
--db-instance-class db.t3.medium \
--engine docdb \
--no-auto-minor-version-upgrade \
--db-cluster-identifier cluster022

export AWS_DEFAULT_REGION=ap-northeast-1

-- 6. クリーンアップ

-- Amazon DocumentDBインスタンス削除

aws docdb delete-db-instance \
--db-instance-identifier instance01

aws docdb delete-db-instance \
--db-instance-identifier instance02

aws docdb delete-db-instance \
--db-instance-identifier instance011

aws docdb delete-db-instance \
--db-instance-identifier instance021

export AWS_DEFAULT_REGION=ap-southeast-1


aws docdb delete-db-instance \
--db-instance-identifier instance012

aws docdb delete-db-instance \
--db-instance-identifier instance022

export AWS_DEFAULT_REGION=ap-northeast-1


-- Amazon DocumentDBクラスター削除

aws docdb delete-db-cluster \
--db-cluster-identifier cluster01 \
--skip-final-snapshot

aws docdb delete-db-cluster \
--db-cluster-identifier cluster02 \
--skip-final-snapshot

aws docdb delete-db-cluster \
--db-cluster-identifier cluster011 \
--skip-final-snapshot

aws docdb delete-db-cluster \
--db-cluster-identifier cluster021 \
--skip-final-snapshot

export AWS_DEFAULT_REGION=ap-southeast-1

aws docdb delete-db-cluster \
--db-cluster-identifier cluster012 \
--skip-final-snapshot

aws docdb delete-db-cluster \
--db-cluster-identifier cluster022 \
--skip-final-snapshot

export AWS_DEFAULT_REGION=ap-northeast-1


-- クラスタースナップショットの一覧
aws docdb describe-db-cluster-snapshots


-- クラスタースナップショットの削除

aws docdb delete-db-cluster-snapshot --db-cluster-snapshot-identifier snap01
aws docdb delete-db-cluster-snapshot --db-cluster-snapshot-identifier snap02
aws docdb delete-db-cluster-snapshot --db-cluster-snapshot-identifier snap011
aws docdb delete-db-cluster-snapshot --db-cluster-snapshot-identifier snap021

 

export AWS_DEFAULT_REGION=ap-southeast-1
aws docdb delete-db-cluster-snapshot --db-cluster-snapshot-identifier snap012
aws docdb delete-db-cluster-snapshot --db-cluster-snapshot-identifier snap022

export AWS_DEFAULT_REGION=ap-northeast-1