linux 定时任务,压缩 日志,并删除掉 指定日期之前的 日志

sh文件

#!/bin/sh
 myPath="/var/www/Client/storage/logs/"
 myFile="lumen.log"
 
 cd $myPath
 
 date=$(date +%Y%m%d)
 
 find ${myPath} -type f -name "*.gz" -mtime +30 -exec rm {} \;
 #这里的-f参数判断$myFile是否存在
 if [ -e "$myFile" ]; then
    mv $myFile ${myFile%.*}${date}.${myFile##*.}
    tar -zcvf ${myFile%.*}${date}.log.tar.gz ${myFile%.*}${date}.${myFile##*.}
    rm -rf $myPath/${myFile%.*}${date}.${myFile##*.}
 fi

  只要定时任务中添加即可,注意 要是同时运行 ,多个脚本的压缩命令。 不要在一个时间 进行 ,会导致 压缩失败

猜你喜欢

转载自www.cnblogs.com/luyidamowang/p/10518797.html