[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. 记录工作中常用的 JS 数组相关操作

    工作中难免会遇到各种各样的数据结构,较为全面的了解数组操作,对于复杂数据结构的处理会非常有用且节省时间 所以想在这里总结一下工作中常用的数组操作,都是一些非常基础的知识,大家看个乐就好~ 目录 工作中 ...

  2. 使用Wesky.Net.Opentools库,一行代码实现自动解析实体类summary注释信息(可用于数据实体文档的快速实现)

    使用前,需要对你的项目勾选输出api文档文件. 引用Wesky.Net.OpenTools包,保持1.0.11版本或以上.   为了方便,我直接在昨天的演示基础上,继续给实体类添加注释. 昨天的演示文 ...

  3. 夜莺项目发布 v6.5.0 版本,暗黑菜单来了

    大家好,夜莺项目发布 v6.5.0 版本,启用新 logo,菜单支持换肤,支持了暗黑版本的菜单,下一步会支持全站暗黑主题,敬请期待,下面是新 logo. 暗黑菜单 页面右上角点击用户名,在下拉框里会有 ...

  4. 大模型高效微调-LoRA原理详解和训练过程深入分析

    博客首发于我的知乎,详见:https://zhuanlan.zhihu.com/p/702629428 一.LoRA原理 LoRA(Low-Rank Adaptation of LLMs),即LLMs ...

  5. OpenTelemetry 实践指南:历史、架构与基本概念

    背景 之前陆续写过一些和 OpenTelemetry 相关的文章: 实战:如何优雅的从 Skywalking 切换到 OpenTelemetry 实战:如何编写一个 OpenTelemetry Ext ...

  6. 用 Visual C++ 2022 和 CMake 编译 CUnit 静态库

    准备工作 源代码获取 CUnit 是知名的 C 语言单元测框架,其源代码最初发布在 sourceforge 上,网址为:https://sourceforge.net/projects/cunit/ ...

  7. linux 下新建显示器分辨率

    1. 输入cvt 1920 1080 (假设需要添加的分辨率为1920x1080), 获取Mode Line # 1920x1080 59.96 Hz (CVT 2.07M9) hsync: 67.1 ...

  8. .NET6 个人博客-推荐文章加载优化

    个人博客-推荐文章加载优化 前言 随着博客文章越来越多,那么推荐的文章也是越来越多,之前推荐文章是只推荐8篇,但是我感觉有点少,然后也是决定加一个加载按钮,也是类似与分页的效果,点击按钮可以继续加载8 ...

  9. 高通与At指令:ATFWD解析

    背景 本章的内容是适用于AP侧AT指令开发调试的有关人员. 主要是介绍高通实现的ATFWD框架.在这需要说明一下的是,或许你对AT Command很了解了,但是却貌似都不知道ATFWD,这很正常,严格 ...

  10. Asp .Net Core 系列:基于 Castle DynamicProxy + Autofac 实践 AOP 以及实现事务、用户填充功能

    目录 什么是 AOP ? .Net Core 中 有哪些 AOP 框架? 基于 Castle DynamicProxy 实现 AOP IOC中使用 Castle DynamicProxy 实现事务管理 ...