簡體   English   中英

logrotate 包含日志文件的整個目錄

[英]logrotate entire directory containing log files

有沒有辦法使用 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

Logrotate僅對目錄中的單個文件進行操作,而不是作為單個實體的整個目錄。 最直接的解決方案是在該目錄上調用gzip之類的cronjob,然后根據需要移動/刪除文件。

考慮到 LOG_DIR 沒有其他會被無意刪除的 tarball,安排為 crontab 的簡單 shell 腳本應該可以工作:

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

您可以放置多個路徑,因此您可以將同一文件用於目錄中的多個單獨日志。 然后,您可以編寫一個腳本以在日志輪換文件之前添加新路徑並將其設置在 cron 上。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM