Linux 下Shell脚本回滚删除数据 近期公司一直在做一些OA系统的开发,我在里面主要协助帮忙搭建及维护测试环境,由于环境的特殊性,我几乎每天需要对数据进行备份及还原,所以就应
Linux 下Shell脚本回滚删除数据
近期公司一直在做一些OA系统的开发,我在里面主要协助帮忙搭建及维护测试环境,由于环境的特殊性,我几乎每天需要对数据进行备份及还原,所以就应用到了一些shell脚本,但是在由于服务每天会产生和你多log文件,拷贝及还原的过程中浪费大量的时间及占用磁盘空间,所以最后使用脚本来通过最后的修改时间来保留需要的数据,该脚本适合于很多数据格式,值需要修改对应的文件名格式即可。具体见下:

#!/bin/bash
log_path="/OAFS/WEAVER/ecology/log" #此处定义你的日志及数据文件夹路径
expried_time=7 #此处定义你的日志过期时间,如7天
function deleteLogs(){
# 获取系统时间,所有时间格式都是秒
local currentDate=`date +%s`
echo "current date: " $currentDate
for file in `find $1 -name "ecology_*.log"` #此处定义文件名格式,避免误删
do
local name=$file
local modifyDate=$(stat -c %Y $file)
#对比时间,算出日志存在时间,距离最近一次修改
local logExistTime=$(($currentDate - $modifyDate))
logExistTime=$(($logExistTime/86400))
if [ $logExistTime -gt $expried_time ]; then
echo "File: " $name "Modify Date: " $modifyDate + "Exist Time: " $logExistTime + "Delete: yes"
rm -f $file
else
echo "File: " $name "Modify Date: " $modifyDate + "Exist Time: " $logExistTime + "Delete: no"
fi
done
}
deleteLogs $log_path
我想每天对指定目录下的ecology的log进行删除,然后我只想保留最新的3份数据

所以我们修改对应的保留天数即可,我修改为2天

我们执行脚本,有一个离修改日期比较久的执行删除


我们测试没有问题的话,我们可以使用计划任务对shell脚本进行控制;crontal -e