shell 脚本远程删除日志脚本

shell 脚本远程删除日志脚本

#!/bin/sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

workDir=`dirname $0`
workDir=`cd ${workDir};pwd`
source $workDir/../conf/config/install_config.conf
source $workDir/../conf/config/clear_log.conf
declare -A workersGroupMap=()
workersGroup=(${workers//,/ })
processDefineIds=(${processDefineIds//;/ })
for workerGroup in ${workersGroup[@]}
do
#  echo $workerGroup;
  worker=`echo $workerGroup|awk -F':' '{print $1}'`
  groupName=`echo $workerGroup|awk -F':' '{print $2}'`
  workersGroupMap+=([$worker]=$groupName)
done


mastersHost=(${masters//,/ })
for master in ${mastersHost[@]}
do
  echo "$master master clearing master logs begin"
        ssh -p $sshPort $master  "cd $installPath/logs; pwd;"
        ssh -p $sshPort $master  "find $installPath/logs -mtime +60 -name '*.*' -exec rm -Rf {} \;"
  echo "$master master clearing master logs end"

  for processDefineId in ${processDefineIds[@]}
  do
  	ssh -p $sshPort $master  "cd $installPath/logs;pwd;"
  	   if ( ssh -p $sshPort $master test -e "$installPath/logs/$processDefineId"  ); then
  	   	 echo "clearing master $master $installPath/logs/$processDefineId"
         ssh -p $sshPort $master  "find $installPath/logs/$processDefineId -mtime +7 -name '*.*' -exec rm -Rf {} \;"
       fi
  done
done

for worker in ${!workersGroupMap[*]}
do
  echo "$worker clearing worker logs begin"
        ssh -p $sshPort $worker  "cd $installPath/logs; pwd;"
        ssh -p $sshPort $worker  "find $installPath/logs -mtime +60 -name '*.*' -exec rm -Rf {} \;"
  echo "$worker clearing worker logs end"

  for processDefineId in ${processDefineIds[@]}
  do
  	ssh -p $sshPort $worker  "cd $installPath/logs;pwd;"
  	  if ( ssh -p $sshPort $worker test -e "$installPath/logs/$processDefineId"  ); then
  	  	echo "clearing worker $worker $installPath/logs/$processDefineId"
        ssh -p $sshPort $worker  "find $installPath/logs/$processDefineId -mtime +7 -name '*.*' -exec rm -Rf {} \;"
      fi
  done
done

echo "$alertServer  clearing alert logs begin"
ssh -p $sshPort $alertServer  "cd $installPath/logs; find $installPath/logs -mtime +90 -name '*.*' -exec rm -Rf {} \;"
echo "$alertServer  clearing alert logs end"

apiServersHost=(${apiServers//,/ })
for apiServer in ${apiServersHost[@]}
do
  echo "$apiServer clearing api server begin"
        ssh -p $sshPort $apiServer  "cd $installPath/logs; pwd;"
        ssh -p $sshPort $apiServer  "find $installPath/logs -mtime +90 -name '*.*' -exec rm -Rf {} \;"
  echo "$apiServer clearing api server end"
done

上面这段脚本的逻辑是logs文件夹下删除60天以外的日志,特定文件夹下删除7天以外的日志。
需要通过ssh遍历多台机器,判断如果存在

$installPath/logs/$processDefineId

这样的文件夹才删除。

if语句这样写不行,他是判断本台机器上面是否存在这个目录

if [ -e "$installPath/logs/$processDefineId"  ]

需要写成下面这样

if ( ssh -p $sshPort $master test -e "$installPath/logs/$processDefineId"  )

通过ssh登录后判断。

参考:
https://blog.csdn.net/weixin_39651356/article/details/125901789

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值