Solaris 中的日志轮换 (zipping/deleting) 脚本

Log rotation (zipping/deleting) script in Solaris

在 linux 环境中有相同的日志轮换文件并且它们在那里工作。在 Solaris 中,我对 运行 这些脚本有问题:

脚本的主要目的是删除所有超过 30 天的日志并压缩所有超过 5 天的日志。使用 -not -name 是因为我只想对旋转的日志文件进行操作,例如 something.log.20181102 因为 .log 文件是当前文件,我不想触摸它们。

#!/bin/bash

find ./logs -mindepth 1 -mtime +30 -type f -not -name "*.log" -delete
find ./logs -mtime +5 -not -name "*.log" -exec gzip {} \;

-mindepth-not 出现问题,因为它给出了错误:

find: bad option -not
find: [-H | -L] path-list predicate-list

基于搜索,我必须在查找中以某种方式使用 -prune,但我不太确定如何使用。

如果您在 Linux(或 Solaris 上的 gfind(1))上查看 find(1) 的手册页,您会看到

-not expr
    Same as ! expr, but not POSIX compliant.

因此您应该能够将 -not 替换为 !,但您需要使用反斜杠或 将其从 shell 中转义]单引号:

find ... \! -name "*.log" ...

请注意,在 Solaris 上,有一个名为 logadm 的命令旨在帮助您处理此类事情,可能值得研究,除非您希望在两个 Solaris 上都具有完全相同的行为和 Linux.

在@Danek Duvall 的帮助下,经过一些搜索,我让它工作了:

find ./logs -mtime +30 -type f ! -name "*.log" -exec rm -f {} \;
find ./logs -mtime +5 ! -name "*.log" -exec gzip {} \;

它会删除所有超过 30 天的日志文件,然后压缩超过 5 天的日志文件。