我有一个包含许多日志文件的目录,所有这些我都想每天轮换。 为了组织的目的,我希望能够将轮转的日志移动到按date命名的不同目录(或子目录),保持日志的最后一周。
我可以使用logrotate来实现大部分的这种转换文件,甚至将它们移动到一个单一的目录使用olddir指令,但我很难find一个解决scheme,使每个单独的子目录。 我怎样才能做到这一点?
Logs to rotate: /var/log/example/* Desired target directories (keeping a week): /var/log/example/20121006/* [ ... ] /var/log/example/20121012/*
您应该能够在postrotate指令中调用外部脚本:
postrotate /path/to/your.sh endscript
并有脚本做移动,例如:
#!/bin/bash newdir=/var/log/example/`date +%Y%m%d` mkdir $newdir mv /var/log/example.1.gz $newdir find /var/log/example -mindepth 1 -maxdepth 1 -mtime +7 \ -type d -print0 | xargs -0 rm -rf
但是,使用dateext指令可能会更容易。 随着旋转的文件将附加一个时间戳(虽然没有移动到不同的目录)。
有关这两个指令的详细信息,请参阅logrotate(8) 。
你可以在configuration文件中使用“olddir”指令
olddir directory Logs are moved into directory for rotation. The directory must be on the same physical device as the log file being rotated, and is assumed to be relative to the directory holding the log file unless an absolute path name is specified. When this option is used all old versions of the log end up in directory. This option may be overridden by the noolddir option.
https://manpages.debian.org/jessie/logrotate/logrotate.8.en.html