Skip to content

Commit

Permalink
优化远程数据库对本地MySQL的依赖
Browse files Browse the repository at this point in the history
  • Loading branch information
showpy committed Jan 14, 2022
1 parent 419206a commit a3da1b7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 25 deletions.
14 changes: 10 additions & 4 deletions BTPanel/templates/default/database.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@
<script type="text/javascript" src="{{g.cdn_url}}/js/database.js?date={{g['version']}}&repair={{data['js_random']}}"></script>
<script type="text/javascript">
bt.set_cookie('backup_path', "{{session['config']['backup_path']}}");
{% if not data['isSetup'] %}
layer.msg('{{data["lan"]["JS1"]}}<a href="/soft#i" style="color:#20a53a;float: right;">{{data["lan"]["JS2"]}}</a>',{icon:7,shade: [0.3, '#000'],time:0});
$(".layui-layer-shade").css("margin-left", "180px");
{% else %}
{% if not data['isSetup'] %}
// layer.msg('{{data["lan"]["JS1"]}}<a href="/soft#i" style="color:#20a53a;float: right;">{{data["lan"]["JS2"]}}</a>',{icon:7,shade: [0.3, '#000'],time:0});
// $(".layui-layer-shade").css("margin-left", "180px");
setTimeout(function(){
$("button[title='phpMyAdmin']").hide();
$("button[title='root密码']").hide();
$("button[title='回收站']").hide();
},500)

