mirror of
https://github.com/aaPanel/aaPanel.git
synced 2026-09-10 03:27:39 +02:00
Update to v7.43.0
This commit is contained in:
@@ -1,30 +1,43 @@
|
||||
# coding: utf-8
|
||||
import os, sys, time, json
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
panelPath = '/www/server/panel'
|
||||
os.chdir(panelPath)
|
||||
if not panelPath + "/class/" in sys.path:
|
||||
sys.path.insert(0, panelPath + "/class/")
|
||||
import public, re
|
||||
from public.exceptions import HintException
|
||||
|
||||
|
||||
class databaseBase:
|
||||
|
||||
def get_base_list(self, args, sql_type='mysql'):
|
||||
"""
|
||||
@获取数据库列表
|
||||
@type:数据库类型,MySQL,SQLServer
|
||||
"""
|
||||
|
||||
search = ''
|
||||
if 'search' in args: search = args['search']
|
||||
|
||||
SQL = public.M('databases');
|
||||
conditions = ''
|
||||
if '_' in search:
|
||||
cs = ''
|
||||
for i in search:
|
||||
if i == '_':
|
||||
cs += '/_'
|
||||
else:
|
||||
cs += i
|
||||
search = cs
|
||||
conditions = " escape '/'"
|
||||
|
||||
SQL = public.M('databases')
|
||||
|
||||
where = "lower(type) = lower('{}')".format(sql_type)
|
||||
if search:
|
||||
where += "AND (name like '%{search}%' or ps like '%{search}%')".format(search=search)
|
||||
|
||||
where += "AND (name like '%{search}%' or ps like '%{search}%'{conditions})".format(search=search,
|
||||
conditions=conditions)
|
||||
if 'db_type' in args:
|
||||
where += " AND db_type='{}'".format(args['db_type'])
|
||||
|
||||
@@ -40,21 +53,21 @@ class databaseBase:
|
||||
info['p'] = 1
|
||||
info['row'] = 20
|
||||
result = '1,2,3,4,5,8'
|
||||
info['count'] = SQL.where(where, ()).count();
|
||||
info['count'] = SQL.where(where, ()).count()
|
||||
|
||||
if hasattr(args, 'limit'): info['row'] = int(args.limit)
|
||||
if hasattr(args, 'result'): result = args.result;
|
||||
if hasattr(args, 'result'): result = args.result
|
||||
if hasattr(args, 'p'): info['p'] = int(args['p'])
|
||||
|
||||
import page
|
||||
# 实例化分页类
|
||||
page = page.Page();
|
||||
page = page.Page()
|
||||
|
||||
info['uri'] = args
|
||||
info['return_js'] = ''
|
||||
if hasattr(args, 'tojs'): info['return_js'] = args.tojs
|
||||
|
||||
rdata['where'] = where;
|
||||
rdata['where'] = where
|
||||
|
||||
# 获取分页数据
|
||||
rdata['page'] = page.GetPage(info, result)
|
||||
@@ -63,18 +76,27 @@ class databaseBase:
|
||||
'id,sid,pid,name,username,password,accept,ps,addtime,type,db_type,conn_config').limit(
|
||||
str(page.SHIFT) + ',' + str(page.ROW)).select()
|
||||
|
||||
for sdata in rdata['data']:
|
||||
sdata['backup_count'] = public.M('backup').where("pid=? AND type=1", (sdata['id'])).count()
|
||||
if type(rdata['data']) == str:
|
||||
raise HintException("Database query error: " + rdata['data'])
|
||||
|
||||
for sdata in rdata['data']:
|
||||
# 清除不存在的
|
||||
backup_count = 0
|
||||
backup_list = public.M('backup').where("pid=? AND type=1", (sdata['id'])).select()
|
||||
for backup in backup_list:
|
||||
if not os.path.exists(backup["filename"]):
|
||||
public.M('backup').where("id=? AND type=1", (backup['id'])).delete()
|
||||
continue
|
||||
backup_count += 1
|
||||
sdata['backup_count'] = backup_count
|
||||
sdata['conn_config'] = json.loads(sdata['conn_config'])
|
||||
return rdata;
|
||||
return rdata
|
||||
|
||||
def get_databaseModel(self):
|
||||
'''
|
||||
获取数据库模型对象
|
||||
@db_type 数据库类型
|
||||
'''
|
||||
# from panelDatabaseController import DatabaseController
|
||||
from panelDatabaseControllerV2 import DatabaseController
|
||||
|
||||
project_obj = DatabaseController()
|
||||
@@ -116,8 +138,10 @@ class databaseBase:
|
||||
get['data'] = {'db_id': x['id']}
|
||||
get['mod_name'] = x['type'].lower()
|
||||
get['def_name'] = 'get_database_size_by_id'
|
||||
|
||||
x['total'] = p.model(get)
|
||||
try:
|
||||
x['total'] = p.model(get)["message"]["result"]
|
||||
except:
|
||||
x['total'] = 0
|
||||
except:
|
||||
x['total'] = 0
|
||||
result[x['name']] = x
|
||||
@@ -128,9 +152,11 @@ class databaseBase:
|
||||
"""
|
||||
@删除数据库前置检测
|
||||
"""
|
||||
if not hasattr(get, 'ids'):
|
||||
raise HintException("Parameter 'ids' is required for deletion.")
|
||||
ids = json.loads(get.ids)
|
||||
slist = {};
|
||||
result = [];
|
||||
slist = {}
|
||||
result = []
|
||||
db_list_size = []
|
||||
db_data = self.get_database_size(ids)
|
||||
|
||||
@@ -159,7 +185,7 @@ class databaseBase:
|
||||
|
||||
return p.model(get)
|
||||
|
||||
def add_base_database(self, get):
|
||||
def add_base_database(self, get, dtype):
|
||||
"""
|
||||
@添加数据库前置检测
|
||||
@return username 用户名
|
||||
@@ -168,18 +194,19 @@ class databaseBase:
|
||||
"""
|
||||
data_name = get['name'].strip().lower()
|
||||
if self.check_recyclebin(data_name):
|
||||
return public.returnMsg(False, public.lang("Database [' + data_name + '] is already in recycle bin, please restore from recycle bin!"));
|
||||
return public.returnMsg(False, public.lang(
|
||||
"Database [' + data_name + '] is already in recycle bin, please restore from recycle bin!"))
|
||||
|
||||
if len(data_name) > 16:
|
||||
return public.returnMsg(False, public.lang("Database name cannot be more than 16 characters!"))
|
||||
|
||||
if not hasattr(get, 'db_user'): get.db_user = data_name;
|
||||
username = get.db_user.strip();
|
||||
if not hasattr(get, 'db_user'): get.db_user = data_name
|
||||
username = get.db_user.strip()
|
||||
checks = ['root', 'mysql', 'test', 'sys', 'panel_logs']
|
||||
if username in checks or len(username) < 1:
|
||||
return public.returnMsg(False, public.lang("Database username is invalid!"));
|
||||
return public.returnMsg(False, public.lang("Database username is invalid!"))
|
||||
if data_name in checks or len(data_name) < 1:
|
||||
return public.returnMsg(False, public.lang("Database name is invalid!"));
|
||||
return public.returnMsg(False, public.lang("Database name is invalid!"))
|
||||
|
||||
reg = r"^\w+$"
|
||||
if not re.match(reg, data_name):
|
||||
@@ -189,7 +216,8 @@ class databaseBase:
|
||||
if len(data_pwd) < 1:
|
||||
data_pwd = public.md5(str(time.time()))[0:8]
|
||||
|
||||
if public.M('databases').where("name=? or username=?", (data_name, username)).count():
|
||||
if public.M('databases').where("(name=? or username=?) AND LOWER(type)=LOWER(?)",
|
||||
(data_name, username, dtype)).count():
|
||||
return public.returnMsg(False, public.lang("Database exists!"))
|
||||
|
||||
res = {
|
||||
@@ -211,23 +239,22 @@ class databaseBase:
|
||||
filename = public.M('backup').where(where, (id,)).getField('filename')
|
||||
if os.path.exists(filename): os.remove(filename)
|
||||
|
||||
if filename == 'qiniu':
|
||||
name = public.M('backup').where(where, (id,)).getField('name');
|
||||
|
||||
public.ExecShell(public.get_run_python("[PYTHON] " + public.GetConfigValue(
|
||||
'setup_path') + '/panel/script/backup_qiniu.py delete_file ' + name))
|
||||
# if filename == 'qiniu':
|
||||
# name = public.M('backup').where(where, (id,)).getField('name')
|
||||
#
|
||||
# public.ExecShell(public.get_run_python("[PYTHON] " + public.GetConfigValue('setup_path') + '/panel/script/backup_qiniu.py delete_file ' + name))
|
||||
public.M('backup').where(where, (id,)).delete()
|
||||
public.WriteLog("TYPE_DATABASE", 'DATABASE_BACKUP_DEL_SUCCESS', (name, filename))
|
||||
public.return_message(0, 0, 'DEL_SUCCESS')
|
||||
return public.return_message(0, 0, 'DEL_SUCCESS')
|
||||
|
||||
# 检查是否在回收站
|
||||
def check_recyclebin(self, name):
|
||||
try:
|
||||
for n in os.listdir('{}/Recycle_bin'.format(public.get_soft_path())):
|
||||
if n.find('BTDB_' + name + '_t_') != -1: return True;
|
||||
return False;
|
||||
for n in os.listdir('{}/Recycle_bin'.format(public.get_setup_path())):
|
||||
if n.find('BTDB_' + name + '_t_') != -1: return True
|
||||
return False
|
||||
except:
|
||||
return False;
|
||||
return False
|
||||
|
||||
# map to list
|
||||
def map_to_list(self, map_obj):
|
||||
@@ -248,22 +275,19 @@ class databaseBase:
|
||||
for key in nlist:
|
||||
if not key in get:
|
||||
return public.return_message(-1, 0, public.lang("Parameter passing error, missing parameter {}!", key))
|
||||
return True
|
||||
return public.return_message(0, 0, "success")
|
||||
|
||||
def check_cloud_database(self, args):
|
||||
'''
|
||||
@检查远程数据库是否存在
|
||||
@conn_config param
|
||||
'''
|
||||
|
||||
p = self.get_databaseModel()
|
||||
|
||||
get = public.dict_obj()
|
||||
# get['db_name'] = 'localhost'
|
||||
get['data'] = args
|
||||
get['mod_name'] = args['type']
|
||||
get['def_name'] = 'check_cloud_database_status'
|
||||
# public.print_log("-------------------进入检测远程数据库是否存在: {}".format(p.model(get)))
|
||||
return p.model(get)
|
||||
|
||||
def AddBaseCloudServer(self, get):
|
||||
@@ -278,44 +302,17 @@ class databaseBase:
|
||||
@param type<string> 数据库类型,mysql/sqlserver/sqlite
|
||||
@return dict
|
||||
"""
|
||||
# mongodb {"db_host":"192.168.168.12","db_port":"27017","db_user":"root","db_password":"8thA5dgB8lr5ACfx","db_ps":"cecee","type":"mongodb"}
|
||||
# sqlserver {"db_host":"192.168.1.23","db_port":"1433","db_user":"sa","db_password":"MfyDytnjXBTD8e6x","db_ps":"666","type":"sqlserver"}
|
||||
|
||||
|
||||
arrs = ['db_host', 'db_port', 'db_user', 'db_password', 'db_ps', 'type']
|
||||
if get.type == 'redis':
|
||||
arrs = ['db_host', 'db_port', 'db_password', 'db_ps', 'type']
|
||||
# try:
|
||||
cRet = self.check_cloud_args(get, arrs)
|
||||
if isinstance(cRet, dict):
|
||||
return cRet
|
||||
|
||||
# try:
|
||||
get['db_name'] = None
|
||||
try:
|
||||
res = self.check_cloud_database(get)
|
||||
except BaseException as ex:
|
||||
# public.print_log("获取远程数据库状态00: {}".format(ex))
|
||||
return public.return_message(-1, 0, public.lang("Database connection failed"))
|
||||
|
||||
|
||||
# # mongodb 远程检测有问题 暂时跳过检测
|
||||
# if get.type != 'mongodb':
|
||||
# if res['message'].get('result', '') == '' or res['message'].get('result', '') == False:
|
||||
# return public.return_message(-1, 0, public.lang("The remote database could not be connected"))
|
||||
# {'status': 0, 'timestamp': 1715394490, 'message': AttributeError("'str' object has no attribute 'command'")}
|
||||
|
||||
# 检测数据库连接状态
|
||||
try:
|
||||
if not isinstance(res['message'], dict):
|
||||
return public.return_message(-1, 0, public.lang("The remote database could not be connected"))
|
||||
if res['message'].get('result', '') == '' or res['message'].get('result', '') == False:
|
||||
return public.return_message(-1, 0, public.lang("The remote database could not be connected"))
|
||||
if res['status'] == -1:
|
||||
return public.return_message(-1, 0, public.lang("The remote database could not be connected"))
|
||||
except Exception as e:
|
||||
# public.print_log("获取远程数据库状态22: {}".format(e))
|
||||
return public.return_message(-1, 0, public.lang("The remote database could not be connected"))
|
||||
res = self.check_cloud_database(get)
|
||||
if isinstance(res, dict):
|
||||
return res
|
||||
|
||||
if public.M('database_servers').where('db_host=? AND db_port=?', (get['db_host'], get['db_port'])).count():
|
||||
return public.return_message(-1, 0, 'The specified server already exists: [{}:{}]'.format(get['db_host'],
|
||||
@@ -334,12 +331,8 @@ class databaseBase:
|
||||
|
||||
if isinstance(result, int):
|
||||
public.WriteLog('Database manager', 'Add remote MySQL server[{}:{}]'.format(get['db_host'], get['db_port']))
|
||||
# return public.returnMsg(True, public.lang("Added successfully!"))
|
||||
return public.return_message(0, 0, public.lang("Added successfully!"))
|
||||
return public.return_message(0, 0, public.lang("Add failed: {}", result))
|
||||
# except Exception as ex:
|
||||
# public.print_log("error info777: {}".format(ex))
|
||||
# return public.return_message(-1, 0, str(ex))
|
||||
|
||||
def GetBaseCloudServer(self, get):
|
||||
'''
|
||||
@@ -366,7 +359,7 @@ class databaseBase:
|
||||
elif get['type'] == 'sqlserver':
|
||||
pass
|
||||
elif get['type'] == 'mongodb':
|
||||
if os.path.exists('/www/server/mongodb'):
|
||||
if os.path.exists('/www/server/mongodb/bin'):
|
||||
data.insert(0, {'id': 0, 'db_host': '127.0.0.1', 'db_port': 27017, 'db_user': 'root', 'db_password': '',
|
||||
'ps': 'local server', 'addtime': 0, 'db_type': 'mongodb'})
|
||||
elif get['type'] == 'redis':
|
||||
@@ -422,9 +415,7 @@ class databaseBase:
|
||||
cRet = self.check_cloud_args(get, arrs)
|
||||
if isinstance(cRet, dict):
|
||||
return cRet
|
||||
# if not cRet['status']:
|
||||
# return public.return_message(-1, 0,)
|
||||
# return cRet
|
||||
|
||||
get['db_name'] = None
|
||||
id = int(get.id)
|
||||
get['db_port'] = int(get['db_port'])
|
||||
@@ -438,21 +429,13 @@ class databaseBase:
|
||||
return public.return_message(-1, 0,
|
||||
'The specified server already exists: [{}:{}]'.format(get['db_host'],
|
||||
get['db_port']))
|
||||
|
||||
if db_find['db_user'] != get['db_user'] or db_find['db_password'] != get['db_password']:
|
||||
_modify = True
|
||||
_modify = True
|
||||
|
||||
if _modify:
|
||||
try:
|
||||
res = self.check_cloud_database(get)
|
||||
except BaseException as ex:
|
||||
# public.print_log("获取远程数据库链接状态报错: {}".format(ex))
|
||||
return public.return_message(-1, 0, public.lang("Database connection failed"))
|
||||
|
||||
if res['message'].get('result', '') == '' or res['message'].get('result', '') == False:
|
||||
return public.return_message(-1, 0, public.lang("The remote database could not be connected"))
|
||||
|
||||
res = self.check_cloud_database(get)
|
||||
if isinstance(res, dict): return res
|
||||
pdata = {
|
||||
'db_host': get['db_host'],
|
||||
'db_port': int(get['db_port']),
|
||||
@@ -480,13 +463,16 @@ class databaseBase:
|
||||
if "2002," in mysqlMsg:
|
||||
return public.return_message(-1, 0, public.lang("ERROR to connect database"))
|
||||
if "2003," in mysqlMsg:
|
||||
return public.return_message(-1, 0, public.lang("Database connection timed out, please check if the configuration is correct."))
|
||||
return public.return_message(-1, 0, public.lang(
|
||||
"Database connection timed out, please check if the configuration is correct."))
|
||||
if "1045," in mysqlMsg:
|
||||
return public.return_message(-1, 0, public.lang("MySQL password error."))
|
||||
if "1040," in mysqlMsg:
|
||||
return public.return_message(-1, 0, public.lang("Exceeded maximum number of connections, please try again later."))
|
||||
return public.return_message(-1, 0, public.lang(
|
||||
"Exceeded maximum number of connections, please try again later."))
|
||||
if "1130," in mysqlMsg:
|
||||
return public.return_message(-1, 0, public.lang("Database connection failed, please check whether the root user is authorized to access 127.0.0.1."))
|
||||
return public.return_message(-1, 0, public.lang(
|
||||
"Database connection failed, please check whether the root user is authorized to access 127.0.0.1."))
|
||||
if "using password:" in mysqlMsg:
|
||||
return public.return_message(-1, 0, public.lang("Database password is incorrect!"))
|
||||
if "Connection refused" in mysqlMsg:
|
||||
@@ -494,11 +480,14 @@ class databaseBase:
|
||||
if "1133" in mysqlMsg:
|
||||
return public.return_message(-1, 0, public.lang("Database user does NOT exist!"))
|
||||
if "2005_login_error" == mysqlMsg:
|
||||
return public.return_message(-1, 0, public.lang("The connection times out, please manually enable the TCP/IP function (Start Menu->SQL 2005->Configuration Tools->2005 Network Configuration->TCP/IP->Enable)"))
|
||||
return public.return_message(-1, 0, public.lang(
|
||||
"The connection times out, please manually enable the TCP/IP function (Start Menu->SQL 2005->Configuration Tools->2005 Network Configuration->TCP/IP->Enable)"))
|
||||
if 'already exists' in mysqlMsg:
|
||||
return public.return_message(-1, 0, public.lang("The specified database already exists, please do not add it repeatedly."))
|
||||
return public.return_message(-1, 0, public.lang(
|
||||
"The specified database already exists, please do not add it repeatedly."))
|
||||
if 'Cannot open backup device' in mysqlMsg:
|
||||
return public.return_message(-1, 0, public.lang("The operation failed, the remote database does not support the operation."))
|
||||
return public.return_message(-1, 0, public.lang(
|
||||
"The operation failed, the remote database does not support the operation."))
|
||||
|
||||
if '1142' in mysqlMsg:
|
||||
return public.return_message(-1, 0, public.lang("Insufficient permissions, please use root user."))
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,18 +1,25 @@
|
||||
#coding: utf-8
|
||||
#-------------------------------------------------------------------
|
||||
# coding: utf-8
|
||||
# -------------------------------------------------------------------
|
||||
# aaPanel
|
||||
#-------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------
|
||||
# Copyright (c) 2015-2099 aaPanel(www.aapanel.com) All rights reserved.
|
||||
#-------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------
|
||||
# Author: hwliang <hwl@aapanel.com>
|
||||
#-------------------------------------------------------------------
|
||||
# -------------------------------------------------------------------
|
||||
|
||||
import json
|
||||
# sqlite模型
|
||||
#------------------------------
|
||||
import os,re,json,shutil,time
|
||||
from databaseModelV2.base import databaseBase
|
||||
# ------------------------------
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import time
|
||||
from typing import Union
|
||||
|
||||
import public
|
||||
from databaseModelV2.base import databaseBase
|
||||
from public.validate import Param
|
||||
|
||||
try:
|
||||
import redis
|
||||
except:
|
||||
@@ -20,11 +27,13 @@ except:
|
||||
import redis
|
||||
try:
|
||||
from BTPanel import session
|
||||
except :pass
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
# 2025/6/6 同步国内
|
||||
|
||||
class panelRedisDB():
|
||||
|
||||
__DB_PASS = None
|
||||
__DB_USER = None
|
||||
__DB_PORT = 6379
|
||||
@@ -33,32 +42,41 @@ class panelRedisDB():
|
||||
__DB_ERR = None
|
||||
|
||||
__DB_CLOUD = None
|
||||
|
||||
def __init__(self):
|
||||
self.__config = self.get_options(None)
|
||||
self.error_message = ""
|
||||
|
||||
def redis_conn(self,db_idx = 0):
|
||||
|
||||
if self.__DB_HOST in ['127.0.0.1','localhost']:
|
||||
if not os.path.exists('/www/server/redis'): return False
|
||||
def redis_conn(self, db_idx=0):
|
||||
if self.__DB_HOST in ['127.0.0.1', 'localhost']:
|
||||
if not os.path.exists('/www/server/redis'):
|
||||
return False
|
||||
if self.__config == "Config Error":
|
||||
return False
|
||||
|
||||
if not self.__DB_CLOUD:
|
||||
self.__DB_PASS = self.__config['requirepass']
|
||||
self.__DB_PORT = int(self.__config['port'])
|
||||
|
||||
try:
|
||||
redis_pool = redis.ConnectionPool(host=self.__DB_HOST, port= self.__DB_PORT, password= self.__DB_PASS, db= db_idx)
|
||||
self.__DB_CONN = redis.Redis(connection_pool= redis_pool)
|
||||
redis_pool = redis.ConnectionPool(
|
||||
host=self.__DB_HOST, port=self.__DB_PORT, password=self.__DB_PASS, db=db_idx
|
||||
)
|
||||
self.__DB_CONN = redis.Redis(connection_pool=redis_pool)
|
||||
self.__DB_CONN.ping()
|
||||
return self.__DB_CONN
|
||||
except :
|
||||
self.__DB_ERR = public.get_error_info()
|
||||
except redis.exceptions.ConnectionError:
|
||||
return False
|
||||
except Exception:
|
||||
self.__DB_ERR = public.get_error_info()
|
||||
return False
|
||||
|
||||
|
||||
def set_host(self,host,port,name,username,password,prefix = ''):
|
||||
def set_host(self, host, port, name, username, password, prefix=''):
|
||||
self.__DB_HOST = host
|
||||
self.__DB_PORT = int(port)
|
||||
self.__DB_NAME = name
|
||||
if self.__DB_NAME: self.__DB_NAME = str(self.__DB_NAME)
|
||||
if self.__DB_NAME:
|
||||
self.__DB_NAME = str(self.__DB_NAME)
|
||||
self.__DB_USER = str(username)
|
||||
self._USER = str(username)
|
||||
self.__DB_PASS = str(password)
|
||||
@@ -66,67 +84,95 @@ class panelRedisDB():
|
||||
self.__DB_CLOUD = 1
|
||||
return self
|
||||
|
||||
|
||||
#获取配置项
|
||||
def get_options(self,get = None):
|
||||
# 获取配置项
|
||||
def get_options(self, get=None):
|
||||
import ipaddress
|
||||
|
||||
result = {}
|
||||
redis_conf = public.readFile("{}/redis/redis.conf".format(public.get_setup_path()))
|
||||
if not redis_conf: return False
|
||||
|
||||
keys = ["bind","port","timeout","maxclients","databases","requirepass","maxmemory"]
|
||||
for k in keys:
|
||||
v = ""
|
||||
rep = "\n%s\\s+(.+)" % k
|
||||
group = re.search(rep,redis_conf)
|
||||
if not group:
|
||||
if k == "maxmemory":
|
||||
v = "0"
|
||||
if k == "maxclients":
|
||||
v = "10000"
|
||||
if k == "requirepass":
|
||||
v = ""
|
||||
if not redis_conf:
|
||||
if not os.path.exists('/www/server/redis'):
|
||||
return False
|
||||
public.ExecShell("mv /www/server/redis/redis.conf /www/server/redis/redis.conf.bak")
|
||||
public.ExecShell(
|
||||
"wget -O /www/server/redis/redis.conf https://node.aapanel.com/conf/redis.conf;chmod 600 /www/server/redis/redis.conf;chown redis:redis /www/server/redis/redis.conf"
|
||||
)
|
||||
time.sleep(1)
|
||||
redis_conf = public.readFile("{}/redis/redis.conf".format(public.get_setup_path()))
|
||||
|
||||
keys = ["bind", "port", "timeout", "maxclients", "databases", "requirepass", "maxmemory"]
|
||||
defaults = ["127.0.0.1", "6379", "300", "10000", "16", "", "0"]
|
||||
errors = []
|
||||
|
||||
for n, k in enumerate(keys):
|
||||
rep = r"\n{}\s+(.*)".format(k) # 更准确地捕获整行
|
||||
group = re.search(rep, redis_conf)
|
||||
if group:
|
||||
value = group.group(1).strip()
|
||||
try:
|
||||
if k == "maxmemory":
|
||||
# 将 maxmemory 从字节转换为兆字节并赋值
|
||||
value = str(int(value) // 1024 // 1024)
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
if k == "maxmemory":
|
||||
v = int(group.group(1)) / 1024 / 1024
|
||||
else:
|
||||
v = group.group(1)
|
||||
result[k] = v
|
||||
value = defaults[n]
|
||||
|
||||
if k in ["port", "timeout", "maxclients", "databases", "maxmemory"]:
|
||||
if not value.isdigit():
|
||||
errors.append(f"'{k}' value must be a number, and the currently configured value is '{value}'")
|
||||
continue
|
||||
|
||||
if k == "bind":
|
||||
try:
|
||||
# 尝试解析IP地址以验证其格式
|
||||
ipaddress.ip_address(value)
|
||||
except ValueError:
|
||||
errors.append(
|
||||
f"'{k}' value must be a valid IP address, the current configuration value is '{value}'")
|
||||
continue
|
||||
|
||||
result[k] = value
|
||||
|
||||
if errors:
|
||||
error_message = "The following error was detected in the configuration file for the Redis database:\n" + "\n".join(
|
||||
errors)
|
||||
error_message += "\nPlease go to the software shop to make the correct changes to the configuration file of the redis plugin!"
|
||||
self.error_message = error_message
|
||||
return "Config Error"
|
||||
|
||||
return result
|
||||
|
||||
|
||||
|
||||
class main(databaseBase):
|
||||
_DB_BACKUP_DIR = os.path.join(public.M("config").where("id=?", (1,)).getField("backup_path"), "database")
|
||||
_REDIS_BACKUP_DIR = os.path.join(_DB_BACKUP_DIR, "redis")
|
||||
_REDIS_CONF = os.path.join(public.get_setup_path(), "redis/redis.conf")
|
||||
|
||||
_db_max = 16 #最大redis数据库
|
||||
def __init__(self):
|
||||
pass
|
||||
if not os.path.exists(self._REDIS_BACKUP_DIR):
|
||||
os.makedirs(self._REDIS_BACKUP_DIR)
|
||||
|
||||
self._db_num = 16
|
||||
if os.path.exists(self._REDIS_CONF):
|
||||
redis_conf = public.readFile(self._REDIS_CONF)
|
||||
db_obj = re.search("\ndatabases\s+(\d+)", redis_conf)
|
||||
if db_obj:
|
||||
self._db_num = int(db_obj.group(1))
|
||||
|
||||
def GetCloudServer(self,args):
|
||||
'''
|
||||
def GetCloudServer(self, args):
|
||||
"""
|
||||
@name 获取远程服务器列表
|
||||
@author hwliang<2021-01-10>
|
||||
@return list
|
||||
'''
|
||||
# # 校验参数
|
||||
# try:
|
||||
# args.validate([
|
||||
# Param('type').Require().String('in', ['redis']),
|
||||
# ], [
|
||||
# public.validate.trim_filter(),
|
||||
# ])
|
||||
# except Exception as ex:
|
||||
# public.print_log("error info: {}".format(ex))
|
||||
# return public.return_message(-1, 0, str(ex))
|
||||
# return self.GetBaseCloudServer(args)
|
||||
return public.return_message(0, 0, self.GetBaseCloudServer(args))
|
||||
"""
|
||||
return public.return_message(0, 0, self.GetBaseCloudServer(args))
|
||||
|
||||
|
||||
def AddCloudServer(self,args):
|
||||
'''
|
||||
def AddCloudServer(self, args):
|
||||
"""
|
||||
@添加远程数据库
|
||||
'''
|
||||
"""
|
||||
# {"db_host":"192.168.66.129","db_port":"6379","db_user":"root","db_password":"password1","db_ps":"192.168.66.129","type":"redis"}
|
||||
# 校验参数
|
||||
try:
|
||||
@@ -146,48 +192,59 @@ class main(databaseBase):
|
||||
return public.return_message(-1, 0, str(ex))
|
||||
|
||||
return self.AddBaseCloudServer(args)
|
||||
# return public.return_message(0, 0, self.AddBaseCloudServer(args))
|
||||
|
||||
def RemoveCloudServer(self,args):
|
||||
'''
|
||||
def RemoveCloudServer(self, args):
|
||||
"""
|
||||
@删除远程数据库
|
||||
'''
|
||||
"""
|
||||
return self.RemoveBaseCloudServer(args)
|
||||
# return public.return_message(0, 0, self.RemoveBaseCloudServer(args))
|
||||
|
||||
def ModifyCloudServer(self,args):
|
||||
'''
|
||||
def ModifyCloudServer(self, args):
|
||||
"""
|
||||
@修改远程数据库
|
||||
'''
|
||||
"""
|
||||
return self.ModifyBaseCloudServer(args)
|
||||
|
||||
# return public.return_message(0, 0, self.ModifyBaseCloudServer(args))
|
||||
|
||||
def get_obj_by_sid(self,sid = 0,conn_config = None):
|
||||
def get_obj_by_sid(self, sid: Union[int, str] = 0, conn_config: dict = None):
|
||||
"""
|
||||
@取mssql数据库对像 By sid
|
||||
@sid 数据库分类,0:本地
|
||||
"""
|
||||
if type(sid) == str:
|
||||
try:
|
||||
sid = int(sid)
|
||||
except :sid = 0
|
||||
|
||||
if sid:
|
||||
if not conn_config: conn_config = public.M('database_servers').where("id=?" ,sid).find()
|
||||
if isinstance(sid, str):
|
||||
sid = int(sid)
|
||||
if sid != 0:
|
||||
if not conn_config: conn_config = public.M('database_servers').where("id=?", sid).find()
|
||||
db_obj = panelRedisDB()
|
||||
|
||||
try:
|
||||
db_obj = db_obj.set_host(conn_config['db_host'],conn_config['db_port'],None,conn_config['db_user'],conn_config['db_password'])
|
||||
db_obj = db_obj.set_host(
|
||||
conn_config['db_host'],
|
||||
conn_config['db_port'],
|
||||
None, conn_config['db_user'],
|
||||
conn_config['db_password']
|
||||
)
|
||||
except Exception as e:
|
||||
raise public.PanelError(e)
|
||||
else:
|
||||
db_obj = panelRedisDB()
|
||||
return db_obj
|
||||
|
||||
def local_xsssec(self, text):
|
||||
"""
|
||||
@name XSS防御,只替换关键字符,不转义字符
|
||||
@author hwliang
|
||||
@param text 要转义的字符
|
||||
@return str
|
||||
"""
|
||||
sub_list = {
|
||||
'<': '<',
|
||||
'>': '>'
|
||||
}
|
||||
for s in sub_list.keys():
|
||||
text = text.replace(s, sub_list[s])
|
||||
return text
|
||||
|
||||
|
||||
def get_list(self,args):
|
||||
def get_list(self, args):
|
||||
"""
|
||||
@获取数据库列表
|
||||
@sql_type = redis
|
||||
@@ -204,45 +261,49 @@ class main(databaseBase):
|
||||
public.print_log("error info: {}".format(ex))
|
||||
return public.return_message(-1, 0, str(ex))
|
||||
|
||||
|
||||
result = []
|
||||
self.sid = args.get('sid/d',0)
|
||||
for x in range(0,self._db_max):
|
||||
sid = args.get('sid/d', 0)
|
||||
|
||||
redis_obj = self.get_obj_by_sid(sid).redis_conn(0)
|
||||
if redis_obj is False:
|
||||
if panelRedisDB().error_message:
|
||||
return public.fail_v2(public.lang(str(panelRedisDB().error_message)))
|
||||
return public.success_v2(result)
|
||||
redis_info = redis_obj.info()
|
||||
is_cluster = redis_info.get("cluster_enabled", 0)
|
||||
if is_cluster != 0:
|
||||
return public.fail_v2(public.lang("not support redis cluster!"))
|
||||
db_num = self._db_num
|
||||
if sid != 0:
|
||||
db_num = 1000
|
||||
for x in range(0, db_num):
|
||||
|
||||
data = {}
|
||||
data['id'] = x
|
||||
data['name'] = 'DB{}'.format(x)
|
||||
|
||||
|
||||
try:
|
||||
redis_obj = self.get_obj_by_sid(self.sid).redis_conn(x)
|
||||
redis_obj = self.get_obj_by_sid(sid).redis_conn(x)
|
||||
|
||||
data['keynum'] = redis_obj.dbsize()
|
||||
if data['keynum'] > 0:
|
||||
result.append(data)
|
||||
result.append(data)
|
||||
except Exception as ex:
|
||||
public.print_log("error info2: {}".format(ex))
|
||||
pass
|
||||
break
|
||||
|
||||
#result = sorted(result,key= lambda x:x['keynum'],reverse=True)
|
||||
# return result
|
||||
return public.return_message(0, 0, result)
|
||||
|
||||
|
||||
def set_redis_val(self,args):
|
||||
def set_redis_val(self, args):
|
||||
"""
|
||||
@设置或修改指定值
|
||||
"""
|
||||
# {"val":"bbbbbb12","endtime":"30","name":"aaaaa","db_idx":0,"sid":0}
|
||||
# 校验参数
|
||||
try:
|
||||
args.validate([
|
||||
|
||||
Param('db_idx').Require().Integer(),
|
||||
Param('sid').Require().Integer(),
|
||||
Param('name').Require().String(), # 键
|
||||
Param('val').Require().String(), # 值
|
||||
Param('endtime').Integer(), # 过期时间
|
||||
Param('endtime').Integer(), # 过期时间
|
||||
], [
|
||||
public.validate.trim_filter(),
|
||||
])
|
||||
@@ -250,28 +311,27 @@ class main(databaseBase):
|
||||
public.print_log("error info: {}".format(ex))
|
||||
return public.return_message(-1, 0, str(ex))
|
||||
|
||||
self.sid = args.get('sid/d',0)
|
||||
if not 'name' in args or not 'val' in args:
|
||||
# return public.returnMsg(False, public.lang("Parameter passing error."))
|
||||
return public.return_message(-1, 0, public.lang("Parameter passing error"))
|
||||
sid = args.get("sid/d", 0)
|
||||
db_idx = args.get("db_idx")
|
||||
name = args.get("name")
|
||||
val = args.get("val")
|
||||
endtime = args.get("endtime", None)
|
||||
|
||||
endtime = 0
|
||||
if 'endtime' in args : endtime = int(args.endtime)
|
||||
|
||||
redis_obj = self.get_obj_by_sid(self.sid).redis_conn(args.db_idx)
|
||||
if endtime:
|
||||
redis_obj.set(args.name, args.val, endtime)
|
||||
redis_obj = self.get_obj_by_sid(sid).redis_conn(db_idx)
|
||||
if redis_obj is False:
|
||||
return public.fail_v2(public.lang("redis connect fail!"))
|
||||
if endtime is not None:
|
||||
redis_obj.set(name, val, int(endtime))
|
||||
else:
|
||||
redis_obj.set(args.name, args.val)
|
||||
public.set_module_logs('linux_redis','set_redis_val',1)
|
||||
# return public.returnMsg(True, public.lang("Operation is successful."))
|
||||
redis_obj.set(name, val)
|
||||
|
||||
public.set_module_logs('linux_redis', 'set_redis_val', 1)
|
||||
return public.return_message(0, 0, public.lang("Operation is successful"))
|
||||
|
||||
def del_redis_val(self,args):
|
||||
def del_redis_val(self, args):
|
||||
"""
|
||||
@删除key值
|
||||
"""
|
||||
# {"db_idx":0,"key":"qq","sid":0}
|
||||
# 校验参数
|
||||
try:
|
||||
args.validate([
|
||||
@@ -286,19 +346,16 @@ class main(databaseBase):
|
||||
public.print_log("error info: {}".format(ex))
|
||||
return public.return_message(-1, 0, str(ex))
|
||||
|
||||
self.sid = args.get('sid/d',0)
|
||||
if not 'key' in args:
|
||||
# return public.returnMsg(False, public.lang("Parameter passing error."))
|
||||
return public.return_message(-1, 0, public.lang("Parameter passing error"))
|
||||
sid = args.get('sid/d', 0)
|
||||
|
||||
redis_obj = self.get_obj_by_sid(self.sid).redis_conn(args.db_idx)
|
||||
redis_obj = self.get_obj_by_sid(sid).redis_conn(args.db_idx)
|
||||
if redis_obj is False:
|
||||
return public.fail_v2(public.lang("redis connect fail!"))
|
||||
redis_obj.delete(args.key)
|
||||
|
||||
# return public.returnMsg(True, public.lang("Operation is successful."))
|
||||
return public.return_message(0, 0, public.lang("Operation is successful"))
|
||||
|
||||
|
||||
def clear_flushdb(self,args):
|
||||
def clear_flushdb(self, args):
|
||||
"""
|
||||
清空数据库
|
||||
@ids 清空数据库列表,不传则清空所有
|
||||
@@ -307,7 +364,7 @@ class main(databaseBase):
|
||||
try:
|
||||
args.validate([
|
||||
|
||||
Param('ids').String(), # "ids":"[0,1]"
|
||||
Param('ids').String(), # "ids":"[0,1]"
|
||||
Param('sid').Require().Integer(),
|
||||
], [
|
||||
public.validate.trim_filter(),
|
||||
@@ -316,21 +373,22 @@ class main(databaseBase):
|
||||
public.print_log("error info: {}".format(ex))
|
||||
return public.return_message(-1, 0, str(ex))
|
||||
|
||||
self.sid = args.get('sid/d',0)
|
||||
sid = args.get('sid/d', 0)
|
||||
ids = json.loads(args.ids)
|
||||
#ids = []
|
||||
# ids = []
|
||||
if len(ids) == 0:
|
||||
for x in range(0,self._db_max):
|
||||
ids.append(x)
|
||||
|
||||
for x in range(0, self._db_num):
|
||||
ids.append(x)
|
||||
redis_obj = self.get_obj_by_sid(sid).redis_conn(0)
|
||||
if redis_obj is False:
|
||||
return public.fail_v2(public.lang("redis connect fail!"))
|
||||
for x in ids:
|
||||
redis_obj = self.get_obj_by_sid(self.sid).redis_conn(x)
|
||||
redis_obj = self.get_obj_by_sid(sid).redis_conn(x)
|
||||
redis_obj.flushdb()
|
||||
|
||||
# return public.returnMsg(True, public.lang("Operation is successful."))
|
||||
return public.return_message(0, 0, public.lang("Operation is successful"))
|
||||
|
||||
def get_db_keylist(self,args):
|
||||
def get_db_keylist(self, args):
|
||||
"""
|
||||
@获取指定数据库key集合
|
||||
"""
|
||||
@@ -342,7 +400,7 @@ class main(databaseBase):
|
||||
Param('limit').Integer(),
|
||||
Param('p').Integer(),
|
||||
Param('search').String(),
|
||||
Param('tojs').String(), # 不知道
|
||||
Param('tojs').String(), # 不知道
|
||||
], [
|
||||
public.validate.trim_filter(),
|
||||
])
|
||||
@@ -351,38 +409,58 @@ class main(databaseBase):
|
||||
return public.return_message(-1, 0, str(ex))
|
||||
|
||||
search = '*'
|
||||
if 'search' in args: search = "*" + args.search+"*"
|
||||
if 'search' in args: search = "*" + args.search + "*"
|
||||
db_idx = args.db_idx
|
||||
self.sid = args.get('sid/d',0)
|
||||
sid = args.get('sid/d', 0)
|
||||
|
||||
redis_obj = self.get_obj_by_sid(self.sid).redis_conn(db_idx)
|
||||
redis_obj = self.get_obj_by_sid(sid).redis_conn(db_idx)
|
||||
if redis_obj is False:
|
||||
return public.fail_v2(public.lang("redis connect fail!"))
|
||||
try:
|
||||
keylist = sorted(redis_obj.keys(search))
|
||||
except :
|
||||
total = redis_obj.dbsize()
|
||||
except Exception as err:
|
||||
if str(err).find("Connection refused"):
|
||||
return public.fail_v2(public.lang(f"redis connection failed, please check if the database service is started!"))
|
||||
return public.fail_v2(public.lang(f"redis connection failed, please check if the database service is started!{err}"))
|
||||
info = {'p': 1, 'row': 20, 'count': total}
|
||||
|
||||
if hasattr(args, 'limit'): info['row'] = int(args.limit)
|
||||
if hasattr(args, 'p'): info['p'] = int(args['p'])
|
||||
|
||||
try:
|
||||
|
||||
if search != '*':
|
||||
keylist = redis_obj.keys(search)
|
||||
info['count'] = len(keylist)
|
||||
else:
|
||||
keys = redis_obj.scan(match="{}".format(search), count=info['p'] * info['row'])
|
||||
keylist = keys[1]
|
||||
except:
|
||||
keylist = []
|
||||
|
||||
info = {'p': 1, 'row': 10, 'count': len(keylist)}
|
||||
|
||||
info = {'p':1,'row':10,'count':len(keylist)}
|
||||
|
||||
if hasattr(args,'limit'): info['row'] = int(args.limit)
|
||||
if hasattr(args,'p'): info['p'] = int(args['p'])
|
||||
if hasattr(args, 'limit'): info['row'] = int(args.limit)
|
||||
if hasattr(args, 'p'): info['p'] = int(args['p'])
|
||||
|
||||
import page
|
||||
#实例化分页类
|
||||
# 实例化分页类
|
||||
page = page.Page()
|
||||
|
||||
info['uri'] = args
|
||||
info['return_js'] = ''
|
||||
if hasattr(args,'tojs'):
|
||||
if hasattr(args, 'tojs'):
|
||||
info['return_js'] = args.tojs
|
||||
|
||||
slist = keylist[(info['p']-1) * info['row']:info['p'] * info['row']]
|
||||
slist = keylist[(info['p'] - 1) * info['row']:info['p'] * info['row']]
|
||||
|
||||
rdata = {}
|
||||
rdata['page'] = page.GetPage(info,'1,2,3,4,5,8')
|
||||
rdata['page'] = page.GetPage(info, '1,2,3,4,5,8')
|
||||
rdata['where'] = ''
|
||||
rdata['data'] = []
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
idx = 0
|
||||
for key in slist:
|
||||
item = {}
|
||||
@@ -392,7 +470,22 @@ class main(databaseBase):
|
||||
item['name'] = str(key)
|
||||
|
||||
item['endtime'] = redis_obj.ttl(key)
|
||||
if item['endtime'] == -1: item['endtime'] = 0
|
||||
if item['endtime'] == -1:
|
||||
item['endtime'] = 0
|
||||
item['showtime'] = "royalty-free"
|
||||
else:
|
||||
key_ttl = redis_obj.ttl(key)
|
||||
INT_MAX = 2147483647
|
||||
INT_MIN = -2147483648
|
||||
if key_ttl > INT_MAX or key_ttl < INT_MIN:
|
||||
item['showtime'] = str(key_ttl) + "second"
|
||||
else:
|
||||
delta = timedelta(seconds=key_ttl)
|
||||
days, remainder = divmod(delta.total_seconds(), 86400)
|
||||
hours, remainder = divmod(remainder, 3600)
|
||||
minutes, seconds = divmod(remainder, 60)
|
||||
formatted_ttl = f"{int(days)}days{int(hours)}hours{int(minutes)}min{int(seconds)}second"
|
||||
item['showtime'] = str(formatted_ttl)
|
||||
|
||||
item['type'] = redis_obj.type(key).decode()
|
||||
|
||||
@@ -402,28 +495,43 @@ class main(databaseBase):
|
||||
except:
|
||||
item['val'] = str(redis_obj.get(key))
|
||||
elif item['type'] == 'hash':
|
||||
item['val'] = str(redis_obj.hgetall(key))
|
||||
if redis_obj.hlen(key) > 500:
|
||||
item['val'] = "The amount of data is too large to display!Total {} entries".format(
|
||||
redis_obj.hlen(key))
|
||||
else:
|
||||
item['val'] = str(redis_obj.hgetall(key))
|
||||
elif item['type'] == 'list':
|
||||
item['val'] = str(redis_obj.lrange(key, 0, -1))
|
||||
if redis_obj.llen(key) > 500:
|
||||
item['val'] = "The amount of data is too large to display!Total {} entries".format(
|
||||
redis_obj.llen(key))
|
||||
else:
|
||||
item['val'] = str(redis_obj.lrange(key, 0, -1))
|
||||
elif item['type'] == 'set':
|
||||
item['val'] = str(redis_obj.smembers(key))
|
||||
if redis_obj.scard(key) > 500:
|
||||
item['val'] = "The amount of data is too large to display!Total {} entries".format(
|
||||
redis_obj.scard(key))
|
||||
else:
|
||||
item['val'] = str(redis_obj.smembers(key))
|
||||
elif item['type'] == 'zset':
|
||||
item['val'] = str(redis_obj.zrange(key, 0, 1, withscores=True))
|
||||
if redis_obj.zcard(key) > 500:
|
||||
item['val'] = "The amount of data is too large to display!Total {} entries".format(
|
||||
redis_obj.zcard(key))
|
||||
else:
|
||||
item['val'] = str(redis_obj.zrange(key, 0, -1, withscores=True))
|
||||
else:
|
||||
item['val'] = ''
|
||||
try:
|
||||
item['len'] = redis_obj.strlen(key)
|
||||
except:
|
||||
item['len'] = len(item['val'])
|
||||
item['val'] = public.xsssec(item['val'])
|
||||
item['val'] = self.local_xsssec(item['val'])
|
||||
item['name'] = public.xsssec(item['name'])
|
||||
rdata['data'].append(item)
|
||||
idx += 1
|
||||
# return rdata
|
||||
return public.return_message(0, 0, rdata)
|
||||
|
||||
|
||||
def ToBackup(self,args):
|
||||
# 备份数据库
|
||||
def ToBackup(self, args):
|
||||
"""
|
||||
@备份数据库
|
||||
"""
|
||||
@@ -438,34 +546,45 @@ class main(databaseBase):
|
||||
except Exception as ex:
|
||||
public.print_log("error info: {}".format(ex))
|
||||
return public.return_message(-1, 0, str(ex))
|
||||
try:
|
||||
self.sid = args.get('sid/d',0)
|
||||
sid = args.get('sid/d', 0)
|
||||
if sid != 0:
|
||||
return public.fail_v2(public.lang("Backing up remote databases is not supported at this time!"))
|
||||
|
||||
redis_obj = self.get_obj_by_sid(self.sid).redis_conn(0)
|
||||
redis_obj.save()
|
||||
db_fidx = None
|
||||
if not hasattr(args, "db_idx"):
|
||||
db_fname = "all_db"
|
||||
else:
|
||||
db_fidx = args.db_idx
|
||||
db_fname = "db_{}".format(db_fidx)
|
||||
|
||||
src_path = '{}/dump.rdb'.format(redis_obj.config_get()['dir'])
|
||||
if not os.path.exists(src_path):
|
||||
# return public.returnMsg(False, public.lang("Backup error"))
|
||||
return public.return_message(-1, 0, public.lang("Backup error"))
|
||||
redis_obj = self.get_obj_by_sid(sid)
|
||||
if redis_obj.redis_conn(0) is False:
|
||||
return public.fail_v2(public.lang("redis connect fail!"))
|
||||
|
||||
backup_path = session['config']['backup_path'] + '/database/redis/'
|
||||
if not os.path.exists(backup_path): os.makedirs(backup_path)
|
||||
if db_fidx:
|
||||
redis_obj.redis_conn(0).execute_command("SELECT", int(db_fidx))
|
||||
redis_obj.redis_conn(0).execute_command("SAVE")
|
||||
else:
|
||||
for db_idx in range(0, self._db_num):
|
||||
redis_obj.redis_conn(db_idx).save()
|
||||
|
||||
fileName = backup_path + str(self.sid) + '_db_' + time.strftime('%Y%m%d_%H%M%S',time.localtime()) +'.rdb'
|
||||
redis_obj = redis_obj.redis_conn(0)
|
||||
src_path = os.path.join(redis_obj.config_get().get("dir", ""), "dump.rdb")
|
||||
if not os.path.exists(src_path):
|
||||
return public.fail_v2(public.lang('BACKUP_ERROR'))
|
||||
|
||||
shutil.copyfile(src_path,fileName)
|
||||
if not os.path.exists(fileName):
|
||||
# return public.returnMsg(False, public.lang("Backup error"))
|
||||
return public.return_message(-1, 0, public.lang("Backup error"))
|
||||
file_name = "{db_fname}_{backup_time}_redis_data.rdb".format(db_fname=db_fname,
|
||||
backup_time=time.strftime("%Y-%m-%d_%H-%M-%S",
|
||||
time.localtime()))
|
||||
file_path = os.path.join(self._REDIS_BACKUP_DIR, file_name)
|
||||
|
||||
# return public.returnMsg(True, public.lang("Backup Succeeded!"))
|
||||
return public.return_message(0, 0, public.lang("Backup Succeeded!"))
|
||||
shutil.copyfile(src_path, file_path)
|
||||
if not os.path.exists(file_path):
|
||||
return public.fail_v2(public.lang('BACKUP_ERROR'))
|
||||
|
||||
except Exception as ex:
|
||||
public.print_log("error info22: {}".format(ex))
|
||||
return public.return_message(-1, 0, str(ex))
|
||||
def DelBackup(self,args):
|
||||
return public.success_v2(public.lang('BACKUP_SUCCESS'))
|
||||
|
||||
def DelBackup(self, args):
|
||||
"""
|
||||
@删除备份文件
|
||||
"""
|
||||
@@ -482,14 +601,12 @@ class main(databaseBase):
|
||||
return public.return_message(-1, 0, str(ex))
|
||||
|
||||
file = args.file
|
||||
|
||||
if os.path.exists(file):
|
||||
os.remove(file)
|
||||
|
||||
# return public.returnMsg(True, public.lang("Delete successfully!"))
|
||||
return public.return_message(0, 0, public.lang("Delete successfully!"))
|
||||
|
||||
def InputSql(self,get):
|
||||
def InputSql(self, get):
|
||||
"""
|
||||
@导入数据库
|
||||
"""
|
||||
@@ -507,9 +624,13 @@ class main(databaseBase):
|
||||
return public.return_message(-1, 0, str(ex))
|
||||
|
||||
file = get.file
|
||||
self.sid = get.get('sid/d',0)
|
||||
sid = get.get('sid/d', 0)
|
||||
if not os.path.isfile(file):
|
||||
return public.fail_v2(public.lang("file not found"))
|
||||
|
||||
redis_obj = self.get_obj_by_sid(self.sid).redis_conn(0)
|
||||
redis_obj = self.get_obj_by_sid(sid).redis_conn(0)
|
||||
if redis_obj is False:
|
||||
return public.fail_v2(public.lang("redis connect fail"))
|
||||
|
||||
rpath = redis_obj.config_get()['dir']
|
||||
dst_path = '{}/dump.rdb'.format(rpath)
|
||||
@@ -517,45 +638,13 @@ class main(databaseBase):
|
||||
if os.path.exists(dst_path): os.remove(dst_path)
|
||||
shutil.copy2(file, dst_path)
|
||||
public.ExecShell("chown redis.redis {dump} && chmod 644 {dump}".format(dump=dst_path))
|
||||
# self.restart_services()
|
||||
public.ExecShell("/etc/init.d/redis start")
|
||||
if os.path.exists(dst_path):
|
||||
# return public.returnMsg(True, public.lang("Restore Successful."))
|
||||
return public.return_message(0, 0, public.lang("Restore Successful."))
|
||||
# return public.returnMsg(False, public.lang("Restore failure."))
|
||||
return public.return_message(-1, 0, public.lang("Restore failure."))
|
||||
|
||||
@staticmethod
|
||||
def _get_backup(search: str, files_path_list: list, cloud_list: dict, current_path: str) -> list:
|
||||
"""
|
||||
获取指定目录下redis备份文件列表
|
||||
"""
|
||||
res = []
|
||||
for file_name in files_path_list:
|
||||
try:
|
||||
if search:
|
||||
if file_name.lower().find(search) == -1:
|
||||
continue
|
||||
arrs = file_name.split('_')
|
||||
filepath = '{}/{}'.format(current_path, file_name).replace('//', '/')
|
||||
stat = os.stat(filepath)
|
||||
item = {
|
||||
'name': file_name,
|
||||
'filepath': filepath,
|
||||
'size': stat.st_size,
|
||||
'mtime': int(stat.st_mtime),
|
||||
'sid': arrs[0],
|
||||
'conn_config': cloud_list.get(f"id-{str(arrs[0])}", {}),
|
||||
}
|
||||
res.append(item)
|
||||
except Exception as ex:
|
||||
public.print_log("error info: {}".format(ex))
|
||||
continue
|
||||
return res
|
||||
|
||||
|
||||
|
||||
def get_backup_list(self,get):
|
||||
def get_backup_list(self, get):
|
||||
"""
|
||||
@获取备份文件列表
|
||||
"""
|
||||
@@ -576,39 +665,39 @@ class main(databaseBase):
|
||||
|
||||
nlist = []
|
||||
cloud_list = {}
|
||||
listm = self.GetCloudServer({'type': 'redis'})
|
||||
|
||||
for x in listm['message']:
|
||||
for x in self.GetCloudServer({'type': 'redis'}).get("message", []):
|
||||
cloud_list['id-' + str(x['id'])] = x
|
||||
|
||||
path = session['config']['backup_path'] + '/database/redis/'
|
||||
if not os.path.exists(path):
|
||||
os.makedirs(path)
|
||||
for name in os.listdir(self._REDIS_BACKUP_DIR):
|
||||
if search:
|
||||
if name.lower().find(search) == -1: continue
|
||||
|
||||
all_path = os.listdir(path)
|
||||
if 'crontab_backup' in all_path:
|
||||
cron_path_list = os.listdir(os.path.join(path, 'crontab_backup'))
|
||||
nlist.extend(self._get_backup(
|
||||
search=search,
|
||||
files_path_list=cron_path_list,
|
||||
cloud_list=cloud_list,
|
||||
current_path=os.path.join(path, 'crontab_backup'),
|
||||
))
|
||||
all_path.remove('crontab_backup')
|
||||
arrs = name.split('_')
|
||||
|
||||
nlist.extend(self._get_backup(
|
||||
search=search,
|
||||
files_path_list=all_path,
|
||||
cloud_list=cloud_list,
|
||||
current_path=path,
|
||||
))
|
||||
file_path = os.path.join(self._REDIS_BACKUP_DIR, name).replace('//', '/')
|
||||
if not os.path.isfile(file_path):
|
||||
continue
|
||||
|
||||
stat = os.stat(file_path)
|
||||
|
||||
item = {}
|
||||
item['name'] = name
|
||||
item['filepath'] = file_path
|
||||
item['size'] = stat.st_size
|
||||
item['mtime'] = int(stat.st_mtime)
|
||||
item['sid'] = arrs[0]
|
||||
try:
|
||||
if 0 <= int(arrs[0]) <= 15:
|
||||
item['conn_config'] = cloud_list['id-' + str(arrs[0])]
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
nlist.append(item)
|
||||
|
||||
if hasattr(get, 'sort'):
|
||||
nlist = sorted(nlist, key=lambda data: data['mtime'], reverse=get["sort"] == "desc")
|
||||
|
||||
return public.return_message(0, 0, nlist)
|
||||
|
||||
|
||||
return public.return_message(0, 0, nlist)
|
||||
|
||||
def restart_services(self):
|
||||
"""
|
||||
@@ -618,19 +707,50 @@ class main(databaseBase):
|
||||
public.ExecShell('net start redis')
|
||||
return True
|
||||
|
||||
|
||||
def check_cloud_database_status(self,conn_config):
|
||||
def check_cloud_database_status(self, conn_config):
|
||||
"""
|
||||
@检测远程数据库是否连接
|
||||
@conn_config 远程数据库配置,包含host port pwd等信息
|
||||
"""
|
||||
try:
|
||||
|
||||
sql_obj = panelRedisDB().set_host(conn_config['db_host'],conn_config['db_port'],conn_config['db_name'],conn_config['db_user'],conn_config['db_password'])
|
||||
keynum = sql_obj.redis_conn(0).dbsize()
|
||||
# return True
|
||||
return public.return_message(0, 0, True)
|
||||
sql_obj = panelRedisDB().set_host(conn_config['db_host'], conn_config['db_port'], conn_config['db_name'],
|
||||
conn_config['db_user'], conn_config['db_password'])
|
||||
redis_obj = sql_obj.redis_conn(0)
|
||||
if redis_obj is False:
|
||||
return public.fail_v2(public.lang("redis connect fail"))
|
||||
keynum = redis_obj.dbsize()
|
||||
return True
|
||||
except Exception as ex:
|
||||
return public.fail_v2(public.lang("remote database connect fail {}".format(ex)))
|
||||
|
||||
# return public.returnMsg(False,ex)
|
||||
return public.return_message(-1, 0, ex)
|
||||
# 数据库状态检测
|
||||
def CheckDatabaseStatus(self, get):
|
||||
"""
|
||||
数据库状态检测
|
||||
"""
|
||||
if not hasattr(get, "sid"):
|
||||
return public.fail_v2("params not found! sid")
|
||||
if not str(get.sid).isdigit():
|
||||
return public.fail_v2("params not found! sid")
|
||||
sid = int(get.sid)
|
||||
|
||||
if sid != 0:
|
||||
conn_config = public.M("database_servers").where("id=? AND LOWER(db_type)=LOWER('redis')",
|
||||
(sid,)).find()
|
||||
if not conn_config:
|
||||
return public.fail_v2(public.lang("Remote database information does not exist!"))
|
||||
conn_config["db_name"] = None
|
||||
redis_obj = panelRedisDB().set_host(conn_config['db_host'], conn_config['db_port'],
|
||||
conn_config.get("db_name"), conn_config['db_user'],
|
||||
conn_config['db_password'])
|
||||
else:
|
||||
redis_obj = panelRedisDB()
|
||||
if redis_obj.redis_conn(0) is False:
|
||||
return {"status": True, "msg": "exceptions", "db_status": False}
|
||||
try:
|
||||
redis_obj.redis_conn(0).dbsize()
|
||||
db_status = True
|
||||
except:
|
||||
db_status = False
|
||||
return {"status": True, "msg": "normalcy" if db_status is True else "exceptions", "db_status": db_status}
|
||||
|
||||
Reference in New Issue
Block a user