
# 示例2：创建数据迁移任务
#### 场景描述
本章节指导用户通过API创建数据迁移任务。API的调用方法请参见[如何调用API](https://support.huaweicloud.com/api-dcs/api-call-process.html)。
本文使用在线迁移、全量迁移的方式为例创建数据迁移任务，源实例与目标实例是华为云上处于相同VPC的Redis。
#### 涉及接口
创建数据迁移任务，涉及的API如下：
- [查询源实例的IP和端口]：确定迁移的源Redis实例的IP和端口。
- [创建数据迁移任务]：创建数据迁移任务。
- [查询迁移结果]：确认迁移是否成功。
 
 #### 步骤1：确定源实例的IP和端口
1. 查询指定实例的IP和端口。 
   - 接口相关信息 URI格式：GET /v2/{project_id}/instances/{instance_id}
     
   
   - 调试 您可以在[API Explorer](https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=DCS&api=ShowInstance)中调试该接口。
     
   
   - 请求示例 GET https://{dcs_endpoint}/v2/666486c2d9b948c1bbea57e714d744fa/instances/21bc7b53-2494-4f10-bb0b-c0b913d9e329
     {dcs_endpoint}信息请从[地区和终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/DCS)获取。
     
   
   - 响应示例
     ```
     {
     "free": null,
     "max_memory": 2048,
     "used_memory": 1,
     "instance_id": "21bc7b53-2494-4f10-bb0b-c0b913d9e329",
     "name": "dcs-api-test",
     "resource_spec_code": "redis.single.au1.large.2",
     "engine": "Redis",
     "engine_version": "5.0",
     "internal_version": null,
     "charging_mode": 0,
     "capacity": 2,
     "capacity_minor": null,
     "vpc_id": "743bf021-2c2d-4511-aeac-85bd48c06af7",
     "vpc_name": "vpc-d2d4",
     "ip": "192.168.0.100",
     "domain_name": "redis-88a7bbb-dcs-api-test.dcs.huaweicloud.com",
     "readonly_domain_name": null,
     "port": 6379,
     "status": "RUNNING",
     "freeze_scene": null,
     "created_at": "2020-06-05T03:30:36.273Z",
     "update_at": "2020-06-05T03:30:47.231Z",
     "error_code": null,
     "user_id": "d53977d1adfb49c5b025ba7d33a13fd7",
     "user_name": "paas_dcs_a00421997_02",
     "maintain_begin": "02:00:00",
     "maintain_end": "06:00:00",
     "no_password_access": "true",
     "access_user": null,
     "enable_publicip": false,
     "publicip_id": null,
     "publicip_address": null,
     "enable_ssl": false,
     "service_upgrade": false,
     "service_task_id": "",
     "enterprise_project_id": "0",
     "security_group_id": null,
     "tags": [],
     "product_type": "generic",
     "cpu_type": "aarch64",
     "storage_type": "DRAM",
     "launched_at": "2020-06-05T03:30:47.238Z",
     "is_free": null,
     "libos": false,
     "cache_mode": "single",
     "available_zones": ["d539378ec1314c85b76fefa3f7071458"],
     "subnet_id": "a4112635-3ec0-471c-95c3-5cf49b9533af",
     "backend_addrs": null,
     "crr_role": null,
     "cloud_service_type_code": "hws.service.type.dcs",
     "cloud_resource_type_code": "hws.resource.type.dcs3",
     "support_slow_log_flag": null,
     "ipv6": null,
     "enable_ipv6": false,
     "description": "",
     "product_id": "redis.single.au1.large.2-h",
     "security_group_name": null,
     "subnet_name": "dcs-beta",
     "order_id": null,
     "subnet_cidr": "192.168.0.0/24",
     "task": null,
     "instance_backup_policy": null,
     "enterprise_project_name": null
     }
     ```
     
   
   
   
   
2. 根据需要，记录实例的IP和端口。
 
 #### 步骤2：创建数据迁移任务
- 接口相关信息 URI格式：POST /v2/{project_id}/migration-task
  
- 调试 您可以在[API Explorer](https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=DCS&api=CreateMigrationTask)中调试该接口。
  
- 请求示例 POST https://{dcs_endpoint}/v2/666486c2d9b948c1bbea57e714d744fa/migration-task
  {dcs_endpoint}信息请从[地区和终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/DCS)获取。
  Body：
  ```
  {
  "migration_method": "full_amount_migration",
  "migration_type": "online_migration",
  "network_type": "vpc",
  "source_instance": {
  "addrs": "192.168.0.79:6379"
  },
  "target_instance": {
  "id": "92b2b922-0f96-4664-acc5-04d922867c4d"
  },
  "task_name": "dcs-test-migration"
  }
  ```
  
- 响应示例
  ```
  {
  "id": "8aa6999f727f0a40017283479d730d7c",
  "name": "dcs-test-migration",
  "status": "MIGRATING",
  "error": ""
  }
  ```
  创建数据迁移任务的参数说明请参考[创建备份导入任务 - CreateMigrationTask](https://support.huaweicloud.com/api-dcs/CreateMigrationTask.html)。
  
 
 #### 步骤3：查询迁移结果
- 接口相关信息 URI格式：GET /v2/{project_id}/migration-task/{task_id}
  
- 调试 您可以在[API Explorer](https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=DCS&api=ShowMigrationTask)中调试该接口。
  
- 请求示例 GET https://{dcs_endpoint}/v2/666486c2d9b948c1bbea57e714d744fa/migration-task/8aa6999f727f0a40017283479d730d7c
  {dcs_endpoint}信息请从[地区和终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/DCS)获取。
  
- 响应示例
  ```
  {
  "task_name": "dcs-test-migration",
  "task_id": "8aa6999f727f0a40017283479d730d7c",
  "description": null,
  "status": "SUCCESS",
  "migration_type": "online_migration",
  "migration_method": "full_amount_migration",
  "backup_files": null,
  "network_type": "vpc",
  "bandwidth_limit_mb": null,
  "source_instance": {
  "id": null,
  "ip": "192.168.0.79:6379",
  "port": null,
  "name": null,
  "password": null,
  "addrs": null
  },
  "target_instance": {
  "id": "92b2b922-0f96-4664-acc5-04d922867c4d",
  "ip": null,
  "port": null,
  "name": "dcs-xwvq",
  "password": null,
  "addrs": null
  },
  "created_at": "2020-06-05T07:00:08.204Z",
  "updated_at": "2020-06-05T07:03:25.075Z"
  }
  ```
  任务状态为SUCCESS时，即迁移成功。
  
 
