我正在尝试设置logrotation以每天旋转日志14天。在posst循环中,它将校准一个单独的脚本以压缩任何10天以上的日志。超过14天的文件将被删除。
到目前为止,轮换似乎工作正常,但是鉴于此所需的时间范围,我在进行一些麻烦的测试。有什么方法可以测试这种设置,而无需等待14天,因为这不太实用。
另外,这是我的配置和tar脚本。考虑到我的要求,关于它的一切看起来是否有错?
logrotate.conf
/home/user1/services/logs/*/*.log{
# Rotate logs daily
daily
copytruncate
missingok
notifempty
# Keep 14 days worth of backlogs
rotate 14
maxage 14
# create new (empty) log files after rotating old ones
create
# use date as a suffix of the rotated file
dateext
dateformat .%Y-%m-%d
# compress your logs
nocompress
#postrotate scripts to tar logs that are too old.
postrotate
/home/user1/service/logrotate/archive.sh
endscript
}
archive.sh:
#!/bin/bash
rotateDate=[0-9]{4}-[0-9]{2}-[0-9]{2}
shopt -s nullglob
for file in $HOME/services/logs/*/*.log.[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]; do
log=$(basename "$file")
find . -type f -mtime +10 | tar -zcvf "$file.tar" "$log"
done
编辑:过夜运行脚本后,我注意到archive.sh
无法正常工作。我没有找到较旧的文件并进行皮重处理,而是看到昨夜旋转的日志文件已被皮重处理。
我应该有:
test.log
test.log.2020-04-09
test.log.2020-04-08
然后在文件使用10天之前看不到任何焦油。
相反,我有:
test.log
test.log.2020-04-09
test.log.2020-04-09.tar
test.log.2020-04-08
test.log.2020-04-08.tar
我对logrotate.conf不太了解。
但是,第二期:请查看这些压缩日志文件是否正确,并且在所有压缩文件中都没有包含相同的内容。
您当前正在做的是:
对于与您的全局表达式匹配的每个$file
:
$file
)tar -zcvf "$file.tar" "$log"
因此,您应该在find
命令中包含文件名,并将find的输出用作tar
的参数(例如,使用xargs
):
for file in $HOME/services/logs/*/*.log.[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]; do
log=$(basename "$file")
find . -iname "$file" -type f -mtime +10 | xargs tar -zcvf "$file.tar"
done
如果文件名中有空格,请考虑将-print0
设置为find
,将-0
设置为xargs
或不带xargs
,将参数-T -
用于tar
:
find . -iname "$file" -type f -mtime +10 -print0 | tar -zcvf "$file.tar" --null -T -
但是具体参数可能取决于您的tar版本:How can I build a tar from stdin?