update_script/shikong.sh

185 lines
5.3 KiB
Bash
Raw Normal View History

#!/bin/bash
# 删除命令乱码解决
stty erase ^h
# 文字颜色
red='\033[31m'
green='\033[32m'
yellow='\033[33m'
blue='\033[34m'
# shellcheck disable=SC2034
purple='\033[35m'
# shellcheck disable=SC2034
cyan='\033[36m'
# shellcheck disable=SC2034
white='\033[37m'
NC='\033[0m' # 恢复默认颜色
# 判断一个字符串是否是合法的IP地址
if_ip() {
local ip=$1
local pattern="^([0-9]{1,3}\.){3}[0-9]{1,3}$"
if [[ $ip =~ $pattern ]]; then
# 检查每个数字是否在0到255之间
local IFS='.'
read -ra octets <<< "$ip"
for octet in "${octets[@]}"; do
if (( octet < 0 || octet > 255 )); then
return 1
fi
done
return 0
else
return 1
fi
}
# 显示指定目录下的文件或目录
show_file(){
filer=$1
find /data/update/"$filer" -maxdepth 1 \( -type f -o -type d \) -printf "%T@\t%-20f\n" | sort -n | awk '{print strftime("%Y-%m-%d %H:%M:%S", $1)"\t"$2}'
}
# 获取当前时间,精确到秒,格式年月日时分秒
get_current_time() {
local current_time
current_time=$(date +"%Y%m%d_%H%M%S")
echo "$current_time"
}
# 发包并解压缩
send_server_package(){
# 服务器地址
local ip=${1}
# tar 包名
local package=${2}.tar.gz
# 源服务器上的tar压缩包路径
local source_tar=${3}
# 游戏服目标服务器上的目标目录路径
local destination_dir=${4}
if [ -e $source_tar ]; then
echo "正在处理服务器 $ip"
scp "$source_tar/$package" "$ip:$destination_dir" || { echo "SCP 失败"; exit 1; }
ssh "$ip" "tar -zxvf $destination_dir/$package -C $destination_dir"
ssh "$ip" "rm -f $destination_dir/$package"
echo "处理完成服务器 $ip "
else
echo "$source_tar/$package 不存在,请先去打包!"
return
fi
}
# 游戏代码更新
server_build_update(){
# 服务器代码
local code=$1
# 包名
show_file package | grep "$code"
read -rp "请输入包名(不带文件后缀):" package
# 机器列表
show_file script/txt
read -rp "请输入ip或文件名不带后缀: " channel
# 是否重启
read -r -p "更新完毕是否需要重启Y/N: " reboot
# 源服务器上的tar压缩包路径
local source_tar="/data/update/package/"
2024-07-15 15:05:50 +08:00
local destination_dir="/data/shikong/$code"
echo -e "开始发送游戏包,文件:$source_tar/$package, 机器:$channel, 重启:$reboot"
case $code in
"logic")
if if_ip "$channel"; then
send_server_package $channel $package $source_tar $destination_dir
case $reboot in
[yY][eE][sS]|[yY])
echo "暂未开放重启功能..."
;;
*)
echo "更新结束..."
;;
esac
else
for ip in $(awk '{print $2}' "txt/$channel")
do
send_server_package $ip $package $source_tar $destination_dir
case $reboot in
[yY][eE][sS]|[yY])
echo "暂未开放重启功能..."
;;
*)
echo "更新结束..."
;;
esac
done
fi
;;
"world")
;;
"chat")
;;
esac
}
# 游戏代码打包
server_code_build(){
local code=$1
# shellcheck disable=SC2155
local time=$(get_current_time)
show_file project | grep sk
read -rp "请输入项目地址: " project
local package="${project}_${time}"
case $code in
"logic")
bash ./update_sk_logic.sh "$project" "$package"
;;
"world")
bash ./update_sk_world.sh "$project" "$package"
;;
"chat")
bash ./update_sk_chat.sh "$project" "$package"
;;
esac
}
# 游戏服操作
game_server_option(){
local code=$1
echo -e "1.游戏服打包"
echo -e "2.游戏服更新"
read -rp "请输入选项编号: " choice
case $choice in
1) server_code_build "$code" ;;
2) server_build_update "$code" ;;
esac
}
# 主程序入口
main_menu() {
while true; do
local current_time
current_time=$(get_current_time)
echo -e "${blue}************************************${NC}"
echo -e "${purple}欢迎使用时空更新脚本...${current_time}${NC}"
echo -e "1.时空游戏服更新"
echo -e "2.时空世界服更新"
echo -e "3.时空聊天服更新"
echo -e "4.登陆服更新"
echo -e "5.支付服更新"
echo -e "6.gm后台更新"
echo -e "0.${red}退出${NC}"
echo -e "${blue}************************************${NC}"
read -rp "请输入选项编号: " choice
case $choice in
1) game_server_option "logic" ;;
2) game_server_option "world" ;;
3) game_server_option "chat" ;;
4) echo -e "${red}暂未开放!!!${NC}" ;;
5) echo -e "${red}暂未开放!!!${NC}" ;;
6) echo -e "${red}暂未开放!!!${NC}" ;;
0) echo -e "${red}谢谢使用,再见!${NC}"
exit 0
;;
*) echo -e "${red}无效的选项,请重新输入${NC}" ;;
esac
done
}
# 脚本开始执行
main