305 lines
7.9 KiB
Bash
305 lines
7.9 KiB
Bash
#!/usr/bin/env bash
|
||
#-------------------------------------------------
|
||
# 命令脚本
|
||
# @author lwt
|
||
#-------------------------------------------------
|
||
# 连接数据库
|
||
DOC[sql_start]="连接数据库 参数:数据库名称"
|
||
fun_sql_start(){
|
||
is_exists_server_id
|
||
DB=`show_game_db ${SERVER_ID}`
|
||
INFO "连接\e[92m${DB}\e[0;0m数据库"
|
||
start ${DB}
|
||
}
|
||
|
||
# 查看数据库
|
||
DOC[sql_all_data]="查看数据库"
|
||
fun_sql_all_data(){
|
||
INFO "查看ip:\e[92m${DB_HOST}\e[0;0m所有数据库..."
|
||
show_databases ${DB_HOST}
|
||
}
|
||
|
||
|
||
|
||
# 远程执行sql脚本
|
||
DOC[sql_update_sql]="远程执行sql脚本"
|
||
fun_sql_update_sql(){
|
||
is_exists_server_id
|
||
DB=`show_game_db ${SERVER_ID}`
|
||
FILE=${ROOT}/${SQL_GAME_UPDATE}
|
||
INFO "数据库::\e[92m${DB}\e[0;0m 执行sql脚本...${FILE}"
|
||
update_sql ${DB} ${FILE}
|
||
# cat /dev/null > ${FILE}
|
||
}
|
||
|
||
# 生成本地sql数据库
|
||
DOC[sql_local]="生成本地sql数据库"
|
||
fun_sql_local(){
|
||
FILE=${ROOT}/${SQL_GAME_INIT}
|
||
mysql -A -h ${DB_LOCAL_HOST} -u${DB_LOCAL_USE} -p${DB_LOCAL_PASS} -P${DB_LOCAL_PORT} -e "DROP DATABASE if exists ${DB_LOCAL_SERVER}; create database if not exists ${DB_LOCAL_SERVER};"
|
||
INFO "数据库::\e[92m${DB_LOCAL_HOST}\e[0;0m 生成本地sql数据库...${FILE}"
|
||
mysql -A -h ${DB_LOCAL_HOST} -u${DB_LOCAL_USE} -p${DB_LOCAL_PASS} -P${DB_LOCAL_PORT} -D ${DB_LOCAL_SERVER} < ${FILE}
|
||
}
|
||
|
||
|
||
|
||
# 查看服务器列表
|
||
DOC[sql_server]="查看服务器列表"
|
||
fun_sql_server(){
|
||
INFO "查看ip:\e[92m${DB_HOST}\e[0;0m所有服务器列表..."
|
||
show_server ${DB_HOST}
|
||
}
|
||
|
||
# 查看所有表
|
||
DOC[sql_all_table]="查看所有数据表"
|
||
fun_sql_all_table(){
|
||
is_exists_server_id
|
||
DB=`show_game_db ${SERVER_ID}`
|
||
INFO "查看所有\e[92m${DB}\e[0;0m数据表..."
|
||
TABLES=`show_tables ${DB}`
|
||
for TABLE in ${TABLES[*]}; do
|
||
NUM=`table_count ${DB} ${TABLE}`
|
||
FROMAT "\e[92m${TABLE}\e[0;0m count in \e[92m${NUM}\e[0;0m"
|
||
done
|
||
}
|
||
|
||
# 查看所有账号
|
||
DOC[sql_all_account]="查看所有账号 参数:服务器id"
|
||
fun_sql_all_account(){
|
||
is_exists_server_id
|
||
DB=`show_game_db ${SERVER_ID}`
|
||
is_exists_data ${DB}
|
||
INFO "查看所有\e[92m${DB}\e[0;0m中的所有账号..."
|
||
show_account ${DB}
|
||
}
|
||
|
||
# 清表结构
|
||
DOC[sql_del_table]="删除数据表 参数:表名 服务器id"
|
||
fun_sql_del_table(){
|
||
if [ "${1}" = "" ]; then
|
||
ERR "请选中要删除的表格"
|
||
exit 0
|
||
else
|
||
TABLE=$1
|
||
fi
|
||
is_exists_server_id
|
||
DB=`show_game_db ${SERVER_ID}`
|
||
is_exists_table ${DB} ${TABLE}
|
||
INFO "删除\e[92m${DB}\e[0;0m数据库的\e[92m${TABLE}\e[0;0m表格"
|
||
INFO "确定要删除(y/n):"
|
||
read IS
|
||
if [ ${IS} == y ]; then
|
||
drop_table ${DB} ${TABLE}
|
||
if [ $? -ne 0 ]; then
|
||
ERR "删除失败,请检查"
|
||
fi
|
||
INFO "删除完成"
|
||
|
||
else
|
||
INFO "取消删除"
|
||
fi
|
||
}
|
||
|
||
# 清档
|
||
DOC[sql_clear_db]="停服清档 参数:服务器id"
|
||
fun_sql_clear_db(){
|
||
is_exists_server_id
|
||
GAME_DB=`show_game_db ${SERVER_ID}`
|
||
LOG_DB=`show_log_db ${SERVER_ID}`
|
||
INFO "正在删除数据表\e[92m${GAME_DB}\e[0;0m"
|
||
INFO "正在删除日志表\e[92m${LOG_DB}\e[0;0m"
|
||
INFO "确定要清档(y/n):"
|
||
read IS
|
||
if [ "${IS}" == "y" ] || [ "${IS}" == "Y" ]; then
|
||
#关服
|
||
SERVER_NODE=`show_server_node ${SERVER_ID}`
|
||
fun_srv_stop ${SERVER_ID}
|
||
drop_login ${SERVER_ID}
|
||
drop_database ${GAME_DB}
|
||
drop_database ${LOG_DB}
|
||
rm -rf ${ROOT}/server/server/dets
|
||
INFO "清档完成"
|
||
else
|
||
INFO "取消清档"
|
||
fi
|
||
}
|
||
|
||
# 执行sql命令
|
||
DOC[sql_command]="执行sql命令 参数:服务器id sql命令"
|
||
fun_sql_command(){
|
||
if [ "$*" = "" ]; then
|
||
ERR "请输入命令"
|
||
exit 1
|
||
else
|
||
COMMAND=$*
|
||
fi
|
||
is_exists_server_id
|
||
DB_NAME=`show_game_db ${SERVER_ID}`
|
||
INFO "数据库\e[92m${DB_NAME}\e[0;0m执行\e[92m${COMMAND}\e[0;0m的命令"
|
||
command ${COMMAND}
|
||
}
|
||
|
||
# 查询表结构
|
||
DOC[sql_desc]="查询表结构 参数:表名 server_id"
|
||
fun_sql_desc(){
|
||
if [ "${1}" = "" ]; then
|
||
ERR "请输入要查询的表格"
|
||
exit 0
|
||
else
|
||
TABLE=$1
|
||
fi
|
||
is_exists_server_id
|
||
DB=`show_game_db ${SERVER_ID}`
|
||
INFO "查看\e[92m${DB}\e[0;0m数据库的\e[92m${TABLE}\e[0;0m表格结构"
|
||
is_exists_table ${DB} ${TABLE}
|
||
NUM=`table_count ${DB} ${TABLE}`
|
||
INFO "表格行数${NUM}"
|
||
desc ${DB} ${TABLE}
|
||
exit 0
|
||
}
|
||
|
||
# 判断表是否存在
|
||
is_exists_table(){
|
||
DB=$1
|
||
TABLE=$2
|
||
TABLES=`show_tables ${DB}`
|
||
if is_exist ${TABLE} ${TABLES}; then
|
||
return 0
|
||
else
|
||
ERR "要删除的${TABLE}表不存在"
|
||
INFO "是否查看所有表(y/n):"
|
||
read ALL
|
||
if [ "${ALL}" == "y" ] || [ "${ALL}" == "Y" ]; then
|
||
fun_sql_all_table
|
||
fi
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
# 判断数据表是否存在
|
||
is_exists_data(){
|
||
DB=$1
|
||
TABLES=`show_databases ${DB_HOST}`
|
||
if is_exist ${DB} ${TABLES}; then
|
||
return 0
|
||
else
|
||
ERR "查询的${DB}数据库不存在"
|
||
INFO "是否查看所有数据库(y/n):"
|
||
read ALL
|
||
if [ "${ALL}" == "y" ] || [ "${ALL}" == "Y" ]; then
|
||
fun_sql_all_data
|
||
fi
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
# 判断服务器是否存在
|
||
is_exists_server_id(){
|
||
TABLES=`show_server_id`
|
||
if is_exist ${SERVER_ID} ${TABLES}; then
|
||
return 0
|
||
else
|
||
ERR "要操作的(server_id = ${SERVER_ID})服务器不存在"
|
||
INFO "是否查看所服务器(y/n):"
|
||
read ALL
|
||
if [ "${ALL}" == "y" ] || [ "${ALL}" == "Y" ]; then
|
||
fun_sql_server
|
||
fi
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
#判断是否存在
|
||
is_exist(){
|
||
TYPE=$1
|
||
shift
|
||
# 将输入的参数转换为列表
|
||
TYPES=$@
|
||
for K in ${TYPES[*]}; do
|
||
if [[ "${TYPE}" == "${K}" ]]; then
|
||
return 0
|
||
fi
|
||
done
|
||
return 1
|
||
}
|
||
|
||
# 启动服务器
|
||
start(){
|
||
mysql -A -h ${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${1}
|
||
}
|
||
|
||
# 获取数据库
|
||
show_server(){
|
||
mysql -A -h ${1} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${DB_SERVER} -e "select server_id,game_db,server_node,role_cnt from server_info;"
|
||
}
|
||
|
||
# 获取数据库
|
||
show_server_node(){
|
||
mysql -A -h ${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${DB_SERVER} -BNe "select server_node from server_info where server_id = ${1};"
|
||
}
|
||
|
||
# 获取数据库
|
||
show_server_id(){
|
||
mysql -A -h ${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${DB_SERVER} -BNe "select server_id from server_info;"
|
||
}
|
||
|
||
# 获取数据库
|
||
show_game_db(){
|
||
mysql -A -h ${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${DB_SERVER} -BNe "select game_db from server_info where server_id = ${1};"
|
||
}
|
||
|
||
# 获取数据库
|
||
show_log_db(){
|
||
mysql -A -h ${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${DB_SERVER} -BNe "select log_db from server_info where server_id = ${1};"
|
||
}
|
||
|
||
# 获取数据库
|
||
show_databases(){
|
||
mysql -A -h ${1} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -e "show databases"
|
||
}
|
||
|
||
# 获取所有表
|
||
show_tables(){
|
||
mysql -A -h ${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${1} -BNe "show tables"
|
||
}
|
||
|
||
# 获取所有账号
|
||
show_account(){
|
||
mysql -A -h ${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${1} -e "select role_id,role_name,fn_uid,level from role"
|
||
}
|
||
|
||
# 获取表数量
|
||
table_count(){
|
||
mysql -A -h ${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${1} -NBe "select count(1) from ${2}"
|
||
}
|
||
|
||
# 查看表结构
|
||
desc(){
|
||
mysql -A -h ${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${1} -e "desc ${2}"
|
||
}
|
||
|
||
# 执行命令
|
||
command(){
|
||
mysql -A -h ${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${DB_NAME} -e "$*"
|
||
}
|
||
|
||
# 清除数据库
|
||
drop_database(){
|
||
mysql -A -h${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -e "DROP DATABASE ${1}"
|
||
}
|
||
|
||
# 清除表
|
||
drop_table(){
|
||
mysql -A -h ${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${1} -e "drop table ${2}"
|
||
}
|
||
|
||
# 清除登录信息
|
||
drop_login(){
|
||
WERL_NUM=`ps_num ${WERL}`
|
||
${ERL} -noshell -name eval_server${WERL_NUM}@127.0.0.1 -setcookie ${DEF_COOKIE} -eval "rpc:call('${LOGIN_SERVER_NODE}', login_server_app, remove_server_data, [${1}])" -s c q
|
||
}
|
||
|
||
# 执行sql更新
|
||
update_sql(){
|
||
mysql -A -h ${DB_HOST} -u${DB_USE} -p${DB_PASS} -P${DB_PORT} -D ${1} < ${2}
|
||
} |