From a72046462c5970a0ae16846de52cef248b2bbe92 Mon Sep 17 00:00:00 2001 From: midoks Date: Fri, 28 Oct 2022 17:49:30 +0800 Subject: [PATCH] Update index.py --- plugins/mysql/index.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/plugins/mysql/index.py b/plugins/mysql/index.py index b325f85b6..ab64dac2b 100755 --- a/plugins/mysql/index.py +++ b/plugins/mysql/index.py @@ -842,35 +842,48 @@ def importDbExternal(): if not os.path.exists(file_path): return mw.returnJson(False, '文件突然消失?') + exts = ['sql', 'gz', 'zip'] + tmp = file.split('.') + ext = tmp[len(tmp) - 1] + if ext not in exts: + return mw.returnJson(False, '导入数据库格式不对!') + + tmp = file.split('/') + tmpFile = tmp[len(tmp) - 1] + tmpFile = tmpFile.replace('.sql.' + ext, '.sql') + tmpFile = tmpFile.replace('.' + ext, '.sql') + tmpFile = tmpFile.replace('tar.', '') + + # print(tmpFile) import_sql = "" if file.find("sql.gz") > -1: cmd = 'cd ' + import_dir + ' && gzip -dc ' + \ - file + " > " + import_dir + "tmp.sql" + file + " > " + import_dir + tmpFile info = mw.execShell(cmd) if info[1] == "": - import_sql = import_dir + "tmp.sql" + import_sql = import_dir + tmpFile if file.find(".zip") > -1: cmd = 'cd ' + import_dir + ' && unzip -o ' + file mw.execShell(cmd) + import_sql = import_dir + tmpFile if file.find("tar.gz") > -1: cmd = 'cd ' + import_dir + ' && tar -zxvf ' + file mw.execShell(cmd) - - if import_sql == "": - cmd = 'cd ' + import_dir + ' && ls && grep ".sql$"' - sql_name = mw.execShell(cmd) - import_sql = import_dir + sql_name[0] + import_sql = import_dir + tmpFile if import_sql == "": return mw.returnJson(False, '未找SQL文件') pwd = pSqliteDb('config').where('id=?', (1,)).getField('mysql_root') sock = getSocketFile() + + os.environ["MYSQL_PWD"] = pwd mysql_cmd = getServerDir() + '/bin/mysql -S ' + sock + ' -uroot -p' + \ pwd + ' ' + name + ' < ' + import_sql + # print(mysql_cmd) os.system(mysql_cmd) os.remove(import_sql)