繁体   English   中英

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

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

我有一个充满各种应用程序日志的目录。 例:

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

所有日志的时间戳均为YYYYMMDD格式,但您还可以看到其他涉及的数字。

我的目标是编写一个脚本,该脚本可以定期运行一次以遍历此目录并执行以下操作: 对于所有早于1个月的日志文件,基于文件名时间戳记

  • 对于每个月的文件(30〜31个文件),将tar.gz合并为一个文件
  • 将tar.gz文件标记为

App1_201508.tar.gz <-包含所有30个日志文件,因此格式为AppnameYYYYMM.tar.gz

日志文件应用程序名称是静态的(时间戳除外)。

我想有几种方法可以做到这一点,但是我想从stackoverflow的伟大思想中收集想法,以找到最简单的方法。

提前致谢

这是您更新的问题的第三个解决方案:

#!/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

注意:这假定8位数字的第一个块是日期戳,此后的所有内容都与要去的存档无关。

更新: sed脚本不再输出不包含时间戳的文件。

在这里,不确定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,... 

以runwhen或cron为例,每天运行一次脚本

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM