- 配置文件
配置使用csv,俩文件,一个被备份的数据库机器ip,用户名,密码,数据名称,数据库用户,密码,数据库名称信息,称为source.csv,另一个目标数据库机器ip,用户名,密码,数据名称,数据库用户,密码,数据库名称,称为destination.csv
如:
source.csv
192.168.3.161, root, 123456, root, 123456, db_1
destination.csv
192.168.3.162, root, 123456, root, 123456, db_1
意思是将192.168.3.161的db1数据库迁移到192.168.3.162的db1数据库
- 读取配置文件ReadCSV.py
def read(file):
config_ = []
with open(file, encoding='utf8') as f:
f_csv = csv.reader(f)
# headers = next(f_csv)
for row in f_csv:
config_.append(row)
return config_
- 执行远程命令SSH.py
# 返回一个字符串,单个命令,返回该命令的远程输出
def ssh(ip, user_name, pass_wd, cmd):
result_str = ""
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(ip, 22, user_name, pass_wd, timeout=5)
print ("执行远程命令:服务器ip:%s,命令:%s" %(ip,cmd))
std_in, stdout, stderr = ssh_client.exec_command(cmd)
# stdin.write("Y") #简单交互,输入 ‘Y’
out = stdout.readlines()
err = stderr.readline()
# 屏幕输出
for o in out:
result_str += o
print("remoteOut:"+o)
# print('%s\t执行完毕\n' % ip)
# print("result_str:"+result_str)
ssh_client.close()
except Exception as e:
print('%s\tError:%s\n' % (ip, e))
return result_str
def upload(host_ip, username, password, local_path, remote_path):
t = paramiko.Transport((host_ip, 22))
t.connect(username=username, password=password) # 登录远程服务器
sftp = paramiko.SFTPClient.from_transport(t) # sftp传输协议
sftp.put(local_path, remote_path)
t.close()
def download(host_ip, username, password, remote_path, local_path):
t = paramiko.Transport((host_ip, 22))
t.connect(username=username, password=password) # 登录远程服务器
sftp = paramiko.SFTPClient.from_transport(t) # sftp传输协议
src = remote_path
des = local_path
sftp.get(src, des)
t.close()
- 迁移数据库DataMigration.py
__author__ = 'micocube'
# python 3.5
# DataMigration.py,SSH.py,ReadCSV.py在同一目录下
from . import SSH
from . import ReadCSV
# dump数据库
# 被备份的数据库远程服务器ip,用户名,密码,mysql用户名,密码,数据库名称
# mysqldump -B选项会生成建库语句,生成的sql文件放到/data/数据库名称.sql
def dump_data_base(ip, user_name, password, mysql_user, mysql_password, db_name):
res_str = SSH.ssh(ip,user_name,password,"mysqldump -h" + ip + " -u" + mysql_user +" -p" + mysql_password + " -B " + db_name + " > /data/" + db_name + ".sql")
print(res_str)
# 拷贝数据库到本地,如果直接scp的话要输入密码
# 被备份的数据库远程服务器ip,用户名,密码,数据库名称
# 数据库名称主要用来拼接sql文件名称
def copy_data(des_ip, des_user_name, des_password, db_name):
SSH.download(des_ip, des_user_name, des_password, "/data/"+db_name + ".sql","resources/" + db_name + ".sql")
# 上传数据库文件到迁移的目标机器
# 目标远程服务器ip,用户名,密码,数据库名称
# 数据库名称主要用来拼接sql文件名称
def upload_data(des_ip, des_user_name, des_password, db_name):
SSH.upload(des_ip, des_user_name, des_password, "resources/" + db_name + ".sql", "/data/" + db_name + ".sql")
# 导入数据库到目标机器的数据库
# logs是个空数据库,mysqldump 已经生成了建库语句,对logs数据库并无影响
# 目标远程服务器ip,用户名,密码,数据库用户名,数据库密码,数据库名称
def import_data_base(ip, user_name, password, mysql_user, mysql_password, db_name):
res_str = SSH.ssh(ip,user_name,password,"mysql -h" + ip + " -u" + mysql_user +" -p" + mysql_password + " logs " + " < /data/" + db_name + ".sql")
print(res_str)
def read_config():
# 在代码目录建立了一个叫resources的文件夹,存放配置文件,
source = ReadCSV.read("resources/source.csv")
destination = ReadCSV.read("resources/destination.csv")
return source,destination
if __name__ == "__main__":
source, destination = read_config()
for i in range(0,len(source)):
# 被备份的数据库机器ip,用户名,密码,数据名称,数据库用户,密码,数据库名称
# '192.168.3.161', 'root', '123456', 'root', '123456', 'db_1'
sou = source[i]
# 目标数据库机器ip,用户名,密码,数据名称,数据库用户,密码,数据库名称
# '192.168.3.162', 'root', '123456', 'root', '123456', 'db_1'
des = destination[i]
# 将192.168.3.161的db1数据库迁移到192.168.3.162的db1数据库
# 如果161上已经有db_1的数据库,可以远程执行mysqladmin -u**** -p*** create db_2 先建库,dump的时候不加-B选项
dump_data_base(sou[0],sou[1],sou[2],sou[3],sou[4],sou[5])
copy_data(sou[0], sou[1], sou[2], sou[5])
# 删除被备份的sql
SSH.ssh(sou[0], sou[1], sou[2], "rm -f /data/" + sou[5] + ".sql")
upload_data(des[0], des[1], des[2], des[5])
import_data_base(des[0], des[1], des[2], des[3], des[4], des[5])
SSH.ssh(des[0], des[1], des[2], "rm -f /data/" + sou[5] + ".sql")
print("source:"+str(sou) + "destination:" + str(des))