###设置时间变量
source /etc/profile
DAY=`date -d "-1 days" +%Y%m%d`
EIGHT=`date -d "-8 days" +%Y%m%d`
TOMCAT_PID=`ps aux|grep tomcat|grep -v grep|grep start$|awk '{print $2}'`
##获取本机IP
IP=`ifconfig|grep "inet addr"|egrep "10.10.10|192.168"|grep -v "127.0.0"|grep "Mask:255.255.255"|awk -F[:] '{print $2}'|awk -F " " '{print $1}'`
##获取本机tomcat运行数量
TOMCAT_PROCESSES=`echo "$TOMCAT_PID"|wc -l`
##将tomcat用户和路径组合,已“分割并去掉空格,方便后面取值
TOMCAT_INFO=`ps aux|grep tomcat|grep -v grep|grep start$|awk '{print $12,"\"",$1}'|awk -F= '{print $2}'|sed 's/[[:space:]]//g'`
cd /home/backup
rm -rf ${IP}_${EIGHT}*.tar.gz
#########判断本脚本没有在运行之后才继续进行下一步tar###
COUNT=`ps aux|grep $0|grep -v grep|wc -l`
TEST=`ps aux|grep $0|grep -v grep|awk '{print $2}'|wc -l`
if [[ 2 -lt $COUNT ]]
then
break;
else
for i in `echo $TOMCAT_INFO`
do
tomcat_uid=`echo $i|awk -F"\"" '{print $2}'|sed 's/[[:space:]]//g'`
if [ $tomcat_uid -gt 0 ] 2>/dev/null; ##判断UID是用户还是数字
then tomcat_user=`cat /etc/passwd|grep $tomcat_uid|awk -F: '{$3==tomcat_uid}END{print $1}'` ##数字,则从/etc/passwd中取值
else
tomcat_user=$tomcat_uid
fi
tomcat_conf_path=`echo $i|awk -F"\"" '{print $1}'`
tomcat_conf_dir=`dirname $tomcat_conf_path`
tomcat_path=`echo $tomcat_conf_dir|sed 's/conf$//g'`
tomcat_name=`echo $tomcat_path|sed 's/.$//'|awk -F"/" '{print $NF}'`
echo "$tomcat_path" >> /home/backup/${IP}_${DAY}.tomcat.list
tar -zcvf ${IP}_${DAY}_${tomcat_user}_${tomcat_name}.tar.gz ${tomcat_path} --exclude=${tomcat_path}logs
done
# echo "end" >${DAY}
fi