tar命令备份压缩7天生产日志

[root@localhost logs]# cat tar_7day.sh
#!/bin/bash
#压缩日期【当天的前一天】
todayStamp_1=`date -d "-1 day" +%Y%m%d`
#压缩日期【当天的前七天】
sevendaysagoStamp=`date -d "-7 day" +%Y%m%d`
Dirname=/mnt/Dirn_ame
Compressed_name=`ls $Dirname/*.log|awk -F '.' '{print $1}'`
 
#压缩7天日志
[[ -d $Dirname ]] && cd $Dirname && find . -name "*" -mtime -7 -type f |xargs tar -zcvf ${Compressed_name}${sevendaysagoStamp}-${todayStamp_1}.tar.gz

if [ ! -d backup ];then
  mkdir backup && mv ${Compressed_name}${sevendaysagoStamp}-${todayStamp_1}.tar.gz backup/
else
  mv ${Compressed_name}${sevendaysagoStamp}-${todayStamp_1}.tar.gz backup/
fi

sleep 2
# find命令find后面如果是.[当前目录缩写,那么后面的排除目录也必须是缩写,否则不生效];如果是全量目录${Dirname},那么后面的排除目录也必须是全量目录,如果是缩写就不生效。
[[ -d $Dirname ]] && cd $Dirname && find . ! -path "./*.tar.gz" ! -path "./*.log" -name "*" -mtime -7 -type f |xargs rm -rf
 
以上就是压缩脚本
被压缩目录为:
[root@localhost logs]# ll /mnt/Dirn_ame/
total 8
-rw-r--r-- 1 root root   0 Oct 17 02:49 archery-competition.log
-rw-r--r-- 1 root root   0 Oct  1 08:00 archery-competition.log.2023-10-00-0
-rw-r--r-- 1 root root   0 Oct  1 08:10 archery-competition.log.2023-10-01-0
-rw-r--r-- 1 root root   0 Oct  2 08:10 archery-competition.log.2023-10-02-0
-rw-r--r-- 1 root root   0 Oct  3 08:10 archery-competition.log.2023-10-03-0
-rw-r--r-- 1 root root   0 Oct  4 08:10 archery-competition.log.2023-10-04-0
-rw-r--r-- 1 root root   0 Oct  5 08:10 archery-competition.log.2023-10-05-0
-rw-r--r-- 1 root root   0 Oct  6 08:10 archery-competition.log.2023-10-06-0
-rw-r--r-- 1 root root   0 Oct  7 08:10 archery-competition.log.2023-10-07-0
-rw-r--r-- 1 root root   0 Oct  8 08:10 archery-competition.log.2023-10-08-0
-rw-r--r-- 1 root root   0 Oct  9 08:10 archery-competition.log.2023-10-09-0
-rw-r--r-- 1 root root   0 Oct 10 08:10 archery-competition.log.2023-10-10-0
-rw-r--r-- 1 root root   0 Oct 11 08:10 archery-competition.log.2023-10-11-0
-rw-r--r-- 1 root root   0 Oct 12 08:10 archery-competition.log.2023-10-12-0
-rw-r--r-- 1 root root   0 Oct 13 08:10 archery-competition.log.2023-10-13-0
-rw-r--r-- 1 root root   0 Oct 14 08:10 archery-competition.log.2023-10-14-0
-rw-r--r-- 1 root root   0 Oct 15 08:10 archery-competition.log.2023-10-15-0
-rw-r--r-- 1 root root   0 Oct 16 08:10 archery-competition.log.2023-10-16-0
-rw-r--r-- 1 root root   0 Oct 17 08:10 archery-competition.log.2023-10-17-0
-rw-r--r-- 1 root root   0 Oct 18 08:10 archery-competition.log.2023-10-18-0
-rw-r--r-- 1 root root   0 Oct 19 08:10 archery-competition.log.2023-10-19-0
 
压缩后效果

[root@localhost Dirn_ame]# ll
total 4
-rw-r--r-- 1 root root 1158 Oct 20 04:47 20231013-20231019.tar.gz
-rw-r--r-- 1 root root 0 Oct 17 02:49 archery-competition.log
-rw-r--r-- 1 root root 0 Oct 1 08:00 archery-competition.log.2023-10-00-0
-rw-r--r-- 1 root root 0 Oct 1 08:10 archery-competition.log.2023-10-01-0
-rw-r--r-- 1 root root 0 Oct 2 08:10 archery-competition.log.2023-10-02-0
-rw-r--r-- 1 root root 0 Oct 3 08:10 archery-competition.log.2023-10-03-0
-rw-r--r-- 1 root root 0 Oct 4 08:10 archery-competition.log.2023-10-04-0
-rw-r--r-- 1 root root 0 Oct 5 08:10 archery-competition.log.2023-10-05-0
-rw-r--r-- 1 root root 0 Oct 6 08:10 archery-competition.log.2023-10-06-0
-rw-r--r-- 1 root root 0 Oct 7 08:10 archery-competition.log.2023-10-07-0
-rw-r--r-- 1 root root 0 Oct 8 08:10 archery-competition.log.2023-10-08-0
-rw-r--r-- 1 root root 0 Oct 9 08:10 archery-competition.log.2023-10-09-0
-rw-r--r-- 1 root root 0 Oct 10 08:10 archery-competition.log.2023-10-10-0
-rw-r--r-- 1 root root 0 Oct 11 08:10 archery-competition.log.2023-10-11-0
-rw-r--r-- 1 root root 0 Oct 12 08:10 archery-competition.log.2023-10-12-0

drwxr-xr-x 2 root root 121 Oct 24 02:59 backup

[root@localhost pre-jf]# ll backup/
total 16
-rw-r--r-- 1 root root 4247 Oct 24 02:59 archery-competition20231017-20231023.tar.gz     

posted @   存钱罐儿  阅读(96)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示