Files
aaPanel/class_v2/databaseModelV2/mongodbModel.py
T
Jack ed55fa708d Update to 7.7.0
Since version 7.7.0, we recommend yours update python to 3.12.

[+] Using nginx technology to load static files improves access speed
[+] Refactor homepage, website, FTP, and database using vue3
[+] Table loading changed to skeleton screen
[+] Add Website statistics-v2 professional plug-in
[+] Add Home page - top 5 resource occupancy
[+] Add protection for Files management (requires Tamper-proof for Enterprise 3.7)
[+] Website, FTP, Databases page add program status
[+] Add FTP log analysis (only supports Centos)
[+] Add password-free login to phpMyAdmin
[+] Add Proxy Project in Website (Supported when web service uses Nginx)
[+] Add WP Toolkit (Pro version only)
[+] Redesigned Docker module
[+] Add WP Toolkit Protection
[+] Add WP Toolkit Backup and Restore
[+] Add WP Toolkit Migrated
[+] Add WP Toolkit Clone site (supports new domain and subdomain)
[+] Add WP Toolkit Create site from backup of other panel
[+] Add WP Toolkit support for Cron automatic backup (only save Local disk)
[+] Add WP Toolkit operation log
[+] Add Integrity check for WP Toolkit
[+] Add WP Toolkit plug-in management and themes management

[*] Optimize phpMyAdmin formula access method
[*] Optimize Home page PHP display problem
[*] Optimize jump to the login interface after the login expires
[*] Optimize automatic renewal of SSL at some times
[*] Optimize Let's Encrypt to increase application success rate

[-] Fix Logs Audit cannot be opened
[-] Fix apache URL rewrite issue
[-] Fix phpmyadmin installation problem
[-] Fix the problem that some servers cannot install software
[-] Fix upload file error
[-] Fix left menu hiding problem
[-] Fix aaPanel Mobile QR code display problem
[-] Fix problem that third-party plug-ins are not displayed in the App Store
[-] Fix issue where the menu bar is blank when opening new tabs
[-] Fixed panel not being accessible in some cases
[-] Fix the issue where Curl warning caused the inability to apply for SSL
[-] Fix Quota issues for Website, FTP, Databases
[-] Fix file interface display problem on mobile terminal
2024-07-19 11:25:10 +08:00

