In this post, we demonstrate how to use the AWS Command Line Interface (AWS CLI) and jq (a JSON processor) to back up AWS Database Migration Service (AWS DMS) task settings and other artifacts to a file. This backup file could be modified (if required) and reused to setup a new AWS DMS task.
One of the cost dimensions of AWS DMS is the underlying Amazon Elastic Compute Cloud (Amazon EC2) instance hours. If you are migrating data at specific times, such as monthly, quarterly, or yearly, the replication instance could be idle when the task isn’t running but still incurring charges.
A migration may require adjusting task settings or table mapping with custom rules to add, remove, or change data columns. Therefore, it’s helpful to back up the AWS DMS task for future use and reuse the artifacts whenever required.
You can create a backup of your AWS DMS task in a JSON file, which you could then use to create a new AWS DMS task. This solution also includes a way to resume a full load and CDC or CDC-only AWS DMS task from the same checkpoint.
The following diagram illustrates the workflows to back up and restore the task.
The high-level steps to back up an AWS DMS task are:
Script – script out AWS DMS task using AWS CLI and JQ processor.
Transform – remove unneeded AWS DMS task output details and transform the JSON to be ready for creating a new AWS DMS task.
Save – finally save the newly generated json to a file.
Similarly high-level steps to restore an AWS DMS task are:
Restore Task – restore AWS DMS task using backup task JSON file.
Start – AWS DMS task is ready to be started.
For this walkthrough, you should have the following prerequisites:
Note: You can install AWS CLI and JQ tool on your local machine or an AWS EC2 server.
Retrieve AWS DMS task settings
To retrieve your AWS DMS replication task settings, run the describe-replication-tasks command using the AWS CLI and use the –filters flag. When you use this flag, AWS CLI returns only the AWS DMS task that matches one of these items:
The replication task ID
The replication task ARN
Run the following query to get the settings for your AWS DMS task using the replication task ID. Replace <dms_task_id> with your AWS DMS task ID.
Or run the following query to get the settings for your AWS DMS task using the replication task ARN. Replace <dms_task_arn> with your AWS DMS task ARN.
Filter and transform the JSON output using jq
Next, you filter and transform the JSON output from the previous step. You need to filter and transform this JSON in order to restore it at a later time.
Use the jq command line JSON processor to make these modifications to the output JSON:
Add the key CdcStartPosition, and then set it to RecoveryCheckpoint. (This is only relevant when the task has CDC.)
Note: even though we are transforming RecoveryCheckpoint to CdcStartPosition for a CDC task it is dependent on availability of that transaction in source DB transaction log.
Remove the following data:
Run one of the following commands to pipe the output JSON to jq, and then filter and transform the replication task data:
Use the following code with the AWS DMS replication task ID:
Use the following code with the AWS DMS replication task ARN:
Save the output to a JSON file
To save the resulting output as a backup, redirect it to a JSON file. For example, the following command saves the task settings for an AWS DMS task with the task ID dms_original_task to a JSON file named task_backup.json:
Or using ARN
The resulting JSON file shows an output similar to the following:
Restore the AWS DMS task
You can now edit the command below as needed to change the AWS DMS task identifier and restore the JSON file using the create-replication-task AWS CLI command. For example, the following command creates an AWS DMS replication task with the task ID restored-dms-task using the task_backup.json file:
Note that when you include –replication-task-identifier in the command, you override the –replication-task-identifier value in the task_backup.json file.
Similarly, if you have full load and cdc task and you are looking to start a cdc only task, you can override migration-type to cdc. Please note that the source database must have transactions referred by cdc-start-position or else the task will fail.
In this post, we demonstrated how to back up an existing AWS DMS task to a JSON file using the AWS CLI and JQ and restore the JSON file to a new AWS DMS task as and when needed.
We encourage you to try out this solution. If you have any feedback or questions, leave a comment in the comments section.
About the Authors
Subhash Raut is a Database Migration Specialist Solutions Architect at AWS. He works on challenges related to database migrations and works closely with customers to help them realize the true potential of AWS DMS. He has helped customers to migrate databases to the AWS Cloud by using AWS DMS and the AWS SCT.
Brian Oliver is a Cloud Support Engineer at AWS. He works with customers to provide guidance and technical assistance on database projects, helping them to improve the value of their solutions when using AWS.
Read MoreAWS Database Blog