|
|
@ -123,14 +123,17 @@ class classApi: |
|
|
|
except Exception as e: |
|
|
|
except Exception as e: |
|
|
|
return result |
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
def sendPlugins(self, name, func, args): |
|
|
|
def sendPlugins(self, name, func, args, timeout=36000): |
|
|
|
url = '/plugins/run' |
|
|
|
url = '/plugins/run' |
|
|
|
|
|
|
|
|
|
|
|
data = {} |
|
|
|
data = {} |
|
|
|
data['name'] = name |
|
|
|
data['name'] = name |
|
|
|
data['func'] = func |
|
|
|
data['func'] = func |
|
|
|
data['args'] = json.dumps(args).replace(": ", ":").replace(", ", ",") |
|
|
|
data['args'] = json.dumps(args).replace(": ", ":").replace(", ", ",") |
|
|
|
return self.send(url, data) |
|
|
|
r = self.send(url, data, timeout) |
|
|
|
|
|
|
|
if r['status']: |
|
|
|
|
|
|
|
return json.loads(r['data']) |
|
|
|
|
|
|
|
return r |
|
|
|
|
|
|
|
|
|
|
|
def get_mode_and_user(self, path): |
|
|
|
def get_mode_and_user(self, path): |
|
|
|
'''取文件或目录权限信息''' |
|
|
|
'''取文件或目录权限信息''' |
|
|
@ -230,7 +233,7 @@ class classApi: |
|
|
|
except Exception as e: |
|
|
|
except Exception as e: |
|
|
|
times = time.time() - start_time |
|
|
|
times = time.time() - start_time |
|
|
|
total_time += times |
|
|
|
total_time += times |
|
|
|
ex = str(ex) |
|
|
|
ex = str(e) |
|
|
|
if ex.find('Read timed out') != -1 or ex.find('Connection aborted') != -1: |
|
|
|
if ex.find('Read timed out') != -1 or ex.find('Connection aborted') != -1: |
|
|
|
# 发生超时的时候尝试调整分片大小, 以确保网络情况不好的时候能继续上传 |
|
|
|
# 发生超时的时候尝试调整分片大小, 以确保网络情况不好的时候能继续上传 |
|
|
|
self._buff_size = int(self._buff_size / 2) |
|
|
|
self._buff_size = int(self._buff_size / 2) |
|
|
@ -487,17 +490,15 @@ class classApi: |
|
|
|
pdata['codeing'] = dbInfo['character'] |
|
|
|
pdata['codeing'] = dbInfo['character'] |
|
|
|
|
|
|
|
|
|
|
|
result = self.sendPlugins('mysql', 'add_db', pdata) |
|
|
|
result = self.sendPlugins('mysql', 'add_db', pdata) |
|
|
|
rdata = json.loads(result['data']) |
|
|
|
if result['status']: |
|
|
|
|
|
|
|
|
|
|
|
if rdata['status']: |
|
|
|
|
|
|
|
return True |
|
|
|
return True |
|
|
|
err_msg = '数据库[{}]创建失败,{}'.format(dbInfo['name'], rdata['msg']) |
|
|
|
err_msg = '数据库[{}]创建失败,{}'.format(dbInfo['name'], result['msg']) |
|
|
|
self.state('databases', index, -1, err_msg) |
|
|
|
self.state('databases', index, -1, err_msg) |
|
|
|
self.error(err_msg) |
|
|
|
self.error(err_msg) |
|
|
|
return False |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
# 数据库密码处理 |
|
|
|
# 数据库密码处理 |
|
|
|
def mypass(self, act, root): |
|
|
|
def myPass(self, act, root): |
|
|
|
# conf_file = '/etc/my.cnf' |
|
|
|
# conf_file = '/etc/my.cnf' |
|
|
|
conf_file = self.getConf('mysql') |
|
|
|
conf_file = self.getConf('mysql') |
|
|
|
mw.execShell("sed -i '/user=root/d' {}".format(conf_file)) |
|
|
|
mw.execShell("sed -i '/user=root/d' {}".format(conf_file)) |
|
|
@ -514,6 +515,20 @@ class classApi: |
|
|
|
return True |
|
|
|
return True |
|
|
|
return True |
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def recognizeDbMode(self): |
|
|
|
|
|
|
|
conf = self.getConf('mysql') |
|
|
|
|
|
|
|
con = mw.readFile(conf) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
path = mw.getServerDir() + '/mysql' |
|
|
|
|
|
|
|
rep = r"!include %s/(.*)?\.cnf" % (path + "/etc/mode",) |
|
|
|
|
|
|
|
mode = 'none' |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
data = re.findall(rep, con, re.M) |
|
|
|
|
|
|
|
mode = data[0] |
|
|
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
return mode |
|
|
|
|
|
|
|
|
|
|
|
def export_database(self, name, index): |
|
|
|
def export_database(self, name, index): |
|
|
|
self.write_speed('done', '正在导出数据库') |
|
|
|
self.write_speed('done', '正在导出数据库') |
|
|
|
write_log("|-正在导出数据库{}...".format(name)) |
|
|
|
write_log("|-正在导出数据库{}...".format(name)) |
|
|
@ -539,12 +554,20 @@ class classApi: |
|
|
|
|
|
|
|
|
|
|
|
root_dir = mw.getServerDir() + '/mysql' |
|
|
|
root_dir = mw.getServerDir() + '/mysql' |
|
|
|
my_cnf = self.getConf('mysql') |
|
|
|
my_cnf = self.getConf('mysql') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mode = self.recognizeDbMode() |
|
|
|
|
|
|
|
gtid_option = '' |
|
|
|
|
|
|
|
if mode == 'gtid': |
|
|
|
|
|
|
|
gtid_option = ' --set-gtid-purged=off ' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.myPass(True, root) |
|
|
|
cmd = root_dir + "/bin/mysqldump --defaults-file=" + my_cnf + " --default-character-set=" + \ |
|
|
|
cmd = root_dir + "/bin/mysqldump --defaults-file=" + my_cnf + " --default-character-set=" + \ |
|
|
|
self.getDatabaseCharacter( |
|
|
|
self.getDatabaseCharacter( |
|
|
|
name) + " --force --opt \"" + name + "\" | gzip > " + backup_name |
|
|
|
name) + gtid_option + " --force --opt \"" + name + "\" | gzip > " + backup_name |
|
|
|
|
|
|
|
# print(cmd) |
|
|
|
mw.execShell(cmd) |
|
|
|
mw.execShell(cmd) |
|
|
|
|
|
|
|
|
|
|
|
self.mypass(False, root) |
|
|
|
self.myPass(False, root) |
|
|
|
if not os.path.exists(backup_name) or os.path.getsize(backup_name) < 30: |
|
|
|
if not os.path.exists(backup_name) or os.path.getsize(backup_name) < 30: |
|
|
|
if os.path.exists(backup_name): |
|
|
|
if os.path.exists(backup_name): |
|
|
|
os.remove(backup_name) |
|
|
|
os.remove(backup_name) |
|
|
@ -559,27 +582,33 @@ class classApi: |
|
|
|
def send_database(self, dbInfo, index): |
|
|
|
def send_database(self, dbInfo, index): |
|
|
|
# print(dbInfo) |
|
|
|
# print(dbInfo) |
|
|
|
# 创建远程库 |
|
|
|
# 创建远程库 |
|
|
|
# if not self.create_database(dbInfo, index): |
|
|
|
if not self.create_database(dbInfo, index): |
|
|
|
# return False |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
self.create_database(dbInfo, index) |
|
|
|
# self.create_database(dbInfo, index) |
|
|
|
filename = self.export_database(dbInfo['name'], index) |
|
|
|
filename = self.export_database(dbInfo['name'], index) |
|
|
|
if not filename: |
|
|
|
if not filename: |
|
|
|
return False |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
db_dir = '/www/backup/database' |
|
|
|
db_dir = '/www/backup/import' |
|
|
|
upload_file = db_dir + '/psync_import_{}.sql.gz'.format(dbInfo['name']) |
|
|
|
new_db_name = 'psync_import_{}.sql.gz'.format(dbInfo['name']) |
|
|
|
d = self.send('/files/exec_shell', |
|
|
|
upload_file = db_dir + '/' + new_db_name |
|
|
|
{"shell": "rm -f " + upload_file, "path": "/www"}, 30) |
|
|
|
self.send('/files/exec_shell', |
|
|
|
|
|
|
|
{"shell": "rm -f " + upload_file, "path": "/www"}, 30) |
|
|
|
|
|
|
|
|
|
|
|
print(d) |
|
|
|
|
|
|
|
if self.upload_file(filename, upload_file): |
|
|
|
if self.upload_file(filename, upload_file): |
|
|
|
|
|
|
|
|
|
|
|
self.write_speed('done', '正在导入数据库') |
|
|
|
self.write_speed('done', '正在导入数据库') |
|
|
|
write_log("|-正在导入数据库{}...".format(dbInfo['name'])) |
|
|
|
write_log("|-正在导入数据库{}...".format(dbInfo['name'])) |
|
|
|
print(filename) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t = self.sendPlugins('mysql', 'import_db_external', { |
|
|
|
|
|
|
|
"file": new_db_name, "name": dbInfo['name']}) |
|
|
|
|
|
|
|
# print(t) |
|
|
|
|
|
|
|
self.send('/files/exec_shell', |
|
|
|
|
|
|
|
{"shell": "rm -f " + upload_file, "path": "/www"}, 30) |
|
|
|
|
|
|
|
return True |
|
|
|
self.state('databases', index, -1, "数据传输失败") |
|
|
|
self.state('databases', index, -1, "数据传输失败") |
|
|
|
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def sync_database(self): |
|
|
|
def sync_database(self): |
|
|
|
data = getCfgData() |
|
|
|
data = getCfgData() |
|
|
|