服务器迁移中心 一次性迁移服务器

By | 2021年4月22日

本文介绍如何通过服务器迁移中心SMC Java SDK调用CreateReplicationJob和StartReplicationJob创建并执行一次性迁移任务,再调用DescribeReplicationJobs查询迁移任务的信息。

前提条件

完整代码示例

from aliyunsdkcore.client import AcsClient
from aliyunsdksmc.request.v20190601 import CreateReplicationJobRequest
from aliyunsdksmc.request.v20190601 import StartReplicationJobRequest
from aliyunsdksmc.request.v20190601 import DescribeReplicationJobsRequest
import json
import time
import uuid

# 初始化AcsClient实例,设置地域ID及您的AccessKey信息
client = AcsClient('<accessKeyId>', '<accessSecret>', '<region-Id>')

# 创建迁移任务
def create_replication_job():
    #创建API请求,并设置参数
    request = CreateReplicationJobRequest.CreateReplicationJobRequest()
    # 迁移源ID
    request.set_SourceId('s-bp152mkfswvidyr3****')
    # 目标阿里云服务器ECS的系统盘大小,单位为GiB
    request.set_SystemDiskSize(80)
    # 保证请求幂等性,您可以从客户端生成一个不超过64个ASCII字符的参数值,并将值赋予ClientToken
    # request.set_ClientToken(uuid.uuid4())
    # 迁移任务名
    request.set_Name('smcName')
    # 迁移任务描述
    request.set_Description('setDescription')
    # 已配置高速通道服务或者VPN网关的VPC的ID
    request.set_VpcId('vpc-bp1vwnn14rqpyiczj****')
    # 指定VPC下的交换机ID
    request.set_VSwitchId('vsw-bp1ddbrxdlrcbim46****')

    # 发送请求获取返回值
    response = client.do_action_with_exception(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))
    print('**********成功创建迁移任务**********')
    return json.loads(response)['JobId']

# 启动迁移任务
def start_replication_job(job_id):
    # 创建API请求,并设置参数
    request = StartReplicationJobRequest.StartReplicationJobRequest()
    # 迁移任务ID
    request.set_JobId(job_id)

    # 发送请求获取返回值
    response = client.do_action_with_exception(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))
    print('**********启动迁移任务**********')

# 查询迁移任务状态
def describe_replication_jobs(job_ids):
    # 创建API请求,并设置参数
    request = DescribeReplicationJobsRequest.DescribeReplicationJobsRequest()
    # 设置迁移任务ID列表
    jobIds = [job_ids]
    request.set_JobIds(jobIds)
    # 设置迁移任务的名称
    request.set_Name('smcName')
    # 设置迁移任务的页码
    request.set_PageNumber(1)
    # 设置每页行数
    request.set_PageSize(10)
    # 设置迁移源ID列表
    sourceIds = ['s-bp152mkfswvidyr3****']
    request.set_SourceIds(sourceIds)

    # 发送请求获取返回值
    response = client.do_action_with_exception(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))
    print('**********查看迁移任务状态**********')
    jobslist = json.loads(response)['ReplicationJobs']['ReplicationJob']
    for i in jobslist:
        return (i['Status'])

# 执行创建迁移任务方法
job_id = create_replication_job()
# 执行启动迁移任务方法
start_replication_job(job_id)
# 每隔10分钟执行一次查询迁移任务状态方法,判断是否迁移完成。
while True:
    status = describe_replication_jobs(job_id)
    print('迁移任务正在执行……')
    time.sleep(600)
    if status == 'Finished':
        print('迁移完成!')
        break

执行结果

{
    "JobId": "j-bp15dpodoftnvm1s****",
    "RequestId": "1042DD41-FEF1-4267-8733-C910B765B9CF"
}
**********成功创建迁移任务**********
{
    "RequestId": "2A7930A1-56B2-4B5D-9874-4CB48F5C5095"
}
**********启动迁移任务**********
{
    "PageNumber": 1,
    "TotalCount": 1,
    "PageSize": 10,
    "ReplicationJobs": {
        "ReplicationJob": [{
            "Description": "setDescription",
            "SystemDiskSize": 80,
            "TargetType": "Image",
            "VSwitchId": "vsw-bp1ddbrxdlrcbim46****",
            "SourceId": "s-bp152mkfswvidyr3****",
            "NetMode": 0,
            "VpcId": "vpc-bp1vwnn14rqpyiczj****",
            "Progress": 0.0,
            "Name": "smcName",
            "CreationTime": "2020-01-08T08:27:45Z",
            "Status": "Running",
            "BusinessStatus": "Preparing",
            "JobId": "j-bp15dpodoftnvm1s****",
            "RegionId": "cn-hangzhou",
            "RunOnce": true,
            "StartTime": "2020-01-08T08:27:46Z",
            "ValidTime": "2020-02-07T08:27:45Z"
        }]
    },
    "RequestId": "B62A8AEE-95AA-400F-8A6C-4822E3F4B690"
}
**********查看迁移任务状态**********
迁移任务正在执行……
{
    "PageNumber": 1,
    "TotalCount": 1,
    "PageSize": 10,
    "ReplicationJobs": {
        "ReplicationJob": [{
            "Description": "setDescription",
            "SystemDiskSize": 80,
            "TargetType": "Image",
            "VSwitchId": "vsw-bp1ddbrxdlrcbim46****",
            "SourceId": "s-bp152mkfswvidyr3****",
            "NetMode": 0,
            "VpcId": "vpc-bp1vwnn14rqpyiczj****",
            "Progress": 0.0,
            "Name": "smcName",
            "CreationTime": "2020-01-08T08:27:45Z",
            "Status": "Running",
            "BusinessStatus": "Preparing",
            "JobId": "j-bp15dpodoftnvm1s****",
            "RegionId": "cn-hangzhou",
            "RunOnce": true,
            "StartTime": "2020-01-08T08:27:46Z",
            "ValidTime": "2020-02-07T08:27:45Z"
        }]
    },
    "RequestId": "B62A8AEE-95AA-400F-8A6C-4822E3F4B690"
}
**********查看迁移任务状态**********
迁移完成!

请关注公众号获取更多资料

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注