[英]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个月的日志文件,基于文件名时间戳记
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.