logrotate: logrotate 程式是一個日誌文件管理工具。用來把舊的日誌文件刪除,並創建新的日誌文件,稱為日誌轉儲或滾動。 作用: 可以根據日誌文件的大小,也可以根據其天數來轉儲,這個過程一般通過 cron 程式來執行 logrotate 相關文件: 計劃任務:/etc/cron.dai ...
logrotate:
logrotate 程式是一個日誌文件管理工具。用來把舊的日誌文件刪除,並創建新的日誌文件,稱為日誌轉儲或滾動。
作用:
可以根據日誌文件的大小,也可以根據其天數來轉儲,這個過程一般通過 cron 程式來執行
logrotate 相關文件:
-
計劃任務:/etc/cron.daily/logrotate
-
程式文件:/usr/sbin/logrotate
-
配置文件: /etc/logrotate.conf
-
日誌文件:/var/lib/logrotate/logrotate.status
說明:
-
logrotate程式是系統自帶的。
-
logrotate的實現是基於計劃任務的。程式本身被計劃任務調用
#每天執行一次logrotate,會調用/etc/logrotate.conf定義的日誌轉儲規則來對日誌進行操作
[root@LogServer ~]# vim /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit $EXITVALUE
#這個文件定義了日誌轉儲的規則
#這是一個全局性配置,沒有特殊約定的都按照這個規則執行
[root@LogServer ~]# vim /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly #每周做一次日誌的轉儲
# keep 4 weeks worth of backlogs
rotate 4 #表示只保存最近的四個日誌文件
# create new (empty) log files after rotating old ones
create #表示老日誌文件滾動以後,創建一個同名的新日誌文件
# use date as a suffix of the rotated file
dateext #舊的日誌文件會添加一個時間尾碼
# uncomment this if you want your log files compressed
#compress
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d #存放指定服務的配置
# system-specific logs may be also be configured here.
例如:
#btmp日誌轉儲的配置
[root@LogServer logrotate.d]# cat btmp
# no packages own btmp -- we'll rotate it here
/var/log/btmp { #/var/log/btmp表示對btm這個文件定義轉儲規則
missingok #如果日誌不存在,不提示錯誤,繼續處理下一個
monthly #表示一個月轉儲一次
create 0600 root utmp #生成新文件的許可權 所有者 所屬組
rotate 1 #只保存最近的一個文件
}
範例: 設置nginx的日誌轉儲
cat /etc/logrotate.d/nginx
/var/log/nginx/*.log { #/var/log/nginx路徑下只要是log結尾的文件都進行轉儲 -- 表示針對誰進行規則配置
daily #指定轉儲周期為每天
rotate 100 #表示保留最近的100哥文件
missingok #如果日誌不存在,不提示錯誤,繼續處理下一個
compress #對轉儲的日誌文件加逆行壓縮
delaycompress #和compress一起使用時,轉儲的日誌文件到下一次轉儲時才壓縮。延遲壓縮
notifempty #not if empty:如果日誌文件為空舊不進行轉儲
create 644 ngnix nginx #創建的新文件 文件許可權-所有者-所屬組
postrotate #
if [ -f /app/nginx/logs/nginx.pid ]; then #如果進程生成的這個pid文件存在
kill -USR1 `cat /app/nginx/logs/nginx.pid` # 在轉儲以後需要執行的命令 發送一個信號重新載入nginx進程
fi
endscript #postrotate和endscript中的內容為對日誌文件轉儲後的操作
}
範例:對指定日誌手動執行日誌轉儲
#生成測試日誌
[root@centos8 ~]#dd if=/dev/zero of=/var/log/test1.log bs=2M count=1
#創建日誌轉儲配置文件
[root@centos8 ~]#cat /etc/logrotate.d/test1
/var/log/test1.log { #表示要對那個文件進行轉儲
daily #每天轉儲一次
rotate 5 #只保存最近的五個文件
compress #對轉儲的日誌文件進行壓縮
delaycompress #不立即壓縮,而是再下一次轉儲的時候再壓縮
missingok #如果這個文件不存在,不提示錯誤
size 1M #文件大小超過1M就進行轉儲
notifempty #如果/var/log/test1.log為空就不進行轉儲
create 640 bin nobody #新創建的/var/log/test1.log 文件許可權為640,所有者為bin,所屬組為nobody
postrotate
echo `date +%F_%T` >> /var/log/test1.log #轉儲完成後,對新生成的/var/log/test1.log執行操作
endscript
}
#手動執行日誌轉儲
[root@LogServer log]# logrotate /etc/logrotate.d/test1
#查看內容
[root@LogServer log]# cat test1.log
2022-10-20_15:54:26
觸發總配置文件:子配置文件裡面沒定義的就按照總配置文件的對應配置來進行轉儲。
配置參數:
配置參數 | 說明 |
---|---|
compress | 通過gzip壓縮轉儲以後的日誌 |
nocompress | 不壓縮 |
copytruncate | 用於還在打開中的日誌文件,把當前日誌備份並截斷 |
nocopytruncate | 備份日誌文件但是不截斷 |
create mode owner group | 轉儲文件,使用指定的許可權,所有者,所屬組創建新的日誌文件 |
nocreate | 不建立新的日誌文件 |
delaycompress | 和 compress 一起使用時,轉儲的日誌文件到下一次轉儲時才壓縮 |
nodelaycompress | 覆蓋 delaycompress 選項,轉儲同時壓縮 |
errors address | 專儲時的錯誤信息發送到指定的Email 地址 |
ifempty | 即使是空文件也轉儲,此為預設選項 |
notifempty | 如果是空文件的話,不轉儲 |
mail address | 把轉儲的日誌文件發送到指定的E-mail 地址 |
nomail | 轉儲時不發送日誌文件 |
olddir directory | 轉儲後的日誌文件放入指定目錄,必須和當前日誌文件在同一個文件系統 |
noolddir | 轉儲後的日誌文件和當前日誌文件放在同一個目錄下 |
prerotate/endscript | 在轉儲以前需要執行的命令,這兩個關鍵字必須單獨成行 |
postrotate/endscript | 在轉儲以後需要執行的命令,這兩個關鍵字必須單獨成行 |
daily | 指定轉儲周期為每天 |
weekly | 指定轉儲周期為每周 |
monthly | 指定轉儲周期為每月 |
rotate count | 指定日誌文件刪除之前轉儲的次數,0 指沒有備份,5 指保留5 個備份 |
tabooext [+] list | 讓logrotate不轉儲指定擴展名的文件,預設的擴展名是:.rpm-orig,.rpmsave, v, 和 ~ |
size size | 當日誌文件到達指定的大小時才轉儲,bytes(預設)及KB或MB |
sharedscripts | 預設,對每個轉儲日誌運行prerotate和postrotate腳本,日誌文件的絕對路徑作為第一個參數傳遞給腳本。 這意味著單個腳本可以針對與多個文件匹配的日誌文件條目多次運行(例如/ var / log / news /*.example)。如果指定此項sharedscripts,則無論有多少個日誌與通配符模式匹配,腳本都只會運行一次 |
nosharedscripts | 針對每一個轉儲的日誌文件,都執行一次prerotate 和 postrotate腳本,此為預設值 |
missingok | 如果日誌不存在,不提示錯誤,繼續處理下一個 |
nomissingok | 如果日誌不存在,提示錯誤,此為預設值 |