// {% else %}
// database.get_list();
{% endif %}
</script>
Expand Down
41 changes: 23 additions & 18 deletions class/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ def GetCloudServer(self,get):
@author hwliang<2021-01-10>
@return list
'''

data = public.M('database_servers').select()
bt_mysql_bin = '{}/mysql/bin/mysql'.format(public.get_setup_path())
if os.path.exists(bt_mysql_bin):
data.insert(0,{'id':0,'db_host':'127.0.0.1','db_port':3306,'db_user':'root','db_password':'','ps':'本地服务器','addtime':0})
return data


Expand Down Expand Up @@ -739,7 +741,7 @@ def ToBackup(self,get):
name = db_find['name']
fileName = name + '_' + time.strftime('%Y%m%d_%H%M%S',time.localtime()) + '.sql.gz'
backupName = session['config']['backup_path'] + '/database/' + fileName

mysqldump_bin = public.get_mysqldump_bin()
if db_find['db_type'] in ['0',0]:
# 本地数据库
result = panelMysql.panelMysql().execute("show databases")
Expand All @@ -752,7 +754,7 @@ def ToBackup(self,get):
try:
password = public.M('config').where('id=?',(1,)).getField('mysql_root')
os.environ["MYSQL_PWD"] = password
public.ExecShell("/www/server/mysql/bin/mysqldump -R -E --triggers=false --default-character-set="+ public.get_database_character(name) +" --force --opt \"" + name + "\" -u root | gzip > " + backupName)
public.ExecShell(mysqldump_bin + " -R -E --triggers=false --default-character-set="+ public.get_database_character(name) +" --force --opt \"" + name + "\" -u root | gzip > " + backupName)
except Exception as e:
raise
finally:
Expand All @@ -765,7 +767,7 @@ def ToBackup(self,get):
res = self.CheckCloudDatabase(conn_config)
if isinstance(res,dict): return res
os.environ["MYSQL_PWD"] = conn_config['db_password']
public.ExecShell("/www/server/mysql/bin/mysqldump -h "+ conn_config['db_host'] +" -P "+ str(int(conn_config['db_port'])) +" -R -E --triggers=false --default-character-set=" + public.get_database_character(name) + " --force --opt \"" + db_find['name'] + "\" -u "+ conn_config['db_user'] +" | gzip > " + backupName)
public.ExecShell(mysqldump_bin + " -h "+ conn_config['db_host'] +" -P "+ str(int(conn_config['db_port'])) +" -R -E --triggers=false --default-character-set=" + public.get_database_character(name) + " --force --opt \"" + db_find['name'] + "\" -u "+ conn_config['db_user'] +" | gzip > " + backupName)
except Exception as e:
raise
finally:
Expand All @@ -776,7 +778,7 @@ def ToBackup(self,get):
res = self.CheckCloudDatabase(conn_config)
if isinstance(res,dict): return res
os.environ["MYSQL_PWD"] = conn_config['db_password']
public.ExecShell("/www/server/mysql/bin/mysqldump -h "+ conn_config['db_host'] +" -P "+ str(int(conn_config['db_port'])) +" -R -E --triggers=false --default-character-set=" + public.get_database_character(name) + " --force --opt \"" + db_find['name'] + "\" -u "+ conn_config['db_user'] +" | gzip > " + backupName)
public.ExecShell(mysqldump_bin + " -h "+ conn_config['db_host'] +" -P "+ str(int(conn_config['db_port'])) +" -R -E --triggers=false --default-character-set=" + public.get_database_character(name) + " --force --opt \"" + db_find['name'] + "\" -u "+ conn_config['db_user'] +" | gzip > " + backupName)
except Exception as e:
raise
finally:
Expand Down Expand Up @@ -817,9 +819,7 @@ def DelBackup(self,get):
#导入
def InputSql(self,get):
#try:
result = panelMysql.panelMysql().execute("show databases")
isError=self.IsSqlError(result)
if isError: return isError

name = get['name']
file = get['file']
root = public.M('config').where('id=?',(1,)).getField('mysql_root')
Expand All @@ -829,7 +829,12 @@ def InputSql(self,get):
if ext not in exts:
return public.returnMsg(False, 'DATABASE_INPUT_ERR_FORMAT')
db_find = public.M('databases').where('name=?',name).find()
mysql_obj = public.get_mysql_obj_by_sid(db_find['sid'])
result =mysql_obj.execute("show databases")
isError=self.IsSqlError(result)
if isError: return isError
isgzip = False
mysql_bin = public.get_mysql_bin()
if ext != 'sql':
tmp = file.split('/')
tmpFile = tmp[len(tmp)-1]
Expand Down Expand Up @@ -862,15 +867,15 @@ def InputSql(self,get):
if db_find['db_type'] in ['0',0]:
password = public.M('config').where('id=?',(1,)).getField('mysql_root')
os.environ["MYSQL_PWD"] = password
public.ExecShell(public.GetConfigValue('setup_path') + "/mysql/bin/mysql -uroot -p" + root + " --force \"" + name + "\" < " +'"'+ input_path +'"')
public.ExecShell(mysql_bin + " -uroot -p" + root + " --force \"" + name + "\" < " +'"'+ input_path +'"')
elif db_find['db_type'] in ['1',1]:
conn_config = json.loads(db_find['conn_config'])
os.environ["MYSQL_PWD"] = conn_config['db_password']
public.ExecShell(public.GetConfigValue('setup_path') + "/mysql/bin/mysql -h "+ conn_config['db_host'] +" -P "+str(int(conn_config['db_port']))+" -u"+conn_config['db_user']+" -p" + conn_config['db_password'] + " --force \"" + name + "\" < " +'"'+ input_path +'"')
public.ExecShell(mysql_bin + " -h "+ conn_config['db_host'] +" -P "+str(int(conn_config['db_port']))+" -u"+conn_config['db_user']+" -p" + conn_config['db_password'] + " --force \"" + name + "\" < " +'"'+ input_path +'"')
elif db_find['db_type'] in ['2',2]:
conn_config = public.M('database_servers').where('id=?',db_find['sid']).find()
os.environ["MYSQL_PWD"] = conn_config['db_password']
public.ExecShell(public.GetConfigValue('setup_path') + "/mysql/bin/mysql -h "+ conn_config['db_host'] +" -P "+str(int(conn_config['db_port']))+" -u"+conn_config['db_user']+" -p" + conn_config['db_password'] + " --force \"" + name + "\" < " +'"'+ input_path +'"')
public.ExecShell(mysql_bin + " -h "+ conn_config['db_host'] +" -P "+str(int(conn_config['db_port']))+" -u"+conn_config['db_user']+" -p" + conn_config['db_password'] + " --force \"" + name + "\" < " +'"'+ input_path +'"')
except Exception as e:
raise
finally:
Expand All @@ -885,15 +890,15 @@ def InputSql(self,get):
if db_find['db_type'] in ['0',0]:
password = public.M('config').where('id=?',(1,)).getField('mysql_root')
os.environ["MYSQL_PWD"] = password
public.ExecShell(public.GetConfigValue('setup_path') + "/mysql/bin/mysql -uroot -p" + root + " --force \"" + name + "\" < " +'"'+ file +'"')
public.ExecShell(mysql_bin + " -uroot -p" + root + " --force \"" + name + "\" < " +'"'+ file +'"')
elif db_find['db_type'] in ['1',1]:
conn_config = json.loads(db_find['conn_config'])
os.environ["MYSQL_PWD"] = conn_config['db_password']
public.ExecShell(public.GetConfigValue('setup_path') + "/mysql/bin/mysql -h "+ conn_config['db_host'] +" -P "+str(int(conn_config['db_port']))+" -u"+conn_config['db_user']+" -p" + conn_config['db_password'] + " --force \"" + name + "\" < " +'"'+ file +'"')
public.ExecShell(mysql_bin + " -h "+ conn_config['db_host'] +" -P "+str(int(conn_config['db_port']))+" -u"+conn_config['db_user']+" -p" + conn_config['db_password'] + " --force \"" + name + "\" < " +'"'+ file +'"')
elif db_find['db_type'] in ['2',2]:
conn_config = public.M('database_servers').where('id=?',db_find['sid']).find()
os.environ["MYSQL_PWD"] = conn_config['db_password']
public.ExecShell(public.GetConfigValue('setup_path') + "/mysql/bin/mysql -h "+ conn_config['db_host'] +" -P "+str(int(conn_config['db_port']))+" -u"+conn_config['db_user']+" -p" + conn_config['db_password'] + " --force \"" + name + "\" < " +'"'+ file +'"')
public.ExecShell(mysql_bin + " -h "+ conn_config['db_host'] +" -P "+str(int(conn_config['db_port']))+" -u"+conn_config['db_user']+" -p" + conn_config['db_password'] + " --force \"" + name + "\" < " +'"'+ file +'"')
except Exception as e:
raise
finally:
Expand All @@ -907,16 +912,16 @@ def InputSql(self,get):

#同步数据库到服务器
def SyncToDatabases(self,get):
result = panelMysql.panelMysql().execute("show databases")
isError=self.IsSqlError(result)
if isError: return isError
# result = panelMysql.panelMysql().execute("show databases")
# isError=self.IsSqlError(result)
# if isError: return isError
type = int(get['type'])
n = 0
sql = public.M('databases')
if type == 0:
data = sql.field('id,sid,name,username,password,accept,db_type').select()
for value in data:
if value['db_type'] not in ['1',1]:
if value['db_type'] in ['1',1]:
continue # 跳过远程数据库
result = self.ToDataBase(value)
if result == 1: n +=1
Expand Down
5 changes: 3 additions & 2 deletions class/panelBackup.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,16 +556,17 @@ def backup_database(self,db_name,dfile = None,save=3):
if os.path.exists(dfile):
os.remove(dfile)
#self.mypass(True)
mysqldump_bin = public.get_mysqldump_bin()
try:
if not is_cloud_db:
# 本地数据库 @author hwliang<2021-01-08>
password = public.M('config').where('id=?',(1,)).getField('mysql_root')
os.environ["MYSQL_PWD"] = password
backup_cmd = "/www/server/mysql/bin/mysqldump -E -R --default-character-set="+ character +" --force --hex-blob --opt " + db_name + " -u root" + " 2>"+self._err_log+"| gzip > " + dfile
backup_cmd = mysqldump_bin + " -E -R --default-character-set="+ character +" --force --hex-blob --opt " + db_name + " -u root" + " 2>"+self._err_log+"| gzip > " + dfile
else:
# 远程数据库 @author hwliang<2021-01-08>
os.environ["MYSQL_PWD"] = conn_config['db_password']
backup_cmd = "/www/server/mysql/bin/mysqldump -h " + conn_config['db_host'] + " -P " + conn_config['db_port'] + " -E -R --default-character-set="+ character +" --force --hex-blob --opt " + db_name + " -u " + conn_config['db_user'] + " 2>"+self._err_log+"| gzip > " + dfile
backup_cmd = mysqldump_bin + " -h " + conn_config['db_host'] + " -P " + conn_config['db_port'] + " -E -R --default-character-set="+ character +" --force --hex-blob --opt " + db_name + " -u " + conn_config['db_user'] + " 2>"+self._err_log+"| gzip > " + dfile
public.ExecShell(backup_cmd)
except Exception as e:
raise
Expand Down
42 changes: 41 additions & 1 deletion class/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -3459,6 +3459,7 @@ def get_plugin_main_object(plugin_name,sys_path):
os_file = sys_path + '/' + plugin_name + '_main.so'
php_file = sys_path + '/index.php'
is_php = False
plugin_obj = None
if os.path.exists(os_file): # 是否为编译后的so文件
plugin_obj = __import__(plugin_name + '_main')
elif os.path.exists(php_file): # 是否为PHP代码
Expand Down Expand Up @@ -4067,4 +4068,43 @@ def get_full_session_file():
from BTPanel import app
full_session_key = app.config['SESSION_KEY_PREFIX'] + get_session_id()
sess_path = get_panel_path() + '/data/session/'
return sess_path + '/' + md5(full_session_key)
return sess_path + '/' + md5(full_session_key)

def get_mysqldump_bin():
'''
@name 获取mysqldump路径
@author hwliang<2022-01-14>
@return string
'''
bin_files = [
'{}/mysql/bin/mysqldump'.format(get_panel_path()),
'/usr/bin/mysqldump',
'/usr/local/bin/mysqldump',
'/usr/sbin/mysqldump',
'/usr/local/sbin/mysqldump'
]

for bin_file in bin_files:
if os.path.exists(bin_file):
return bin_file
return bin_files[0]

def get_mysql_bin():
'''
@name 获取mysql路径
@author hwliang<2022-01-14>
@return string
'''
bin_files = [
'{}/mysql/bin/mysql'.format(get_panel_path()),
'/usr/bin/mysql',
'/usr/local/bin/mysql',
'/usr/sbin/mysql',
'/usr/local/sbin/mysql'
]

for bin_file in bin_files:
if os.path.exists(bin_file):
return bin_file
return bin_files[0]

0 comments on commit a3da1b7

Please sign in to comment.