【问题标题】:logrotate entire directory containing log fileslogrotate 包含日志文件的整个目录
【发布时间】:2018-04-14 10:24:52
【问题描述】:

有没有办法使用 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

【问题讨论】:

  • 也许您只是想要一个压缩目录的每日 cron 作业?我不认为 logrotate 可以处理目录。
  • 感谢您的建议,我尝试查找和试验,但无法正常工作。在得出结论之前,如果我误解或尝试了一些不正确的事情,我想与人们联系。

标签: linux unix logrotate


【解决方案1】:

Logrotate 仅对目录中的单个文件进行操作,而不是将整个目录作为单个实体进行操作。最直接的解决方案是一个 cronjob,它在该目录上调用诸如 gzip 之类的东西,然后在您认为合适的时候移动/删除文件。

【讨论】:

    【解决方案2】:

    一个安排为 crontab 的简单 shell 脚本应该可以工作,因为 LOG_DIR 没有其他会被无意删除的 tarball:

    #!/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
    

    【讨论】:

      猜你喜欢
      • 2018-04-14
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多