python之paramiko模块 数据迁移

import paramiko
import datetime,zipfile,os,time
class gridToalone():
    '''
    grid环境迁移到grid独立部署的环境

    '''
    date = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
    def conn(self,ip,port,privatekey):
            private_key = paramiko.RSAKey.from_private_key_file('conf/privatekey_grid')
            transport = paramiko.Transport((ip, port))
            transport.connect(username="root", pkey=private_key)
            self.__transport = transport

    def ftp_put(self, source_file, target_file):
        '''
        上传文件
        :param source_file: 本地文件地址
        :param target_file: 目标地址
        :return:
        '''
        fpt = paramiko.SFTPClient.from_transport(self.__transport)
        fpt.put(source_file, target_file)

    def ftp_get(self, source_file, target_file):
        '''
        下载文件
        :param source_file: 文件所在地址
        :param target_file: 本地地址
        :return:
        '''
        fpt = paramiko.SFTPClient.from_transport(self.__transport)
        fpt.get(source_file, target_file)
    def cmd(self,commd):
        '''
        执行命令
        :param commd:
        :return:
        '''
        ssh = paramiko.SSHClient()
        ssh._transport =self.__transport
        stdin, stdout, stderr = ssh.exec_command(commd)
        r = stdout.read().decode()
        print(r)
    def script_zip(self,siteid,script_path):
        '''
        创建备份脚本
        :param siteid:
        :return:
        '''
        dump_shell = "dump_start.sh"
        list = os.listdir('{}/'.format(script_path))
        if 'kf_' in siteid :
            cmd = "nohup sh b2c.sh table_name_all.txt %s %s &" % (siteid, siteid)
            f = open('{}/{}'.format(script_path,dump_shell), 'w')
            f.write(cmd)
            f.close()
        elif '_1000' in siteid:
            first,second = siteid.split('_')
            cmd = "nohup sh b2b.sh table_name_all.txt {}_% {} &" .format(first, siteid)
            f = open('{}/{}'.format(script_path,dump_shell), 'w')
            f.write(cmd)
            f.close()
        else:
            print("siteid is not exist!")
        #压缩
        zpf = zipfile.ZipFile('{}/script_new.zip'.format(script_path), mode='w')
        try:
            for i in list:
                if ".zip" in i:
                    pass
                else:
                    zpf.write('{}/{}'.format(script_path,i))
        finally:
            print('压缩完成')
            zpf.close()
    def backup_data(self,common):
        '''
        源服务器,备份数据
        :param common:
        :return:
        '''
        self.conn(common['source_host'], common['port'],common['privatekey'])
        self.cmd('mkdir -p {}'.format(common['backup_path']))
        self.script_zip(common['siteid'], '{}'.format(common['local_script_path']))
        print(common['local_script_path'])
        # 上传备份文件
        print('上传备份脚本!!!!')
        self.ftp_put('{}script_new.zip'.format(common['local_script_path']), '{}/script_new.zip'.format(common['backup_path']))
        self.cmd('cd  {};unzip -o script_new.zip'.format(common['backup_path']))
        print('start backup.....')
        self.cmd('cd  {}/{};sh dump_start.sh'.format(common['backup_path'],common['local_script_path'],))
        zip_name = "{}{}.zip".format(common['siteid'], current_date)
        self.cmd('cd  {}/{}/;zip -r {} {}/'.format(common['backup_path'],common['local_script_path'],
                                                    zip_name, common['siteid']))
        print('start down file....')
        self.ftp_get('{}/{}/{}'.format(common['backup_path'],common['local_script_path'], zip_name),
                     '{}/{}'.format(common['local_get_path'],zip_name))
        print("down ok!")
        self.conn_close()
    def import_data(self,common):
        '''
        目标服务器,导入数据
        :param common:
        :return:
        '''
        self.conn(common['target_host'], common['port'],common['privatekey'])
        self.cmd('mkdir -p {}'.format(common['backup_path']))
        zip_name = "{}{}.zip".format(common['siteid'], current_date)
        print('start put file')
        self.ftp_put('{}/{}'.format(common['local_get_path'],zip_name),
                  '{}/{}'.format(common['backup_path'], zip_name))
        print('put ok!')
        self.cmd('cd {};unzip -o {}'.format(common['backup_path'], zip_name))
        print('解压成功!')
        for_cmmd = '''for i in `ls -lh data/|grep -v total|grep -v dump.sh|awk '{print $NF}'`;do mysql test -e " source data/$i";done'''
        f = open('{}import_script.sh'.format(common['local_script_path']),'w')
        f.write(for_cmmd)
        f.close()
        self.ftp_put('{}import_script.sh'.format(common['local_script_path']),
                     '{}/{}/import_script.sh'.format(common['backup_path'], common['siteid']))
        #删除生成的shell脚本
        os.remove('{}import_script.sh'.format(common['local_script_path']))
        # self.full_backup(common)
        #execute import data script
        self.cmd('cd {}/{};sh -x import_script.sh'.format(common['backup_path'], common['siteid']))
        print('执行结束!')
    def full_backup(self,common):
        self.conn(common['target_host'],common['port'],common['privatekey'])
        print('start back data.....')
        dump_cmd = 'mysqldump -h127.0.0.1 test >{}/kf_full-{}.sql'.format(common['backup_path'],common['current_date'])
        self.cmd(dump_cmd)
        print('end back....')

    def conn_close(self):
        self.__transport.close()
        print('close conntenct')
    def run(self):
        pass


if __name__ == '__main__':
    current_date = datetime.datetime.now().strftime('%Y%m%d')
    common = {
        'source_host':'',#源服务器ip
        'target_host':'',#目标服务器ip
        'port':22,
        'database':'test',
        'privatekey': 'conf/privatekey_grid',
        'siteid': 'kf_0000',
        'backup_path': '/opt/prod/ntupload/{}'.format(current_date),
        'local_script_path': 'script/',
        'local_get_path': 'db',
        'current_date':current_date

    }

    import time
    starttime = time.time()
    #源服务器
    c = gridToalone()
    c.backup_data(common)
    c.conn_close()
    # #目标服务器

    # print('\033[1;31;1m start target server import data........\033[1m')
    #导入数据
    # t = gridToalone()
    # t.full_backup(common)
    # t.import_data(common)
    # t.conn_close()

    endtime = time.time()
    print('total use time:',endtime-starttime)
    print('ok.............ok')

猜你喜欢

转载自blog.csdn.net/q936889811/article/details/80504194