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