简体   繁体   English

Linux shell脚本将1个月以上的tar.gzip日志文件按月分组

[英]Linux shell script to tar.gzip log files older than 1 month grouped by month

I have a directory full of various application logs. 我有一个充满各种应用程序日志的目录。 Example: 例:

FailedAudit_20150101_000000.log FailedAudit_20150209_000000.log FailedAudit_20150316_000000.log stats20150116.log stats20150224.log FailedAudit_20150102_000000.log FailedAudit_20150210_000000.log FailedAudit_20150317_000000.log stats20150117.log stats20150225.log FailedAudit_20150103_000000.log RepoV4Error20150227.log FailedAudit_20150101_000000.log FailedAudit_20150209_000000.log FailedAudit_20150316_000000.log stats20150116.log stats20150224.log FailedAudit_20150102_000000.log FailedAudit_20150210_000000.log FailedAudit_2015.stat20sStats.2015FailedAudit_2015.Stats_Stats.2015

All the logs have timestamp in format YYYYMMDD but also other numbers involved as you can see. 所有日志的时间戳均为YYYYMMDD格式,但您还可以看到其他涉及的数字。

My objective is to write a script that can be run once periodically to go through this directory and do the following: For all log files older than 1 month, based on filename timestamp 我的目标是编写一个脚本,该脚本可以定期运行一次以遍历此目录并执行以下操作: 对于所有早于1个月的日志文件,基于文件名时间戳记

  • for each months worth of files (30~31 files), tar.gz them into one file 对于每个月的文件(30〜31个文件),将tar.gz合并为一个文件
  • label the tar.gz file as 将tar.gz文件标记为

App1_201508.tar.gz <-- contains all 30 log files So format AppnameYYYYMM.tar.gz App1_201508.tar.gz <-包含所有30个日志文件,因此格式为AppnameYYYYMM.tar.gz

The log file application name is static except for the timestamp. 日志文件应用程序名称是静态的(时间戳除外)。

I suppose there is a few ways to do this but I would like to gather ideas from the great minds of stackoverflow to find the simplest way. 我想有几种方法可以做到这一点,但是我想从stackoverflow的伟大思想中收集想法,以找到最简单的方法。

Thanks in advance 提前致谢

Here's the third solution for your updated question: 这是您更新的问题的第三个解决方案:

#!/usr/bin/env bash

LOGTYPES=$( ls *log* | sed -rn "s/([0-9]{6})[0-9]{2}.*$/\1/p" | sort -u )

# the sed command, item by item:
#
# s/ search and replace
# ([0-9]{6}) block of 6 digits, and store it
# [0-9]{2} followed by 2 more digits
# .*$ followed by any and all characters until the end of the input
# / replace all of that with
# \1 the first stored block (the 6 digits)
# /p print the output
#
# So this turns FailedAudit_20150101_000000.log into FailedAudit_201501

THIS_MONTH=$(date +%Y%m)
for LOG in $LOGTYPES; do
    MONTH=${LOG: -6} # Last 6 characters of the LOGTYPE are YYYYMM

    if [[ "$MONTH" -lt "$THIS_MONTH" ]]; then
        LOG_FILES=$(ls ${LOG}*)
        tar -czf ${LOG}.tar.gz ${LOG_FILES}
        RC=$? # Check whether an error occured
        if [[ "$RC" == "0" ]]; then
            rm ${LOG_FILES}
        fi
    fi
done

Note: This assumes that the first block of 8 digits is the datestamp, and everything after that is not relevant for which archive it is to go to. 注意:这假定8位数字的第一个块是日期戳,此后的所有内容都与要去的存档无关。

Update: The sed script no longer outputs files that do not contain a timestamp. 更新: sed脚本不再输出不包含时间戳的文件。

here, not sure if workimg 在这里,不确定workimg

#!/bin/bash
MONTH=$(date +%m)
OLDMONTH=$MONTH-1
for FILE in `ls $DIR`
do
    if [ ${FILE:-4:2} == $OLDMONTH]; then
        # do what you want with the file, it's one month old, eg add it to a list
    fi
done
# do what you want with the list, eg tar,... 

run the script once a day as example with runwhen or cron 以runwhen或cron为例,每天运行一次脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 修改代码以复制/粘贴.zip和.tar.gzip文件吗? - Adapt code to copy/paste .zip and .tar.gzip files? 如何使用 linux shell 脚本为超过 7 天的文件创建 tar - How to create tar for files older than 7 days using linux shell scripting 如何使用文件名中的日期查找一个月以上的文件? - How to find files older than a month using date in filename? 将删除上个月文件的 linux 脚本 - linux script that will delete files from previous month 使用gzip命令通过Shell脚本在tomcat服务器中备份日志文件 - Backup log files in tomcat server by shell script with gzip command 用户输入以在Linux Shell脚本中设置校准月份和年份 - User input to set cal month and year in linux shell script 用于删除 ftp 服务器上超过 x 天的文件的 Shell 脚本 - Shell script to delete files older than x days on ftp server 如何编写一个 Linux shell 脚本来删除早于 X 天的文件,但在修改时间之前留下当天的第一个文件? - How to write a Linux shell script that removes files older than X days, but leaves the first file of the day by modification time? 用于查找早于2010年1月1日的文件的Shell脚本 - Shell script to find files older than 1st Jan 2010 如何删除超过 7 天的文件但在 Amazon S3 中保留每月 1 号的最后 6 个文件? - How to delete files older than 7 days but keep last 6 files of 1st of every month in Amazon S3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM