腾讯云数据迁移DTS代码编写

# -*- coding: utf-8 -*-
from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# 导入对应产品模块的client models。
from tencentcloud.dts.v20180330 import dts_client, models

def returnClient():
    cred = credential.Credential("xxxx", "xxx")
    client = dts_client.DtsClient(cred, "ap-shanghai")
    return client

def createDtsTransfer(client,src_ins,des_ins,job_name,dbname):
    try:

        # 实例化一个请求对象
        req = models.CreateMigrateJobRequest()

        params = '''{
            "Region": "ap-shanghai-2",
            "JobName" : "%s",
            "MigrateOption" : {"RunMode":1,"MigrateType":2,"MigrateObject":1,"ConsistencyType":5,"IsOverrideRoot":0},
            "SrcDatabaseType" : "mysql",
            "SrcAccessType": "cdb",
            "SrcInfo" : {"InstanceId":"%s","Region":"ap-shanghai","Supplier":"others","User":"xxxxxx","Password":"xxxxxx"},
            "DstDatabaseType" : "mysql",
            "DstAccessType" : "cdb",
            "DstInfo" : {"InstanceId":"%s","Region":"ap-shanghai"},
        }'''%(job_name,src_ins,des_ins)
        print(params)
        req.from_json_string(params)
		# 这里后置Database参数,是因为前置在params里面的话,from_json_string函数会报错不识别""这种标识符
        req.DatabaseInfo = """[{"Database":"ali_order_0"}]"""
        #print(req.from_json_string(params))
	# 通过client对象调用想要访问的接口,需要传入请求对象
        resp = client.CreateMigrateJob(req)
        # 输出json格式的字符串回包

        ret = eval(resp.to_json_string())
        print(resp.to_json_string())
        return ret

    except TencentCloudSDKException as err:
        print(err)

#创建检查脚本
def createCheckDtsTransfer(client,jobid):
    try:
        req = models.CreateMigrateCheckJobRequest()
        params = '''{
            "Region": "ap-shanghai-2",
            "JobId": "%s"
        }''' %(jobid)
        req.from_json_string(params)
        resp = client.CreateMigrateCheckJob(req)
        return eval(resp.to_json_string())
    except TencentCloudSDKException as err:
        print(err)

#获得传输任务
def descDtsTransfer(client,jobid):
    try:
        req = models.DescribeMigrateJobsRequest()
        params = '''{
            "Region": "ap-shanghai-2",
            "JobId": "%s"
        }''' %(jobid)
        req.from_json_string(params)
        resp = client.DescribeMigrateJobs(req)
        return eval(resp.to_json_string())
    except TencentCloudSDKException as err:
        print(err)

#获得数据迁移检查信息
def descCheckDtsTransfer(client,jobid):
    try:
        req = models.DescribeMigrateCheckJobRequest()
        params = '''{
            "Region": "ap-shanghai-2",
            "JobId": "%s"
        }''' %(jobid)
        req.from_json_string(params)
        resp = client.DescribeMigrateCheckJob(req)
        return eval(resp.to_json_string())
    except TencentCloudSDKException as err:
        print(err)

#启动数据迁移任务		
def startDtsTransfer(client,jobid):
    try:
        req = models.StartMigrateJobRequest()
        params = '''{
            "Region": "ap-shanghai-2",
            "JobId": "%s"
        }'''%(jobid)
        req.from_json_string(params)
        resp = client.StartMigrateJob(req)
        return eval(resp.to_json_string())
    except TencentCloudSDKException as err:
        print(err)

#停止数据迁移任务		
def stopDtsTransfer(client,jobid):
    try:
        req = models.StopMigrateJobRequest()
        params = '''{
            "Region": "ap-shanghai-2",
            "JobId": "%s"
        }'''%(jobid)
        req.from_json_string(params)
        resp = client.StopMigrateJob(req)
        return eval(resp.to_json_string())
    except TencentCloudSDKException as err:
        print(err)

		
#删传输任务
def delDtsTransfer(client,jobid):
    try:
        req = models.DeleteMigrateJobRequest()
        params = '''{
            "Region": "ap-shanghai-2",
            "JobId": "%s"
        }'''%(jobid)
        req.from_json_string(params)
        resp = client.DeleteMigrateJob(req)
        return eval(resp.to_json_string())
    except TencentCloudSDKException as err:
        print(err)



if __name__ == '__main__':
        client = returnClient()
        src_ins = 'cdbro-xx'
        des_ins = 'cdb-xx'
        jobid='dts-xx'
        jobid = createDtsTransfer(client,src_ins,des_ins,'ali_order_0','ali_order_0')

猜你喜欢

转载自blog.csdn.net/mchdba/article/details/81241036