[linux 管理紀錄] 設定循環紀錄 Log 檔 – logrotate

參考文件

https://officeguide.cc/linux-logrotate-manage-log-rotation-tutorial-examples/
https://www.linuxtechi.com/manage-linux-log-files-using-logrotate/

情境

今天用了 nginx 紀錄 log
但經年累月的 log 越來越肥,但你基本上只會用近一個月的 log

工具

logrotate
目前 linux 基本上已經安裝好的工具

新增檔案
/etc/logrotate.d/nginx

# nginx 記錄檔輪替設定
/var/log/nginx/*.log { # 記錄檔位置
    daily                # 每日輪替一次
    missingok            # 忽略記錄檔不存在問題
    rotate 7            # 保留 14 次輪替的記錄檔
    compress             # 壓縮輪替後的記錄檔
    delaycompress        # 延遲壓縮記錄檔
    notifempty           # 不輪替空的記錄檔
    create 0640 www-data adm # 記錄檔擁有者/群組為 www-data/adm,權限為 0640
    sharedscripts        # 所有記錄檔輪替,只執行一次 prerotate 與 postrotate 指令稿
    prerotate            # 輪替前指令稿
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
            run-parts /etc/logrotate.d/httpd-prerotate; \
        fi \
    endscript
    postrotate           # 輪替後指令稿
        invoke-rc.d nginx rotate >/dev/null 2>&1
    endscript
}
關於

登山嚮導 / IT / 武術/網頁工程 - 貪多而正努力咀嚼的人生小吃貨