【Amazon RDS】Restoring From a DB Snapshot with AWS CLI
In this post, I will illustrate how to restore a DB instance from a DB Snapshot with AWS CLI (Version "1.11.145").
Overview
- We would like to restore a DB instance to the same state as a backed up DB instance
- After checking the new DB instance, delete the current DB instance
How to restore from a DB Snapshot
Step1. Configure environment variables
export AWS_ACCESS_KEY_ID=****** export AWS_SECRET_ACCESS_KEY=****** export AWS_DEFAULT_REGION=us-west-2
Step2. Checking AWS account and IAM users
It is a good practice to check the IAM user and AWS account ID first.
aws sts get-caller-identity
Step3. Get information on the DB Instance to be restored
db_instance=mydbinstance echo ${db_instance}
aws rds describe-db-instances --db-instance-identifier ${db_instance} > ${db_instance}.json
Step4. Execute restore
Configure variables according to the DB instance we want to restore.
db_new_instance=mynewdbinstance instance_class=db.t2.micro subnet_group=mySubnetgroup snapshot=mySnapshotid echo ${db_new_instance} echo ${instance_class} echo ${subnet_group} echo ${snapshot}
Select options according to the DB instance to be restored. AWS CLI Command Reference | restore-db-instance-from-db-snapshot
aws rds restore-db-instance-from-db-snapshot \ --db-instance-identifier ${db_new_instance} \ --db-snapshot-identifier ${snapshot} \ --db-instance-class ${instance_class} \ --db-subnet-group-name ${subnet_group} \ --no-publicly-accessible \ --multi-az
Step5. Wait until the restored DB instance becomes available
AWS CLI Command Reference | db-instance-available
aws rds wait db-instance-available --db-instance-identifier ${db_new_instance}
Step6. Modifying Parameter Group and Security Group
The parameter group and security groups associated with the restored instance are assigned to their defaults. Therefore, we should modify the parameter group and security groups.
paramter_group=mydbparametergroup security_group=vpcsecuritygroup echo ${paramter_group} echo ${security_group}
AWS CLI Command Reference | modify-db-instance
aws rds modify-db-instance \ --db-instance-identifier ${db_new_instance} \ --db-parameter-group-name ${paramter_group} \ --vpc-security-group-ids ${security_group} \ --apply-immediately
Step7. Rebooting a DB Instance to apply changes
AWS CLI Command Reference | reboot-db-instance
aws rds reboot-db-instance \ --db-instance-identifier ${db_new_instance}
Step8. Wait until the restored DB Instance becomes available
aws rds wait db-instance-available --db-instance-identifier ${db_new_instance}
Step9. Get information on the restored DB Instance
aws rds describe-db-instances --db-instance-identifier ${db_new_instance} > ${db_new_instance}.json
Step10. Check the difference between the current DB Instance and the restored DB Instance
- DBInstanceIdentifier
- Address InstanceCreateTime
- LatestRestorableTime
- DbiResourceId
- DBInstanceArn
If values other than the above are the same, the restore process is completed.
diff ${db_instance}.json ${db_new_instance}.json
An example
$ diff ${db_instance}.json ${db_new_instance}.json 4c4 < "DBInstanceIdentifier": "mydbinstance", --- > "DBInstanceIdentifier": "mynewdbinstance", 11c11 < "Address": "mydbinstance.abcde*******.us-west-2.rds.amazonaws.com", --- > "Address": "mynewdbinstance.abcde*******.us-west-2.rds.amazonaws.com", 16c16 < "InstanceCreateTime": "2017-09-03T06:05:09.731Z", --- > "InstanceCreateTime": "2017-09-03T06:48:12.348Z", 64c64 < "LatestRestorableTime": "2017-09-03T06:35:00Z", --- > "LatestRestorableTime": "2017-09-03T07:00:00Z", 81c81 < "DbiResourceId": "db-RVYFY6********************", --- > "DbiResourceId": "db-5BKZOM********************", 86c86 < "DBInstanceArn": "arn:aws:rds:us-west-2:123456789012:db:mydbinstance", --- > "DBInstanceArn": "arn:aws:rds:us-west-2:123456789012:db:mynewdbinstance",
If the restore operation changes the new DB instance to a different availability zone, we can change it to the original availability zone using the --force-failover
parameter.
aws rds reboot-db-instance \ --db-instance-identifier ${db_new_instance} \ --force-failover
Step11. Delete previous DB Instance
aws rds delete-db-instance --db-instance-identifier ${db_instance} --skip-final-snapshot
Conclusion
This post introduced how to restore from DB Snapshot using the AWS CLI.
Note
※From a DB snapshot, it is not possible to restore to an existing DB instance. The restore process must be done to a new DB instance. Since the endpoint of the DB instance will be changed, we add a CNAME to the endpoint so that the endpoint can respond by switching DNS.
※Since Microsoft SQL Server and Oracle have additional requirements, please check the document at the referenced URL below.
※Some commands such as restore-db-instance-from-db-snapshot
have many parameters so please refer to the documents in the URLs below.