unix logrotate包含日志文件的整个目录

zlwx9yxi  于 2022-11-04  发布在  Unix
关注(0)|答案(3)|浏览(168)

有没有一种方法可以使用logrotate旋转整个目录并压缩它,而不是只压缩某个特定目录中的文件?我尝试使用下面的配置,但没有工作。给予下面的错误消息:
设定:

/path/to/folder/test {
daily
rotate 5
missingok
compress
delaycompress
}

错误:

$logrotate -vf test.conf
reading config file test.conf
reading config info for /path/to/folder/test

Handling 1 logs

rotating pattern: /path/to/folder/test  forced from command line (5 
rotations)
empty log files are rotated, old logs are removed
error: error creating unique temp file: Permission denied
kcrjzv8t

kcrjzv8t1#

Logrotate只对目录中的单个文件进行操作,而不是将整个目录作为一个实体。最直接的解决方案是cronjob,它对该目录调用gzip之类的命令,然后根据需要移动/删除文件。

k0pti3hp

k0pti3hp2#

如果LOG_DIR没有其他可能被无意删除的tar文件,那么一个简单的shell脚本作为crontab应该可以运行:


# !/bin/bash

DIR_ROTATE_DAYS=7
TARBALL_DELETION_DAYS=60
LOG_DIR=/var/log/<program>/

cd $LOG_DIR
log_line "compressing $LOG_DIR dirs that are $DIR_ROTATE_DAYS days old...";
for DIR in $(find ./ -maxdepth 1 -mindepth 1 -type d -mtime +"$((DIR_ROTATE_DAYS - 1))" | sort); do
  echo -n "compressing $LOG_DIR/$DIR ... ";
  if tar czf "$DIR.tar.gz" "$DIR"; then
    echo "done" && rm -rf "$DIR";
  else
    echo "failed";
  fi
done

echo "removing $LOG_DIR .tar.gz files that are $TARBALL_DELETION_DAYS days old..."
for FILE in $(find ./ -maxdepth 1 -type f -mtime +"$((TARBALL_DELETION_DAYS - 1))" -name "*.tar.gz" | sort); do
  echo -n "removing $LOG_DIR/$FILE ... ";
  if rm -f "$LOG_DIR/$FILE"; then
    echo "done";
  else
    echo "failed";
  fi
done
zphenhs4

zphenhs43#

您可以放置多个路径,这样您就可以在一个目录中为多个单独的日志使用同一个文件。然后,您可以编写一个脚本,在日志轮转文件前面添加新路径,并将其设置在cron上。

/path/to/folder/test/file1
/path/to/folder/test/file2
 {
    daily
    rotate 5
    missingok
    compress
    delaycompress
    }

相关问题