客户端脚本:
#!/bin/bash
echo -e "\033[32m 命令介绍如下:\033[0m"
echo 'backup : 备份'
echo 'start : 启动'
echo 'restart : 重启'
echo 'stop : 停止'
echo 'rsync : 同步'
echo 'rollback : 回滚'
read -p "请输入要同步的命令: " command
if [ "$command" == "" ];then
echo "请输入你要进行的操作:{backup start stop restart rsync rollback }"
elif ([ "$command" != "start" ] && [ "$command" != "stop" ] && [ "$command" != "restart" ] && [ "$command" != "backup" ] && [ "$command" != "rsync" ] && [ "$command" != "rollback" ]);then
echo "请输入你要进行的操作:{backup start stop restart rsync rollback }"
elif([ "$command" == "rsync" ]);then
read -p "请输入要同步到的服务器ip地址: " ip
read -p "请输入要同步到的文件名称: " file_name
echo $file_name
/usr/bin/rsync -avzogP --stats --progress --include="$file_name" --exclude="/*" --password-file=/usr/local/rsync/backup.secrets /usr/local/tomcat/webapps/ rsync@$ip::webapps
else
echo "/home/www/tomcat_m_20021 $command" > /usr/local/tomcat/command
read -p "请输入要同步到的服务器ip地址: " ip
/usr/bin/rsync -avzogP --stats --progress --password-file=/usr/local/rsync/backup.secrets --include='command' --exclude='/*' /usr/local/tomcat/ rsync@$ip::rsync
fi
线上脚本如下:(但是要开启定时任务,不挺扫描客户端的请求)
#!/bin/sh
Command_file="/usr/local/tomcat/command"
Project_name=`cat $Command_file |awk '{print $1}'`
Command=`cat $Command_file |awk '{print $2}'`
my_Back="webapps_backup"
echo $Command
if [ -n "$Command" ];then
case "$Command" in
start)
export JAVA_HOME=/usr/local/java/jdk1.6.0_23
cd $Project_name
rm -rf work/*
$Project_name/bin/startup.sh
`/bin/cat /dev/null >$Command_file`
sleep 3
/usr/bin/tail -1000 $Project_name/logs/catalina.out
echo -e "\033[32m启动成功\033[0m"
;;
stop)
pidlist=`ps -ef|grep $Project_name|grep -v "grep" |awk '{print $2}'`
echo "tomcat Id list :$pidlist"
kill -15 $pidlist
echo "KILL $pidlist:"
sleep 30
tompid=`ps -ef|grep $Project_name|grep -v "grep" |awk '{print $2}'`
if ( [ -n "$tompid" ] && [ $tompid == $pidlist ] );then
echo -e "\033[31m停止失败,请重新执行一次命令\033[0m"
`/bin/cat /dev/null >$Command_file`
else
echo -e "\033[32m停止成功\033[0m"
`/bin/cat /dev/null >$Command_file`
fi
;;
restart)
export JAVA_HOME=/usr/local/java/jdk1.6.0_23
pidlist=`ps -ef|grep $Project_name|grep -v "grep"|awk '{print $2}'`
echo "tomcat Id list :$pidlist"
kill -15 $pidlist
echo "KILL $pidlist:"
echo "stop success"
echo "start tomcat"
cd $Project_name
rm -rf work/*
cd bin
./startup.sh
`/bin/cat /dev/null >$Command_file`
sleep 3
/usr/bin/tail -1000 $Project_name/logs/catalina.out
echo -e "\033[32m重启成功\033[0m"
;;
backup)
cd $Project_name
before_num=`/bin/ls webapps_backup/ |wc -l`
`/bin/find -type d -name "webapps_[0-9]*" -mtime +10 | xargs rm -fr`
echo $my_Back
if [ ! -d "$my_Back" ]; then
`/bin/mkdir $my_Back`
fi
/bin/cp -frp webapps $my_Back/webapps_$(/bin/date -d "today" +"%Y%m%d_%H%M")
`/bin/cat /dev/null >$Command_file`
after_num=`/bin/ls webapps_backup/ |wc -l`
if [ $after_num -eq $(($before_num+1)) ];then
echo -e "\033[32m备份成功\033[0m"
else
echo -e "\033[31m备份失败,请重新备份\033[0m"
fi
;;
rollback)
file_name=`/bin/ls -ltc $Project_name/$my_Back|sed -n 2p|awk '{print $9}'`
echo "$Project_name/$my_Back/$file_name"
/bin/cp -frp $Project_name/$my_Back/$file_name $Project_name/$my_Back/webapps
/bin/cp -frp $Project_name/$my_Back/webapps $Project_name
`/bin/cat /dev/null >$Command_file`
rm -fr $Project_name/$my_Back/webapps
cd $Project_name
if ([ "`find ./ -maxdepth 1 -type d -cmin -1|sed -n 2p`" == './webapps' ]);then
echo -e "\033[32m回滚成功\033[0m"
else
echo -e "\033[31m回滚失败,请重新执行一次\033[0m"
fi
;;
*)
echo "Usage: $0 {start|stop|restart}"
cat /dev/null >$Command_file
exit 1
esac
exit 0
fi