shell脚本实践:自动清理脚本,最低保留120天

目录

背景:

需求:

shell脚本开发过程:

shell脚本功能验证:

脚本执行日志记录:

定时任务配置:

总结:


背景:

运维工作中遇见了需要定期去清理日志文件,但是每次都人工的方式介入的,这样的方式有点浪费人力,同时降低了运维工作的效率,可以开发一个shell脚本去自动清理日志文件。

需求:

1、日志文件所在的目录是/home/service/logs/ 

2、日志文件是名称+日期的形式存在的,需要清理的是gateway_error.log.*和gateway_error.log.*日志文件

3、最低保留120天

4、删除的时候需要判断该日志文件所在的磁盘使用率,阀值为75%,低于则不清理,高于则清理

5、并要求是每天的晚上的23:30分开始执行

6、执行的过程需要日志记录并保留在指定的文件中

shell脚本开发过程:

#!/bin/bash
#author:jiang
#time:2022-06-06
# 清理日志文件,最低保留日志文件的修改时间的前120天的,,最大保留日志文件前150天的,保留的原则根据根目录的使用率,75%为阀值

log_output () {
    if [ ! -f /root/bin/clean-log.log ]
    then
        cd /root/bin/
        touch /root/bin/clean-log.log
    fi
    echo -e "\e[$1m[$(date +"%F %T")] $2 \e[0m" 
}

for ((i=150;i>120;i--))
do
    use=`df -h | grep /dev/mapper/vg_yqdb2-lv_root | awk -F" " '{print $5}' | awk -F"%" '{print $1}'`
    if [ $use -ge 75 ]
    then
        find /home/service/logs/ -type f -name "gateway_error.log.*" -mtime +$i -exec rm {} \;  
        find /home/service/logs/ -type f -name "gateway_debug.log.*" -mtime +$i -exec rm {} \;
        log_output 32 "INFO Clear the gateway_error.log and gateway_debug.log log files in the /home/service/logs/ path $i day before" | tee -a /root/bin/clean-log.log
    else
        log_output 32 "INFO The disk  / usage of the root directory is $use located has a utilization rate of, and there is no need to clean up log files" | tee -a /root/bin/clean-log.log
        break
    fi
done

shell脚本功能验证:

[root@db1 bin]# sh -x del_log.sh 
+ (( i=150 ))
+ (( i>120 ))
++ df -h
++ grep /dev/mapper/vg_yqdb2-lv_root
++ awk '-F ' '{print $5}'
++ awk -F% '{print $1}'
+ use=
+ '[' -ge 75 ']'
del_log.sh: line 18: [: -ge: unary operator expected
+ log_output 32 'INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files'
+ tee -a /root/bin/clean-log.log
+ '[' '!' -f /root/bin/clean-log.log ']'
++ date '+%F %T'
+ echo -e '\e[32m[2022-06-06 16:29:43] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files \e[0m'
[2022-06-06 16:29:43] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
+ break

脚本执行日志记录:

 

[root@yqdb1 bin]# cat clean-log.log 
[2022-06-06 16:03:16] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:03:17] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:04:43] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 
[2022-06-06 16:29:43] INFO The disk  / usage of the root directory is  located has a utilization rate of, and there is no need to clean up log files 

定时任务配置:

[root@yqdb1 bin]# crontab -l

30 23 * * * /bin/bash /root/bin/del_log.sh > /dev/null 2>&1

总结:


如上就完成了自动清理日志文件脚本的开发过程,在实际的运维工作的,日志文件的清理时一个经常要做,但是又很重复的事情,这样的工作我们可以使用脚本来提高我们运维工作效率,同时也能实现运维的自动化。感谢大家的阅读,对大家有帮助的记得多多点赞加关注!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jiang0615csdn

你对鼓励是我最大的动力来源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值