[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. uniapp colorui的使用

    1.首先我们在Hbuilder x中新建一个uniapp的项目,如下图所示,选择 colorUI项目 2.copy 项目文件夹下的colorUI文件夹到你的项目中去,如下图所示 3.打开根目录下的Ap ...

  2. 安装配置intelli IDEA

    效果 操作 去官网下载安装包 下载 Intelli IDEA 下载插件 插件下载 打开IDEA安装目录下的bin目录,找到idea64.exe.vmoptions配置文件 添加配置 打开indea,添 ...

  3. react withRouter高阶组件

    作用:把不是通过路由切换过来的组件中,将react-router 的 history.location.match 三个对象传入props对象上 默认情况下必须是经过路由匹配渲染的组件才存在this. ...

  4. kettle从入门到精通 第三十课 mysql 数据连接常用配置

    1.我们平常用的最多的数据库就是mysql了,这里我以mysql为例说下数据库连接池配置.为啥要用连接池,因为数据库建立连接很费性能,所以就建立连接池(提前建立好一批连接)缓存起来提高性能.下图中my ...

  5. Vue学习:21.mixins混入

    在Vue中,mixins(混入)是一种用于分发Vue组件中可复用功能的灵活机制.它们允许你抽取组件中的共享功能,如数据.计算属性.方法.生命周期钩子等,并将其作为单独的模块复用到多个组件中.这种方式有 ...

  6. flutter 调用环信sdk 实现即时通讯

    首先下载依赖 导包 import 'package:im_flutter_sdk/im_flutter_sdk.dart';登录 import 'package:flutter/material.da ...

  7. iOS:长图切割并转为动画gif——精灵表单sprite Sheet的转化

    iOS:长图切割并转为动画gif--精灵表单sprite Sheet的转化 通常的,iOS显示gif可以将文件转为NSData后再对其进行解析,通过CADisplayLink逐帧进行提取.播放,判断N ...

  8. Java解析微信获取手机号信息

    在微信中,用户手机号的获取通常是通过微信小程序的getPhoneNumber接口来实现的.这个接口允许用户在授权后,将加密的手机号数据传递给开发者.由于隐私保护,微信不会直接提供用户的明文手机号,而是 ...

  9. Godot中鼠标点击3D对象

    Godot中鼠标点击3D对象 方法一:调用RigidBody3D中的input_event事件 RigidBody3D中有信号input_event可以接受鼠标的输入,用这个信号可以处理点击事件. 具 ...

  10. 高通安卓:androidboot.mode参数控制系统流程原理

    高通安卓:androidboot.mode参数控制系统流程原理 参考:https://blog.csdn.net/guofeizhi/article/details/106644773 背景 在做出厂 ...