Point in time recovery in DynamoDB
Hello Everyone
Welcome to CloudAffaire and this is Debjeet
In the last blog posts, we have discussed On-Demand backup and restore in DynamoDB.
https://cloudaffaire.com/on-demand-backup-and-restore-in-dynamodb/
In this blog post, we are going to discuss Point In Time Recovery in DynamoDB. We will also enable point in time recovery for a table and restore from a point in time backup.
Point in time recovery in DynamoDB:
Amazon DynamoDB point-in-time recovery (PITR) provides automatic backups of your DynamoDB table data. You can enable point-in-time recovery using the AWS Management Console, AWS Command Line Interface (AWS CLI), or the DynamoDB API. When it’s enabled, point-in-time recovery provides continuous backups until you explicitly turn it off. After you enable point-in-time recovery, you can restore to any point in time within EarliestRestorableDateTime and LatestRestorableDateTime. LatestRestorableDateTime is typically 5 minutes before the current time.
Note: The point-in-time recovery process always restores to a new table.
For EarliestRestorableDateTime, you can restore your table to any point in time during the last 35 days. The retention period is a fixed 35 days (five calendar weeks) and can’t be modified. Any number of users can execute up to four concurrent restores (any type of restore) in a given account.If you disable point-in-time recovery and later re-enable it on a table, you reset the start time for which you can recover that table. As a result, you can only immediately restore that table using the LatestRestorableDateTime. When you restore using point in time recovery, DynamoDB restores your table data to the state based on the selected date and time (day:hour:minute:second) to a new table.
Included in Point In Time Recovery:
- Global secondary indexes (GSIs)
- Local secondary indexes (LSIs)
- Provisioned read and write capacity
- Encryption settings
Excluded from Point In Time Recovery and needs to be manually setup post restoration:
- Auto scaling policies
- AWS Identity and Access Management (IAM) policies
- Amazon CloudWatch metrics and alarms
- Tags
- Stream settings
- Time to Live (TTL) settings
- Point-in-time recovery settings
Next, we are going to create a table, enable point in time recovery and finally restore the point in time backup to another table.
Step 1: Create a table
1 2 3 4 5 6 |
aws dynamodb create-table ^ --table-name CloudAffaire ^ --attribute-definitions AttributeName=company,AttributeType=S AttributeName=id,AttributeType=N ^ --key-schema AttributeName=company,KeyType=HASH AttributeName=id,KeyType=RANGE ^ --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 ^ --output table |
Note: We have not included the endpoint of local dynamodb, hence the table will be created in AWS.
Step 2: Insert some data in the ClodAffaire table
1 2 |
aws dynamodb batch-write-item ^ --request-items file://employee-short.json |
Note: You can download the employee-short.json file from github
https://github.com/CloudAffaire/DynamoDB
Step 3: Enable Point In Time Recovery in CloudAffaire table
1 2 3 |
aws dynamodb update-continuous-backups ^ --table-name CloudAffaire ^ --point-in-time-recovery-specification PointInTimeRecoveryEnabled=True |
You can also check the point in time backup details using describe-continuous-backups AWS CLI.
1 2 |
aws dynamodb describe-continuous-backups ^ --table-name CloudAffaire |
Step 4: Insert a single record in the table.
1 2 3 |
aws dynamodb put-item ^ --table-name CloudAffaire ^ --item "{""company"":{""S"":""Google""},""id"":{""N"":""103""},""ename"":{""S"":""Steve""},""salary"":{""N"":""1500""}}" |
Next, we are going restore the point in time backup to another table. The new table will also contain data that we have just inserted.
Step 5: Restore point in time backup to a new table
1 2 3 4 |
aws dynamodb restore-table-to-point-in-time ^ --source-table-name CloudAffaire ^ --target-table-name CloudAffaire_Restore ^ --use-latest-restorable-time |
Note: It may take some time to get the backup restored.
If you view the items of restored table, it will include the new record that we have inserted after we enabled Point In Time Recovery.
Step 6: Disable Point In Time Recovery
1 2 3 |
aws dynamodb update-continuous-backups ^ --table-name CloudAffaire ^ --point-in-time-recovery-specification PointInTimeRecoveryEnabled=False |
Step 7: Delete both tables
1 2 |
aws dynamodb delete-table ^ --table-name CloudAffaire |
1 2 |
aws dynamodb delete-table ^ --table-name CloudAffaire_Restore |
Hope you have enjoyed this article. In the next blog post, we will discuss Global Tables in DynamoDB
To get more details on DynamoDB, please refer below AWS documentation
https://docs.aws.amazon.com/dynamodb/index.html