[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     

tar命令备份压缩7天生产日志的更多相关文章

  1. linux使用tar命令打包压缩时排除某个文件夹或文件

    今天在使用tar命令进行文件夹打包压缩的时候,需要打包压缩masalaPage目录,但是该目录中的2017,2016两个目录中的文件不进行打包压缩 所以通常使用的tar -zcvf masalaPag ...

  2. linux zip命令 tar命令 【压缩、解压缩】参数列表:

    linux zip命令参数列表:   -a 将文件转成ASCII模式 -F 尝试修复损坏的压缩文件 -h 显示帮助界面 -m 将文件压缩之后,删除源文件   -n 特定字符串 不压缩具有特定字尾字符串 ...

  3. 03_每周 5 使用 tar 命令备份/var/log 下的所有日志文件

    ]# vim /root/logbak.shtar -czf log-`date +%Y%m%d`.tar.gz /var/log ]# crontab -e -u root00 03 * * 5 / ...

  4. tar命令加密压缩

    场景 Centos6下使用加密压缩,可以从A机器到B机器解压. 可用在kali上解压就不行. 命令 解包 tar zxvf FileName.tar 打包 tar czvf FileName.tar ...

  5. tar命令加密压缩/解密解压

    在tar解压文件时发生下面错误信息 gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not rec ...

  6. linux中tar命令(打包、压缩、解压)、zip和unzip、rar多种压缩文件

    一.名词解释 打包:将一大堆文件或目录变成一个总的文件[tar命令] 压缩:将一个大的文件通过一些压缩算法变成一个小文件[gzip,bzip2等] Linux中很多压缩程序只能针对一个文件进行压缩,这 ...

  7. tar 命令压缩时报错 tar: Removing leading `/' from member names

    在使用tar命令进行压缩打包的时候我们常常会遇到下面的错误.虽然它不会影响我们最后的压缩打包,但是间接说明了我们的命令是有问题的.接下来我们来看看解决的方法. 报错内容: [root@haha ~]# ...

  8. tar命令-解压和压缩文件

    tar命令 可以用来压缩打包单文件.多个文件.单个目录.多个目录. Linux打包命令_tar tar命令可以用来压缩打包单文件.多个文件.单个目录.多个目录. 常用格式: 单个文件压缩打包 tar ...

  9. 学习Linux tar 命令:最简单也最困难

    摘要:在本文中,您将学习与tar 命令一起使用的最常用标志.如何创建和提取 tar 存档以及如何创建和提取 gzip 压缩的 tar 存档. 本文分享自华为云社区<Linux 中的 Tar 命令 ...

  10. liunx之tar 命令

    tar命令 可以用来压缩打包单文件.多个文件.单个目录.多个目录. Linux打包命令_tar tar命令可以用来压缩打包单文件.多个文件.单个目录.多个目录. 常用格式: 单个文件压缩打包 tar ...

随机推荐

  1. MySQL创建表的时候建立联合索引的方法

    1.MySQL创建表建立联合索引的步骤 在MySQL中,联合索引(也称为复合索引或多列索引)是基于表中的多个列创建的索引.这种索引可以提高多列查询的性能,特别是当查询条件涉及这些列时.下面是一个详细的 ...

  2. 【WPF】Dispatcher 与消息循环

    这一期的话题有点深奥,不过按照老周一向的作风,尽量讲一些人鬼都能懂的知识. 咱们先来整个小活开开胃,这个小活其实老周在 N 年前写过水文的,常阅读老周水文的伙伴可能还记得.通常,咱们按照正常思路构建的 ...

  3. 新手入门html 表格 表单 超链接 图片

    超链接标签 <a></a> A里面的html属性 href="所要跳转到的目标连接" <a href="http:是必须加的"&g ...

  4. webpack配置css预处理

    webpack默认只支持js的打包,不支持其它类型,为了让它支持样式的打包就需要加载一些loader 打包css文件 在webpack中配置对应的loader 在入口js文件中通过import导入样式 ...

  5. vuex做购物车功能

    先创建一个cart组件 <template> <div> <ListItem></ListItem> </div> </templat ...

  6. ABC330

    D 记录每一行,每一列有多少个 o,然后统计答案即可. code E 想到 \(mex^{i \le n}_{i = 1} a_i \le n\) 这整个题就可做了(赛时因为没想到这个,痛失 \(47 ...

  7. LINQ to Entities does not recognize the method 'System.String ToString()' method

    LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method ca ...

  8. Linux实时查看Java接口数据

    1.Linux实时查看Java接口数据的方法 在Linux系统中实时查看Java接口数据通常涉及几个步骤: (1)编写Java应用程序:首先,我们需要有一个Java应用程序,它暴露了一个或多个HTTP ...

  9. k8s中的亲和性、污点与容忍(调度到不同的宿主机)

    本文章并未经过严格实践,如有错误,辛苦指出. 背景 服务需要多副本,来保证高可靠.多活. 那么问题来了,假如这些副本都在同一个宿主机上,或者同一个交换机下-宿主机.交换机其中一项坏掉了,那多副本还有什 ...

  10. php不使用Office包实现上万条数据导出表格

    经过上传客户要求主副表迁出,又提出可以将某张表的数据导出excel,听着很简单,实际看数据表发现上万条数据,并且需要关联表查询相关字段,导出的表格才可以被客户看明白. 要是使用office包目前后台内 ...