Docs/shell/更新微服务/rsync_server_to_remote.sh.md
2022-10-18 16:59:37 +08:00

4.7 KiB

#!/bin/bash

## Edit by ZeroC
# 20170908

. ./server
logpath=/root/update
DIRDATE=`date +%Y-%m-%d`
UPDATE=`date +%Y-%m-%d:%H:%M:%S`

## 帮助
Help()
{
	if [ $# -eq 1 ];then
		baseName=$1
		echo -e "\033[31;1mUsge:\t'bash $baseName help'  打印帮助信息\n\t'bash $baseName'  更新应用服务\033[0m\n"
	elif [ $# -eq 2 -a $2 == "list" ];then
		echo -e "\033[32;1m++++++++ServerName List++++++++\033[0m"
		echo -e "\033[31;1m\ta api\n\tb admin\n\tc pc\n\td swoole\n\te users\n\tf transaction\n\tg products\n\th orders\n\ti fullscale\n\tj repayment\n\tk capital\n\tl sysaccount\n\tm bases\n\tn trusteeship\n\to contract\n\tp others\n\tq scan\033[0m\n"
	fi
}

## ip列表
IPlist()
{
	server=$1
	List=$2
	echo "$server:"
	for ip_l in ${List[@]};do
		echo -e "\033[33;1m\t$ip_l\033[0m"
	done
}

## rsync推送更新
Rsync()
{
    server_c=$1
    ip=$2
	if [ ! -d $logpath/$DIRDATE/$server_c ];then
		mkdir -p $logpath/$DIRDATE/$server_c
	fi
	echo "+++++++++++++++++++++++++++++++++++++++++" >> $logpath/$DIRDATE/$server_c/$ip
	echo $UPDATE >> $logpath/$server_c/$ip
    /usr/bin/rsync -avz --exclude-from=/data/script/rsync/exclude --password-file=/data/script/rsync/pass /data/htdocs/v/$server_c/ rsync@$ip::server  >> $logpath/$DIRDATE/$server_c/$ip
	echo "+++++++++++++++++++++++++++++++++++++++++" >> $logpath/$DIRDATE/$server_c/$ip
	echo >> $logpath/$DIRDATE/$server_c/$ip
}

## 重启微服务
Restart()
{
    #serverU=`echo $1|sed 's/\b[a-z]/\U&/'`
    ip=$2
    ssh root@$ip "/bin/bash /data/script/run.sh restart"
}


## 更新模块
UpdateM()
{
	serverM=$1
	server="\<$1\>"
	iparray=$2
	## 同一服务的所有ip都更新
	if [ $# -eq 2 ];then
		## 微服务
		if [[ "${micro[@]}" =~ $serverM ]];then
			for ip in ${iparray[@]};do
				Rsync $serverM $ip
				Restart $serverM $ip
				sleep 5
			done
		## swoole
		elif [ $serverM == "swoole" ];then
			for ip in ${iparray[@]};do
				Rsync swoole $ip
				Restart swoole $ip
				sleep 5
			done
		## api/admin/pc
		elif [ $serverM == "api" -o "admin" -o "pc" ];then
			for ip in ${iparray[@]};do
				Rsync $serverM $ip
			done
		fi
	# 一个服务只更新其中一个ip
	elif [ $# -eq 3 ]; then
		## 微服务
		if [[ "${micro[@]}" =~ $serverM ]];then
			if [[ "${iparray[@]}" =~ $3 ]];then
				Rsync $serverM $3
				Restart $serverM $3
			else
				echo -e "\033[31;1mWrong IP!\033[0m"
				exit 1
			fi
		## swoole
		elif [ $serverM == "swoole" ];then
			if [[ "${swoole[@]}" =~ $3 ]];then
				Rsync swoole $3
				Restart swoole $3
			else
				echo -e "\033[31;1mWrong IP!\033[0m"
				exit 1
			fi
		# api/admin/pc
		elif [ $serverM == "api" -o "admin" -o "pc" ];then
			if [[ "${iparray[@]}" =~ $2 ]];then
				Rsync $serverM $3
			else
				echo -e "\033[31;1mWrong IP!\033[0m"
				exit 1
			fi
		else
			echo -e "\033[31;1mWrong Server!\033[0m"
		fi
	fi
}

## 更新系统
UpdateS()
{
	serverS=$1
	serverL=$2
	read -p "The IP[default all ip]:" addr
	if [ -z $addr ];then
		echo -e "\033[33;1mUpdate all $serverS\033[0m"
		UpdateM $serverS "${serverL[*]}"
	else
		echo -e "\033[33;1mUpdate one of $serverS: $addr\033[0m"
		UpdateM $serverS "${serverL[*]}" $addr
	fi
}


## Main
echo -e "\033[33;1mChoose the server you want to update\033[0m"
Help $0 list
read -p "Your choose:" sername
case $sername in
	help)
		Help $0
		;;
	a|A|api)
		IPlist api "${api[*]}"
		UpdateS api "${api[*]}"
		;;
	b|B|admin)
		IPlist admin "${admin[*]}"
		UpdateS admin "${admin[*]}"
		;;
	c|C|pc)
		IPlist pc "${pc[*]}"
		UpdateS pc "${pc[*]}"
		;;
	d|D|swoole)
		IPlist swoole "${swoole[*]}"
		UpdateS swoole "${swoole[*]}"
		;;
	e|E|users)
		IPlist users "${users[*]}"
		UpdateS users "${users[*]}"
		;;
	f|F|transaction)
		IPlist transaction "${transaction[*]}"
		UpdateS transaction "${transaction[*]}"
		;;
	g|G|products)
		IPlist products "${products[*]}"
		UpdateS products "${products[*]}"
		;;
	h|H|orders)
		IPlist orders "${orders[*]}"
		UpdateS orders "${orders[*]}"
		;;
	i|I|fullscale)
		IPlist fullscale "${fullscale[*]}"
		UpdateS fullscale "${fullscale[*]}"
		;;
	j|J|repayment)
		IPlist repayment "${repayment[*]}"
		UpdateS repayment "${repayment[*]}"
		;;
	k|K|capital)
		IPlist capital "${capital[*]}"
		UpdateS capital "${capital[*]}"
		;;
	l|L|sysaccount)
		IPlist sysaccount "${sysaccount[*]}"
		UpdateS sysaccount "${sysaccount[*]}"
		;;
	m|M|bases)
		IPlist bases "${bases[*]}"
		UpdateS bases "${bases[*]}"
		;;
	n|N|trusteeship)
		IPlist trusteeship "${trusteeship[*]}"
		UpdateS trusteeship "${trusteeship[*]}"
		;;
	o|O|contract)
		IPlist contract "${contract[*]}"
		UpdateS contract "${contract[*]}"
		;;
	p|P|others)
		IPlist others "${others[*]}"
		UpdateS others "${others[*]}"
		;;
	q|Q|scan)
		IPlist scan "${scan[*]}"
		UpdateS scan "${scan[*]}"
		;;
	*)
		echo -e "\033[31;1mWrong option!\033[0m"
		;;
esac