934 lines
38 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#coding: utf-8
#-------------------------------------------------------------------
# aaPanel
#-------------------------------------------------------------------
# Copyright (c) 2015-2099 aaPanel(www.aapanel.com) All rights reserved.
#-------------------------------------------------------------------
# Author: hwliang <hwl@aapanel.com>
#-------------------------------------------------------------------
#角色说明:
#read:允许用户读取指定数据库
#readWrite:允许用户读写指定数据库
#dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
#userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
#clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
#readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
#readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
#userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
#dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
#root:只在admin数据库中可用。超级账号,超级权限
# sqlite模型
#------------------------------
import os,re,json,time
from databaseModelV2.base import databaseBase
import public
from public.validate import Param
try:
import pymongo
except:
public.ExecShell("btpip install pymongo")
import pymongo
try:
from BTPanel import session
except :pass
class panelMongoDB():
__DB_PASS = None
__DB_USER = None
__DB_PORT = 27017
__DB_HOST = '127.0.0.1'
__DB_CONN = None
__DB_ERR = None
__DB_CLOUD = None
def __init__(self):
self.__config = self.get_options(None)
def __Conn(self,auth):
if not self.__DB_CLOUD:
path = '{}/data/mongo.root'.format(public.get_panel_path())
if os.path.exists(path): self.__DB_PASS = public.readFile(path)
self.__DB_PORT = int(self.__config['port'])
try:
if not self.__DB_USER and auth:
self.__DB_USER = "root"
self.__DB_CONN = pymongo.MongoClient(host=self.__DB_HOST, port=self.__DB_PORT, username = self.__DB_USER, password=self.__DB_PASS)
self.__DB_CONN.admin.command({"listDatabases":1})
return True
except :
try:
self.__DB_CONN = pymongo.MongoClient(host=self.__DB_HOST, port=self.__DB_PORT, username = self.__DB_USER, password=self.__DB_PASS)
self.__DB_CONN.admin.authenticate('root', self.__DB_PASS)
return True
except :
self.__DB_ERR = public.get_error_info()
return False
def get_db_obj(self,db_name = 'admin',auth=0):
"""
@获取连接对象
"""
if not self.__Conn(auth): return self.__DB_ERR
return self.__DB_CONN[db_name]
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)
self.__DB_USER = str(username)
self._USER = str(username)
self.__DB_PASS = str(password)
self.__DB_PREFIX = prefix
self.__DB_CLOUD = 1
return self
#获取配置文件
def get_config(self,get):
filename = '{}/mongodb/config.conf'.format(public.get_setup_path())
if os.path.exists(filename):
return public.readFile(filename)
return ""
#获取配置项
def get_options(self,get):
options = ['port','bind_ip','logpath','dbpath','authorization']
data = {}
conf = self.get_config(None)
for opt in options:
tmp = re.findall(opt + r":\s+(.+)",conf)
if not tmp: continue;
data[opt] = tmp[0]
if not 'authorization' in data:data['authorization'] = "disabled"
# public.writeFile('/www/server/1.txt',json.dumps(data))
return data
class main(databaseBase):
__conf_path = '{}/mongodb/config.conf'.format(public.get_setup_path())
def __init__(self):
pass
def get_list(self,args):
"""
@获取数据库列表
@sql_type = sqlserver
"""
# {"table": "databases", "search": "", "limit": 20, "p": 1}
# 校验参数
try:
args.validate([
Param('table').Require().String().Xss(),
Param('search').String(),
Param('order').String().Xss(),
Param('limit').Integer(),
Param('p').Integer(),
], [
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.get_base_list(args, sql_type = 'mongodb')
return public.return_message(0, 0, self.get_base_list(args, sql_type = 'mongodb'))
def GetCloudServer(self,args):
'''
@name 获取远程服务器列表
@author hwliang<2021-01-10>
@return list
'''
# return self.GetBaseCloudServer(args)
return public.return_message(0, 0, self.GetBaseCloudServer(args))
def AddCloudServer(self,args):
'''
@添加远程数据库
'''
return self.AddBaseCloudServer(args)
# return public.return_message(0, 0, self.AddBaseCloudServer(args))
def RemoveCloudServer(self,args):
'''
@删除远程数据库
'''
return self.RemoveBaseCloudServer(args)
# return public.return_message(0, 0, self.RemoveBaseCloudServer(args))
def ModifyCloudServer(self,args):
'''
@修改远程数据库
'''
return self.ModifyBaseCloudServer(args)
# return public.return_message(0, 0, self.ModifyBaseCloudServer(args))
#获取数据库列表
def exists_databases(self,get):
db_name = get
if type(get) != str:db_name = get['db_name']
auth_status = self.get_local_auth(get)
db_obj = self.get_obj_by_sid(self.sid).get_db_obj('admin',auth=auth_status)
data = db_obj.command({"listDatabases":1})
if 'databases' in data:
for x in data['databases']:
if x['name'] == db_name:
return True
return False
def __set_auth_open(self,status):
"""
@设置数据库密码访问开关
@状态 status:1 开启,2:关闭
"""
conf = public.readFile(self.__conf_path)
if status:
conf = re.sub(r'authorization\s*\:\s*disabled','authorization: enabled',conf)
else:
conf = re.sub(r'authorization\s*\:\s*enabled','authorization: disabled',conf)
public.writeFile(self.__conf_path,conf)
self.restart_services()
return True
def set_auth_status(self,get):
"""
@设置密码认证状态
@status int 0:关闭,1:开启
"""
if not public.process_exists("mongod"):
# return public.returnMsg(False,"Mongodb service has not been started yet!")
return public.return_message(-1, 0, "Mongodb service has not been started yet!")
status = int(get.status)
path = '{}/data/mongo.root'.format(public.get_panel_path())
if status:
if hasattr(get,'password'):
password = get['password'].strip()
if not password or not re.search(r"^[\w@\.]+$", password):
# return public.return_msg_gettext(False, 'Database password cannot be empty or have special characters!')
return public.return_message(-1, 0, 'Database password cannot be empty or have special characters!')
# if re.search('[\u4e00-\u9fa5]',password):
# return public.returnMsg(False,'Database password cannot be Chinese, please change the name!')
else:
password = public.GetRandomString(16)
self.__set_auth_open(0)
_client = panelMongoDB().get_db_obj('admin')
try:
_client.command("dropUser", "root")
except : pass
_client.command("createUser", "root", pwd=password, roles=[
{'role':'root','db':'admin'},
{'role':'clusterAdmin','db':'admin'},
{'role':'readAnyDatabase','db':'admin'},
{'role':'readWriteAnyDatabase','db':'admin'},
{'role':'userAdminAnyDatabase','db':'admin'},
{'role':'dbAdminAnyDatabase','db':'admin'},
{'role':'userAdmin','db':'admin'},
{'role':'dbAdmin','db':'admin'}
])
self.__set_auth_open(1)
public.writeFile(path,password)
else:
if os.path.exists(path): os.remove(path)
self.__set_auth_open(0)
# return public.return_msg_gettext(True,'Setup successfully!')
return public.return_message(0, 0, 'Setup successfully!')
def restart_services(self):
"""
@重启服务
"""
public.ExecShell('/etc/init.d/mongodb restart')
return True
def get_obj_by_sid(self,sid = 0,conn_config = 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()
db_obj = panelMongoDB()
try:
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 = panelMongoDB()
return db_obj
def get_local_auth(self,get):
"""
@验证本地数据库是否需要密码
"""
self.sid = get.get('sid/d',0)
if self.sid != 0: return True
conf = panelMongoDB().get_options(None)
if conf['authorization'] == 'enabled':
return True
return False
def AddDatabase(self,args):
"""
@添加数据库
"""
# 校验参数
try:
args.validate([
Param('sid').Require().Integer(),
], [
public.validate.trim_filter(),
])
except Exception as ex:
public.print_log("error info: {}".format(ex))
return public.return_message(-1, 0, str(ex))
# try:
# int(args.sid)
# except:
# return public.returnMsg(False, 'Database type sid needs int type!')
if not int(args.sid) and not public.process_exists("mongod"):
# return public.returnMsg(False,"Mongodb service has not been started yet!")
return public.return_message(-1, 0, "Mongodb service has not been started yet!")
# try:
username = ''
password = ''
auth_status = self.get_local_auth(args) #auth为true时如果__DB_USER为空则将它赋值为 root,用于开启本地认证后数据库用户为空的情况
data_name = args.name.strip()
# 检测重复添加
find = public.M('databases').where("name=?", (data_name,)).find()
if find:
return public.return_message(-1, 0, 'Cannot be added repeatedly')
if not data_name:
# return public.returnMsg(False, "Database name cannot be empty!")
return public.return_message(-1, 0, "Database name cannot be empty!")
if auth_status:
res = self.add_base_database(args)
if not res['status']:
# return res
return public.return_message(-1, 0, res['msg'])
data_name = res['data_name']
username = res['username']
password = res['data_pwd']
else:
username = data_name
db_obj = self.get_obj_by_sid(self.sid).get_db_obj(data_name,auth=auth_status)
dtype = 'MongoDB'
if not hasattr(args,'ps'): args['ps'] = public.getMsg('INPUT_PS');
addTime = time.strftime('%Y-%m-%d %X',time.localtime())
pid = 0
if hasattr(args,'pid'): pid = args.pid
if hasattr(args,'contact'):
site = public.M('sites').where("id=?",(args.contact,)).field('id,name').find()
if site:
pid = int(args.contact)
args['ps'] = site['name']
db_type = 0
if self.sid: db_type = 2
db_obj.chat.insert_one({})
if auth_status:
db_obj.command("createUser", username, pwd=password, roles=[{'role':'dbOwner','db':data_name},{'role':'userAdmin','db':data_name}])
public.set_module_logs('linux_mongodb','AddDatabase',1)
#添加入SQLITE
public.M('databases').add('pid,sid,db_type,name,username,password,accept,ps,addtime,type',(pid,self.sid,db_type,data_name,username,password,'127.0.0.1',args['ps'],addTime,dtype))
public.WriteLog("TYPE_DATABASE", 'DATABASE_ADD_SUCCESS',(data_name,))
# return public.returnMsg(True,'ADD_SUCCESS')
return public.return_message(0, 0, 'ADD_SUCCESS')
# except Exception as ex:
# public.print_log("error info66: {}".format(ex))
# return public.return_message(-1, 0, str(ex))
def DeleteDatabase(self,args):
"""
@删除数据库
"""
# 校验参数
try:
args.validate([
Param('id').Require().Integer(),
Param('name').Require().String(),
], [
public.validate.trim_filter(),
])
except Exception as ex:
public.print_log("error info: {}".format(ex))
return public.return_message(-1, 0, str(ex))
id = args['id']
find = public.M('databases').where("id=?",(id,)).field('id,pid,name,username,password,type,accept,ps,addtime,sid,db_type').find()
if not find:
# return public.returnMsg(False,'The specified database does not exist.')
return public.return_message(-1, 0, 'The specified database does not exist.')
# try:
# int(find['sid'])
# except:
# return public.returnMsg(False, 'Database type sid needs int type!')
if not public.process_exists("mongod") and not int(find['sid']):
# return public.returnMsg(False,"Mongodb service has not been started yet!")
return public.return_message(-1, 0, "Mongodb service has not been started yet!")
name = args['name']
username = find['username']
auth_status = self.get_local_auth(args)
db_obj = self.get_obj_by_sid(find['sid']).get_db_obj(name,auth_status)
try:
db_obj.command("dropUser", username)
except :
pass
db_obj.command('dropDatabase')
#删除SQLITE
public.M('databases').where("id=?",(id,)).delete()
public.WriteLog("Database manager", 'Successfully deleted!',(name,))
# return public.returnMsg(True, 'Successfully deleted!')
return public.return_message(0, 0, 'Successfully deleted!')
def get_info_by_db_id(self,db_id):
"""
@获取数据库连接详情
@db_id 数据库id
"""
find = public.M('databases').where("id=?" ,db_id).find()
if not find: return False
data = {
'db_host':'127.0.0.1',
'db_port':int(panelMongoDB().get_options(None)['port']),
'db_user':find['username'],
'db_password':find['password']
}
if int(find['sid']):
conn_config = public.M('database_servers').where("id=?" ,find['sid']).find()
data['db_host'] = conn_config['db_host']
data['db_port'] = int(conn_config['db_port'])
return data
#导入
def InputSql(self,args):
# 校验参数
try:
args.validate([
Param('file').SafePath(), # 文件路径
Param('name').String(),
], [
public.validate.trim_filter(),
])
except Exception as ex:
public.print_log("error info: {}".format(ex))
return public.return_message(-1, 0, str(ex))
name = args.name
file = args.file
# try:
if not os.path.exists(file):
# return public.returnMsg(False,'The import path does not exist!')
return public.return_message(-1, 0, 'The import path does not exist!')
if not os.path.isfile(file):
# return public.returnMsg(False,'Only importing compressed files is supported!')
return public.return_message(-1, 0, 'Only importing compressed files is supported!')
find = public.M('databases').where("name=? AND LOWER(type)=LOWER('MongoDB')",(name,)).find()
if not find:
# return public.returnMsg(False,'This database was not found!')
return public.return_message(-1, 0, 'This database was not found!')
get = public.dict_obj()
get.sid = find['sid']
if not public.process_exists("mongod") and not int(find['sid']):
# return public.returnMsg(False,"Mongodb service has not been started yet!")
return public.return_message(-1, 0, "Mongodb service has not been started yet!")
info = self.get_info_by_db_id(find['id'])
mongorestore_obj = '{}/mongodb/bin/mongorestore'.format(public.get_setup_path())
mongoimport_obj = '{}/mongodb/bin/mongoimport'.format(public.get_setup_path())
if not os.path.exists(mongorestore_obj):
# return public.returnMsg(False,'Lack of backup tools, please install MongoDB through [APP Store] first!')
return public.return_message(-1, 0, 'Lack of backup tools, please install MongoDB through [APP Store] first!')
dir_tmp, file_tmp = os.path.split(file)
split_tmp = file_tmp.split(".")
ext = split_tmp[-1]
ext_err = ".".join(split_tmp[1:])
if len(split_tmp[1:]) == 2 and split_tmp[1] not in ['json', 'csv']:
# return public.returnMsg(False, f'.{ext_err} This file format is not currently supported')
return public.return_message(-1, 0, f'.{ext_err} This file format is not currently supported')
if ext not in ['json', 'csv', 'gz', 'zip']:
# return public.returnMsg(False, f'.{ext_err} This file format is not currently supported')
return public.return_message(-1, 0, f'.{ext_err} This file format is not currently supported')
tmpFile = ".".join(split_tmp[:-1])
isgzip = False
if ext != '': # gz zip
if tmpFile == '':
# return public.returnMsg(False, 'FILE_NOT_EXISTS', (tmpFile,))
return public.return_message(-1, 0, 'FILE_NOT_EXISTS', (tmpFile,))
isgzip = True
# 面板默认备份路径
backupPath = session['config']['backup_path'] + '/database'
input_path = os.path.join(backupPath, tmpFile)
# 备份文件的路径
input_path2 = os.path.join(dir_tmp, tmpFile)
if ext == 'zip': # zip
public.ExecShell("cd " + backupPath + " && unzip " + '"' + file + '"')
else: # gz
public.ExecShell("cd " + backupPath + " && tar zxf " + '"' + file + '"')
if not os.path.exists(input_path):
# 兼容从备份文件所在目录恢复
if not os.path.exists(input_path2):
public.ExecShell("cd " + backupPath + " && gunzip -q " + '"' + file + '"')
else:
input_path = input_path2
if not os.path.exists(input_path) and os.path.isfile(input_path2):
input_path = input_path2
else:
input_path = file
if os.path.isdir(input_path): # zip,gz,bson
if self.get_local_auth(get):
for temp_file in os.listdir(input_path):
shell = f"""
{mongorestore_obj} \
--host={info['db_host']} \
--port={info['db_port']} \
--db={find['name']} \
--username={info['db_user']} \
--password={info['db_password']} \
--drop \
{os.path.join(input_path, temp_file)}
"""
public.ExecShell(shell)
else:
for temp_file in os.listdir(input_path):
shell = f"""
{mongorestore_obj} \
--host={info['db_host']} \
--port={info['db_port']} \
--db={find['name']} \
--drop \
{os.path.join(input_path, temp_file)}
"""
public.ExecShell(shell)
if isgzip is True:
public.ExecShell("rm -f " + input_path)
else:# json,csv
file_tmp = os.path.basename(input_path)
file_name = file_tmp.split(".")[0]
ext = file_tmp.split(".")[-1]
if ext not in ["json","csv"]:
# return public.returnMsg(False, 'File format is incorrect!')
return public.return_message(-1, 0, 'File format is incorrect!')
shell_txt = ""
if ext == "csv":
fp = open(input_path, "r")
fields_list = fp.readline()
fp.close()
shell_txt = f"--fields={fields_list}"
if self.get_local_auth(get):
shell = f"""
{mongoimport_obj} \
--host={info['db_host']} \
--port={info['db_port']} \
--db={find['name']} \
--username={info['db_user']} \
--password={info['db_password']} \
--collection={file_name} \
--file={input_path} \
--type={ext} \
--drop
"""
else:
shell = f"""
{mongoimport_obj} \
--host={info['db_host']} \
--port={info['db_port']} \
--db={find['name']} \
--collection={file_name} \
--file={input_path} \
--type={ext} \
--drop
"""
shell = f"{shell} {shell_txt}"
public.ExecShell(shell)
public.WriteLog("Database manager", 'Import database [{}] succeeded'.format(name))
# return public.returnMsg(True, 'Successfully imported database!')
return public.return_message(0, 0, 'Successfully imported database!')
# except Exception as ex:
# public.print_log("error info66: {}".format(ex))
# return public.return_message(-1, 0, str(ex))
#######################
def ToBackup(self,args):
"""
@备份数据库 id 数据库id
"""
# 校验参数
try:
args.validate([
Param('id').Require().Integer(),
], [
public.validate.trim_filter(),
])
except Exception as ex:
public.print_log("error info: {}".format(ex))
return public.return_message(-1, 0, str(ex))
id = args['id']
find = public.M('databases').where("id=? AND LOWER(type)=LOWER('MongoDB')",(id,)).find()
if not find:
# return public.returnMsg(False,'The specified database does not exist.')
return public.return_message(-1, 0, 'The specified database does not exist.')
fileName = f"{find['name']}_mongodb_data_{time.strftime('%Y%m%d_%H%M%S',time.localtime())}"
backupName = session['config']['backup_path'] + '/database/mongodb/' + fileName
spath = os.path.dirname(backupName)
if not os.path.exists(spath): os.makedirs(spath)
get = public.dict_obj()
get.sid = find['sid']
try:
sid = int(find['sid'])
except:
# return public.returnMsg(False, 'Database type sid needs int type!')
return public.return_message(-1, 0, 'Database type sid needs int type!')
if not public.process_exists("mongod") and not int(find['sid']):
# return public.returnMsg(False,"Mongodb service has not been started yet!")
return public.return_message(-1, 0, "Mongodb service has not been started yet!")
info = self.get_info_by_db_id(id)
sql_dump = '{}/mongodb/bin/mongodump'.format(public.get_setup_path())
if not os.path.exists(sql_dump):
# return public.returnMsg(False,'Lack of backup tools, please install MongoDB through [APP Store] first!')
return public.return_message(-1, 0, 'Lack of backup tools, please install MongoDB through [APP Store] first!')
if self.get_local_auth(get):
if not info['db_password']:
# return public.returnMsg(False,'Password authentication has been enabled. The password cannot be empty when the database is backed up. Please set a password and try again!')
return public.return_message(-1, 0, 'Password authentication has been enabled. The password cannot be empty when the database is backed up. Please set a password and try again!')
shell = "{} -h {} --port {} -u {} -p {} -d {} -o {} ".format(sql_dump,info['db_host'],info['db_port'],info['db_user'],info['db_password'],find['name'] ,backupName)
else:
shell = "{} -h {} --port {} -d {} -o {} ".format(sql_dump,info['db_host'],info['db_port'],find['name'] ,backupName)
ret = public.ExecShell(shell)
if not os.path.exists(backupName):
# return public.returnMsg(False,'Database backup failed, file does not exist');
return public.return_message(-1, 0, 'Database backup failed, file does not exist');
backupFile = f"{backupName}.zip"
public.ExecShell(f"cd {spath} && zip {backupFile} -r {fileName}")
fileName = f"{fileName}.zip"
public.M('backup').add('type,name,pid,filename,size,addtime',(1,fileName,id,backupFile,0,time.strftime('%Y-%m-%d %X',time.localtime())))
public.WriteLog("TYPE_DATABASE", "DATABASE_BACKUP_SUCCESS",(find['name'],))
public.ExecShell(f"rm -rf {backupName}")
if not os.path.exists(backupFile):
# return public.returnMsg(True, 'Backup failed,{}.'.format(ret[0]))
return public.return_message(0, 0, 'Backup failed,{}.'.format(ret[0]))
if os.path.getsize(backupFile) < 1:
# return public.returnMsg(True, 'The backup is executed successfully, the backup file is less than 1b, please check the backup integrity.')
return public.return_message(0, 0, 'The backup is executed successfully, the backup file is less than 1b, please check the backup integrity.')
else:
# return public.returnMsg(True, 'BACKUP_SUCCESS')
return public.return_message(0, 0, 'BACKUP_SUCCESS')
def DelBackup(self,args):
"""
@删除备份文件
"""
return self.delete_base_backup(args)
# return public.return_message(0, 0, self.delete_base_backup(args))
#同步数据库到服务器
def SyncToDatabases(self,get):
type = int(get['type'])
n = 0
sql = public.M('databases')
if type == 0:
data = sql.field('id,name,username,password,accept,type,sid,db_type').where('type=?',('MongoDB',)).select()
for value in data:
if value['db_type'] in ['1',1]:
continue # 跳过远程数据库
result = self.ToDataBase(value)
if result == 1: n +=1
else:
import json
data = json.loads(get.ids)
for value in data:
find = sql.where("id=?",(value,)).field('id,name,username,password,sid,db_type,accept,type').find()
result = self.ToDataBase(find)
if result == 1: n +=1
if n == 1:
# return public.returnMsg(True, 'Synchronization succeeded')
return public.return_message(0, 0, 'Synchronization succeeded')
elif n == 0:
# return public.returnMsg(False,'Sync failed')
return public.return_message(-1, 0, 'No synchronized database')
# return public.returnMsg(True,'DATABASE_SYNC_SUCCESS',(str(n),))
return public.return_message(0, 0, 'DATABASE_SYNC_SUCCESS {}'.format(n))
#添加到服务器
def ToDataBase(self,find):
if find['username'] == 'bt_default':
# return 0
return public.return_message(0, 0, 0)
if len(find['password']) < 3 :
find['username'] = find['name']
find['password'] = public.md5(str(time.time()) + find['name'])[0:10]
public.M('databases').where("id=?",(find['id'],)).save('password,username',(find['password'],find['username']))
self.sid = find['sid']
try:
int(find['sid'])
except:
return public.return_message(-1, 0, 'Database type sid needs int type!')
# return public.returnMsg(False, 'Database type sid needs int type!')
if not public.process_exists("mongod") and not int(find['sid']):
# return public.returnMsg(False,"Mongodb service has not been started yet!")
return public.return_message(-1, 0, "Mongodb service has not been started yet!")
get = public.dict_obj()
get.sid = self.sid
auth_status = self.get_local_auth(get)
if auth_status:
db_obj = self.get_obj_by_sid(self.sid).get_db_obj(find['name'], auth_status)
try:
db_obj.chat.insert_one({})
db_obj.command("dropUser", find['username'])
except :pass
try:
db_obj.command("createUser", find['username'], pwd=find['password'], roles=[{'role':'dbOwner','db':find['name']},{'role':'userAdmin','db':find['name']}])
except:
pass
# return 1
return public.return_message(0, 0, 1)
def SyncGetDatabases(self,get):
"""
@从服务器获取数据库
"""
n = 0;s = 0;
db_type = 0
self.sid = get.get('sid/d',0)
if self.sid: db_type = 2
try:
int(get.sid)
except:
# return public.returnMsg(False, 'The database type SID requires an INT!')
return public.return_message(-1, 0, 'The database type SID requires an INT!')
if not public.process_exists("mongod") and not int(get.sid):
# return public.returnMsg(False,"The Mongodb service is not enabled!")
return public.return_message(-1, 0, "The Mongodb service is not enabled!")
auth_status = self.get_local_auth(get)
data = self.get_obj_by_sid(self.sid).get_db_obj('admin',auth=auth_status).command({"listDatabases":1})
sql = public.M('databases')
nameArr = ['information_schema','performance_schema','mysql','sys','master','model','msdb','tempdb','config','local','admin']
for item in data['databases']:
dbname = item['name']
if sql.where("name=?",(dbname,)).count(): continue
if not dbname in nameArr:
if sql.table('databases').add('name,username,password,accept,ps,addtime,type,sid,db_type',(dbname,dbname,'','',public.getMsg('INPUT_PS'),time.strftime('%Y-%m-%d %X',time.localtime()),'MongoDB',self.sid,db_type)): n +=1
# return public.returnMsg(True,'DATABASE_GET_SUCCESS',(str(n),))
return public.return_message(0, 0, 'DATABASE_GET_SUCCESS {}'.format(n))
def ResDatabasePassword(self,args):
"""
@修改用户密码
"""
id = args['id']
username = args['name'].strip()
newpassword = public.trim(args['password'])
try:
if not newpassword:
# return public.returnMsg(False, 'Modify the failureThe database[' + username + ']password cannot be empty.');
return public.return_message(-1, 0, 'Modify the failureThe database[' + username + ']password cannot be empty.');
if len(re.search(r"^[\w@\.]+$", newpassword).groups()) > 0:
# return public.returnMsg(False, 'The database password cannot be empty or contain special characters')
return public.return_message(-1, 0, 'The database password cannot be empty or contain special characters')
if re.search('[\u4e00-\u9fa5]',newpassword):
# return public.returnMsg(False,'Database password cannot be Chinese, please change the name!')
return public.return_message(-1, 0, 'Database password cannot be Chinese, please change the name!')
except :
# return public.returnMsg(False, 'The database password cannot be empty or contain special characters')
return public.return_message(-1, 0, 'The database password cannot be empty or contain special characters')
find = public.M('databases').where("id=?",(id,)).field('id,pid,name,username,password,type,accept,ps,addtime,sid').find();
if not find:
# return public.returnMsg(False, 'The modification failed because the specified database does not exist.');
return public.return_message(-1, 0, 'The modification failed because the specified database does not exist.');
get = public.dict_obj()
get.sid = find['sid']
try:
int(find['sid'])
except:
# return public.returnMsg(False, 'The database type SID requires an INT!')
return public.return_message(-1, 0, 'The database type SID requires an INT!')
if not public.process_exists("mongod") and not int(find['sid']):
# return public.returnMsg(False,"The Mongodb service is not enabled!")
return public.return_message(-1, 0, "The Mongodb service is not enabled!")
auth_status = self.get_local_auth(args)
if auth_status:
db_obj = self.get_obj_by_sid(find['sid']).get_db_obj(username,auth=auth_status)
try:
print(db_obj.command("updateUser", username, pwd = newpassword))
except :
print(db_obj.command("createUser", username, pwd=newpassword, roles=[{'role':'dbOwner','db':find['name']},{'role':'userAdmin','db':find['name']}]))
else:
# return public.returnMsg(False, 'Password access is not enabled for the database.')
return public.return_message(-1, 0, 'Password access is not enabled for the database.')
#修改SQLITE
public.M('databases').where("id=?",(id,)).setField('password',newpassword)
public.WriteLog("TYPE_DATABASE",'DATABASE_PASS_SUCCESS',(find['name'],))
# return public.returnMsg(True,'DATABASE_PASS_SUCCESS',(find['name'],))
return public.return_message(0, 0, 'DATABASE_PASS_SUCCESS',(find['name'],))
def get_root_pwd(self,args):
"""
@获取root密码
"""
config = panelMongoDB().get_options(None)
sa_path = '{}/data/mongo.root'.format(public.get_panel_path())
if os.path.exists(sa_path):
config['msg'] = public.readFile(sa_path)
else:
config['msg'] = ''
config['root'] = config['msg']
# return config
return public.return_message(0, 0, config)
def get_database_size_by_id(self, args):
"""
@获取数据库尺寸(批量删除验证)
@args json/int 数据库id
"""
# if not public.process_exists("mongod"):
# return public.returnMsg(False,"The Mongodb service is not enabled!")
total = 0
db_id = args
if not isinstance(args, int): db_id = args['db_id']
find = public.M('databases').where('id=?', db_id).find()
try:
int(find['sid'])
except:
# return 0
return public.return_message(0, 0, 0)
if not public.process_exists("mongod") and not int(find['sid']):
# return 0
return public.return_message(0, 0, 0)
try:
auth_status = self.get_local_auth(args)
db_obj = self.get_obj_by_sid(find['sid']).get_db_obj(find['name'], auth=auth_status)
print(db_obj)
print(db_obj.stats())
total = tables[0][1]
if not total: total = 0
except:
print(public.get_error_info())
# return total
return public.return_message(0, 0, total)
def check_del_data(self,args):
"""
@删除数据库前置检测
"""
# return self.check_base_del_data(args)
return public.return_message(0, 0, self.check_base_del_data(args))
def check_cloud_database_status(self,conn_config):
"""
@检测远程数据库是否连接
@conn_config 远程数据库配置,包含host port pwd等信息
"""
try:
if not 'db_name' in conn_config: conn_config['db_name'] = None
sql_obj = panelMongoDB().set_host(conn_config['db_host'],conn_config['db_port'],conn_config['db_name'],conn_config['db_user'],conn_config['db_password'])
db_obj = sql_obj.get_db_obj('admin')
data = db_obj.command({"listDatabases":1})
if 'databases' in data:
# return True
return public.return_message(0, 0, True)
# return False
return public.return_message(-1, 0, False)
except Exception as ex:
# return public.returnMsg(False,ex)
return public.return_message(0, 0, ex)