You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.6 KiB

import json
import paramiko
# 实例化ssh客户端
from Util import MysqlUtil
from scp import SCPClient
from Util.CommonUtil import *
# 初始化连接
with open("Config.json", 'r') as load_f:
connect = json.load(load_f)
db = MysqlUtil.MySQLConnect(connect)
# 获取所有需要备份的表【注意:%在python中需要转义:%%】
sql = "select table_name from information_schema.tables where table_schema='dsideal_db' and table_name like 't_un%%'"
tableNames = db.fetchall(sql)
tableStrs = ''
for t in tableNames:
tableStrs = tableStrs + ' ' + t['table_name']
# 备份多张表
gzName = 't_university_' + getCurrentTimeFileName() + '.sql.gz'
cmd = 'mysqldump -uroot -pDsideaL147258369 dsideal_db ' + tableStrs + '| gzip > /usr/local/' + gzName
# 连接远程主机进行操作
# 实例化ssh客户端
ssh = paramiko.SSHClient()
# 创建默认的白名单
policy = paramiko.AutoAddPolicy()
# 设置白名单
ssh.set_missing_host_key_policy(policy)
# 链接服务器
ssh.connect(
hostname="10.10.14.187", # 服务器的ip
port=26611, # 服务器的端口
username="root", # 服务器的用户名
password="dsideal" # 用户名对应的密码
)
# 远程执行命令
stdin, stdout, stderr = ssh.exec_command(cmd)
result = stdout.read().decode()
# 下载回来
scp_client = SCPClient(ssh.get_transport(), socket_timeout=15.0)
scp_client.get('/usr/local/'+gzName, "..\\Sql\\" + gzName)
# 关闭远程主机
ssh.close()
# 关闭数据库
db.close()
# 提示
printf("恭喜,文件备份成功!")