用shell脚本删除N天外的备份或者日志【原创】

晨光BT群里面有人问,就顺手写了一个


[bash]#!/bin/bash
echo 'Copyright 2010 By [email protected]'
echo 'ClearYour Logs By ihipop.gicp.net for lidaof'

############## SET THE LOGS PATH ###########
logspath=/root/
############################################

######### SET THE MAX NUM TO RESERVE #######
maxnum=2
############################################

######### SET THE HEADER OF LOGS ###########
header=help
############################################
deleteit(){
filetodelete=ls -t|grep ^$header |sed "1,${maxnum}d"
for ihipop in $filetodelete
do
echo deleteing $ihipop
rm -f $ihipop
done
}

cd $logspath
num=ls |grep -c ^$header
if [ "$num" -gt "$maxnum" ];then
echo the file number is $num and it is grater than $maxnum,Starting to detele.....
deleteit
echo all done
else
echo Nothing to do ~~~
fi
exit 0[/bash]

复制上面的代码,另存为为clear.sh

然后

[bash]
chmod +x clear.sh
[/bash]

小小解释一下,

logspath=/root/

定义日志或者备份目录

maxnum=2

定义超过多少个文件开始清理

header=help

定义日志或者备份文件前缀

一下是我的测试过程

首先,我随即创建5个文件,这些文件的时间都不一样,按照创建顺序有先后

[bash]
root@bbs:[/root]rm -f help*
root@bbs:[/root]touch helpdate +%H:%M:%S
root@bbs:[/root]touch helpdate +%H:%M:%S
root@bbs:[/root]touch helpdate +%H:%M:%S
root@bbs:[/root]touch helpdate +%H:%M:%S
root@bbs:[/root]touch helpdate +%H:%M:%S
root@bbs:[/root]ls -t|grep help
help23:02:50
help23:02:48
help23:02:47
help23:02:46
help23:02:44
root@bbs:[/root]./clear.sh
Copyright 2010 By [email protected]
ClearYour Logs By ihipop.gicp.net for lidaof
the file number is 5 and it is grater than 2,Starting to detele.....
deleteing help23:02:47
deleteing help23:02:46
deleteing help23:02:44
all done
root@bbs:[/root]ls -t|grep help
help23:02:50
help23:02:48
root@bbs:[/root]./clear.sh
Copyright 2010 By [email protected]
ClearYour Logs By ihipop.gicp.net for lidaof
Nothing to do ~~
[/bash]

脚本的大概原理就是ls -t列出文件然后按照时间“新->旧”排列,再用管道输送给SED裁减掉头$maxnum个文件名,然后,一个for循环搞定。

^_^

顺便附送一个每天凌晨3点运行的cron

[bash]0 3 * * * /path/of/the/shell/script
[/bash]

关于更加详细的cron解释

请移步这里linux 定时任务 crontab 详细解释!(更新文件解析)


更新CentOS 5/RHEL5上面的情况:

不知道为什么,在公司的CentOS上ls -t是乱的。
ls -t换成ls -ct才对
或者这样写
[bash]#!/bin/bash
echo 'Copyright 2010 By [email protected]'
echo 'ClearYour Logs By ihipop.info'

############## SET THE LOGS PATH ###########
logspath=/root/
############################################

######### SET THE MAX NUM TO RESERVE #######
maxnum=2
############################################

######### SET THE HEADER OF LOGS ###########
header=help
############################################
deleteit(){
maxnum=$(($1-$maxnum))
filetodelete=ls|grep ^$header |head -n${maxnum}
for ihipop in $filetodelete
do
echo deleteing $ihipop
rm -f $ihipop
done
}

cd $logspath
num=ls |grep -c ^$header
if [ "$num" -gt "$maxnum" ];then
echo the file number is $num and it is grater than $maxnum,Starting to detele.....
deleteit $num
echo all done
else
echo Nothing to do ~~~
fi
exit 0[/bash]

Author Info :
  • From:用shell脚本删除N天外的备份或者日志【原创】
  • URL:https://blog.ihipop.com/2010/03/777.html
  • Please Reserve This Link,Thanks!
  • 发表回复

    您的电子邮箱地址不会被公开。 必填项已用*标注