linux 通过prerotate脚本logrotate多个文件

zvokhttg  于 2023-08-03  发布在  Linux
关注(0)|答案(1)|浏览(94)

我有“ansible作业日志DIR”/var/log/ansible,其中存储所有ansible日志。
例如,在

# ll
total 35688
-rw-r--r--. 1 root root     5874 May 16 07:28 ansible_20230516_072619.log
-rw-r--r--. 1 root root     2937 May 16 07:35 ansible_20230516_073516.log
-rw-r--r--. 1 root root     2937 May 16 07:37 ansible_20230516_073654.log
-rw-r--r--. 1 root root     2937 May 16 07:41 ansible_20230516_074104.log
...
-rw-r--r--. 1 root root    11476 Jun 27 11:14 ansible_20230627_111401.log
-rw-r--r--. 1 root root    33871 Jun 27 11:26 ansible_20230627_111442.log
-rw-r--r--. 1 root root    61888 Jun 27 11:57 ansible_20230627_115531.log

字符串
我想通过logrotate做家务:因为有太多的“小”日志文件,我想压缩所有文件旧然后1个月。所以我使用prerotate脚本将文件在旋转之前移动到文件夹/var/log/ansible/archive。但logrotate不工作。目标是每个月都有一个压缩文件。此文件将包含特定月份的所有日志。
这是我的会议

/var/log/ansible/archive/*.log {
    missingok
    notifempty
    compress
    delaycompress
    monthly
    rotate 30
    maxage 30
    prerotate
        find /var/log/ansible/ -name "*.log" -type f -mtime +30 -exec mv {} /var/log/ansible/archive/ \;
    endscript
}


输出量:

logrotate -df /etc/logrotate.d/ansible
Creating new state

Handling 1 logs

rotating pattern: /var/log/ansible/archive/*.log  forced from command line (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/ansible/archive/*.log
  log /var/log/ansible/archive/*.log does not exist -- skipping
Creating new state


//更新//
好吧,我删除了prerotate脚本,看起来它开始工作的东西,但我不能看到任何旋转的文件。正如我前面提到的,我的目标是将所有1个月前的文件旋转/压缩到一个文件中,而不是每个文件单独旋转/压缩,因此我之前使用了这个prerotate脚本。

Creating new state

Handling 1 logs

rotating pattern: /var/log/ansible/*.log  forced from command line (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/ansible/ansible_20230516_072619.log
Creating new state
  Now: 2023-07-24 13:16
  Last rotated at 2023-07-24 13:00
  log needs rotating
...
renaming /var/log/ansible/ansible_20230627_115531.log.1.gz to /var/log/ansible/ansible_20230627_115531.log.2.gz (rotatecount 30, logstart 1, i 1),
renaming /var/log/ansible/ansible_20230627_115531.log.0.gz to /var/log/ansible/ansible_20230627_115531.log.1.gz (rotatecount 30, logstart 1, i 0),
log /var/log/ansible/ansible_20230627_115531.log.31.gz doesn't exist -- won't try to dispose of it
set default create context to unconfined_u:object_r:var_log_t:s0
renaming /var/log/ansible/ansible_20230627_115531.log to /var/log/ansible/ansible_20230627_115531.log.1
compressing log with: /bin/gzip
# logrotate.d]#

...

ll /var/log/ansible/ansible_20230627_115531.log.1.gz
ls: cannot access '/var/log/ansible/ansible_20230627_115531.log.1.gz': No such file or directory

ryhaxcpt

ryhaxcpt1#

看起来你只需要删除你的prerotate脚本和delaycompress关键字;
logrotate已经压缩了你的文件,因为compress关键字
如果你查看/etc/logrotate.d,你会发现很多logrotate的脚本。
/var/log中,您可以看到它们的结果。
看看这些,你可以很容易地调整你的配置文件

相关问题