一次性迁移服务器
本文介绍如何通过服务器迁移中心SMC Java SDK调用CreateReplicationJob和StartReplicationJob创建并执行一次性迁移任务,再调用DescribeReplicationJobs查询迁移任务的信息。
前提条件
- 已导入迁移源,详情请参见步骤一:导入迁移源。
- 已将Maven依赖添加至pom.xml文件中,详情请参见安装Java SDK。
完整代码示例
package com.aliyun.smc;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.smc.model.v20190601.*;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class Demo {
public static void main(String[] args) {
//初始化AcsClient实例,设置地域ID及您的AccessKey信息
DefaultProfile profile = DefaultProfile.getProfile("<region-Id>", "<accessKeyId>", "<accessSecret>");
IAcsClient client = new DefaultAcsClient(profile);
//创建迁移任务
String jobId = createReplicationJob(client);
//启动迁移任务
startReplicationJob(client,jobId);
//查询迁移任务状态
while(true){
String result = describeReplicationJobs(client,jobId);
System.out.println("迁移正在进行……");
if("Finished".equals(result)){
System.out.println("迁移完成!");
break;
}
try {
Thread.sleep(600000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static String createReplicationJob (IAcsClient client){
//创建API请求,并设置参数
CreateReplicationJobRequest request = new CreateReplicationJobRequest();
//迁移源ID
request.setSourceId("s-bp152mkfswviw7sd****");
//目标阿里云服务器ECS的系统盘大小,单位为GiB
request.setSystemDiskSize(80);
//保证请求幂等性,您可以从客户端生成一个不超过64个ASCII字符的参数值,并将值赋予ClientToken
//request.setClientToken(UUID.randomUUID().toString());
//迁移任务描述
request.setDescription("smcDescription");
//迁移任务名
request.setName("MigrationTask");
//创建一次性迁移任务还是增量迁移任务
request.setRunOnce(true);
//迁移任务的执行时间
//request.setScheduledStartTime("2019-06-04T13:35:00Z");
//迁移任务的标签键值对
List<CreateReplicationJobRequest.Tag> tags = new ArrayList<>();
CreateReplicationJobRequest.Tag tag = new CreateReplicationJobRequest.Tag();
tag.setKey("usage");
tag.setValue("production");
tags.add(tag);
request.setTags(tags);
//迁移任务交付的目标类型
//request.setTargetType("Image");
//指定VPC下的交换机ID
request.setVSwitchId("vsw-bp1ddbrxdlrcbim46****");
//迁移任务的过期时间
//request.setValidTime("2019-06-04T13:35:00Z");
//已配置高速通道服务或者VPN网关的VPC的ID
request.setVpcId("vpc-bp1vwnn14rqpyiczj****");
//发送请求获取返回值或处理异常
CreateReplicationJobResponse response = null;
try {
response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
System.out.println("**********成功创建迁移任务**********");
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return response.getJobId();
}
private static void startReplicationJob(IAcsClient client,String jobId){
//创建API请求,并设置参数
StartReplicationJobRequest request = new StartReplicationJobRequest();
//迁移任务ID
request.setJobId(jobId);
//发送请求获取返回值或处理异常
try {
StartReplicationJobResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
System.out.println("**********启动迁移任务**********");
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
private static String describeReplicationJobs(IAcsClient client,String jobId){
//创建API请求,并设置参数
DescribeReplicationJobsRequest request = new DescribeReplicationJobsRequest();
//设置迁移任务的页码
request.setPageNumber(1);
//设置每页行数
request.setPageSize(10);
//设置迁移任务的名称
request.setName("MigrationTask");
//设置迁移任务ID列表
List<String> jobIds = new ArrayList<>();
jobIds.add(jobId);
request.setJobIds(jobIds);
//设置迁移源ID列表
List<String> sourceIds = new ArrayList<>();
sourceIds.add("s-bp152mkfswviw7sd****");
request.setSourceIds(sourceIds);
//发送请求获取返回值或处理异常
DescribeReplicationJobsResponse response = null;
try {
response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
System.out.println("**********查询迁移任务状态**********");
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
String status = null;
List<DescribeReplicationJobsResponse.ReplicationJob> replicationJobList = response.getReplicationJobs();
for(DescribeReplicationJobsResponse.ReplicationJob rl : replicationJobList){
status = rl.getStatus();
}
return status;
}
}
执行结果
{
"requestId": "667E03A3-7D16-4822-8B8D-1F3883B25903",
"jobId": "j-bp152mkfswviwbqj****"
}
**********成功创建迁移任务**********
{
"requestId": "3A236693-5FF9-4F76-8F7D-A405113A6B55"
}
**********启动迁移任务**********
{
"requestId": "0E9F3604-D010-4EDE-995C-F123ED9CEB5B",
"totalCount": 1,
"pageNumber": 1,
"pageSize": 10,
"replicationJobs": [{
"jobId": "j-bp152mkfswviwbqj****",
"sourceId": "s-bp152mkfswviw7sd****",
"name": "MigrationTask",
"description": "smcDescription",
"regionId": "cn-hangzhou",
"targetType": "Image",
"status": "Running",
"businessStatus": "Preparing",
"progress": 0.0,
"creationTime": "2020-01-08T06:44:04Z",
"validTime": "2020-02-07T06:44:05Z",
"startTime": "2020-01-08T06:44:05Z",
"netMode": 0,
"systemDiskSize": 80,
"vpcId": "vpc-bp1vwnn14rqpyiczj****",
"vSwitchId": "vsw-bp1ddbrxdlrcbim4****j",
"runOnce": true,
"dataDisks": [],
"replicationJobRuns": []
}]
}
**********查询迁移任务状态**********
迁移正在进行……
{
"requestId": "0E9F3604-D010-4EDE-995C-F123ED9CEB5B",
"totalCount": 1,
"pageNumber": 1,
"pageSize": 10,
"replicationJobs": [{
"jobId": "j-bp152mkfswviwbqj****",
"sourceId": "s-bp152mkfswviw7sd****",
"name": "MigrationTask",
"description": "smcDescription",
"regionId": "cn-hangzhou",
"targetType": "Image",
"status": "Running",
"businessStatus": "Preparing",
"progress": 0.0,
"creationTime": "2020-01-08T06:44:04Z",
"validTime": "2020-02-07T06:44:05Z",
"startTime": "2020-01-08T06:44:05Z",
"netMode": 0,
"systemDiskSize": 80,
"vpcId": "vpc-bp1vwnn14rqpyiczj****",
"vSwitchId": "vsw-bp1ddbrxdlrcbim46****",
"runOnce": true,
"dataDisks": [],
"replicationJobRuns": []
}]
}
**********查询迁移任务状态**********
迁移正在进行……
{
"requestId": "0E9F3604-D010-4EDE-995C-F123ED9CEB5B",
"totalCount": 1,
"pageNumber": 1,
"pageSize": 10,
"replicationJobs": [{
"jobId": "j-bp152mkfswviwbqj****",
"sourceId": "s-bp152mkfswviw7sd****",
"name": "MigrationTask",
"description": "smcDescription",
"regionId": "cn-hangzhou",
"targetType": "Image",
"status": "Finished",
"businessStatus": "Preparing",
"progress": 0.0,
"creationTime": "2020-01-08T06:44:04Z",
"validTime": "2020-02-07T06:44:05Z",
"startTime": "2020-01-08T06:44:05Z",
"netMode": 0,
"systemDiskSize": 80,
"vpcId": "vpc-bp1vwnn14rqpyiczj****",
"vSwitchId": "vsw-bp1ddbrxdlrcbim46****",
"runOnce": true,
"dataDisks": [],
"replicationJobRuns": []
}]
}
**********查询迁移任务状态**********
迁移完成!
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论