logrotate 日誌分割
阿新 • • 發佈:2021-10-02
logrotate可以自動對日誌進行截斷(或輪循)、壓縮以及刪除舊的日誌檔案。例如,可以設定logrotate,讓/var/log/foo日誌檔案每30天輪循,並刪除超過6個月的日誌。配置完後,logrotate的運作完全自動化,不必進行任何進一步的人為干預,舊日誌也可以通過電子郵件傳送
1. 配置
# install logrotate cron # ubuntu # install logrotate crontabs # RHEL # cat /etc/logrotate.d/myserver # myserver /var/log/myserver { notifempty missingok monthly dateext rotate 7 size=100M sharedscripts delaycompress create 644 root root postrotate [ ! -f /var/run/myserver.pid ] || kill -USR2 `cat /var/run/myserver.pid` # /usr/bin/killall -HUP rsyslogd endscript } # for syslog /var/log/cron /var/log/maillog /var/log/messages /var/log/secure /var/log/spooler { missingok sharedscripts postrotate /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true endscript } # for zabbix-agent /var/log/zabbix/zabbix_agentd.log { weekly rotate 12 compress delaycompress missingok notifempty create 0664 zabbix zabbix } # for nginx /var/log/nginx/*log { create 0644 nginx nginx daily rotate 7 missingok notifempty compress sharedscripts postrotate /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true endscript } # for influxdb /var/log/influxdb/influxd.log { daily rotate 7 missingok dateext copytruncate compress }
2. 引數
完整引數參考 man logrotate compress 通過gzip 壓縮轉儲以後的日誌 nocompress 不壓縮 copytruncate 把當前日誌備份並截斷,先拷貝原日誌檔案再清空,由於拷貝和清空之間有一個時間差,可能會丟失部分日誌資料。 nocopytruncate 備份日誌檔案但是不截斷 create mode owner group 轉儲檔案,使用指定的檔案模式建立新的日誌檔案 nocreate 不建立新的日誌檔案 delaycompress 和compress一起使用,轉儲的日誌檔案到下一次轉儲時才壓縮,仍然需要讀取最新歸檔時很有用 nodelaycompress 覆蓋 delaycompress 選項,轉儲同時壓縮。 errors address 專儲時的錯誤資訊傳送到指定的Email 地址 missingok: 在日誌輪循期間,任何錯誤將被忽略,例如“檔案無法找到”之類的錯誤 ifempty 即使是空檔案也轉儲,這個是 logrotate 的預設選項。 notifempty 如果是空檔案的話,不轉儲 mail address 把轉儲的日誌檔案傳送到指定的E-mail 地址 nomail 轉儲時不傳送日誌檔案 olddir directory 轉儲後的日誌檔案放入指定的目錄,必須和當前日誌檔案在同一個檔案系統 noolddir 轉儲後的日誌檔案和當前日誌檔案放在同一個目錄下 prerotate/endscript 在轉儲以前需要執行的命令可以放入這個對,這兩個關鍵字必須單獨成行 daily 指定轉儲週期為每天 weekly 指定轉儲週期為每週 monthly 指定轉儲週期為每月 rotate count 指定日誌檔案刪除之前轉儲的次數,0指沒有備份,5指保留5個備份,對於第六個歸檔,時間最久的歸檔將被刪除 tabootext [+] list 讓logrotate 不轉儲指定副檔名的檔案,預設的副檔名是:.rpm-orig, .rpmsave, v, 和 ~ size size 當日志文件到達指定的大小時才轉儲,Size 可以指定 bytes (預設)以及KB(sizek)或者MB (sizem) notifempty: 如果日誌檔案為空,輪循不會進行 create 644 root root: 以指定的許可權建立全新的日誌檔案,同時logrotate也會重新命名原始日誌檔案 postrotate/endscript: 在所有其它指令完成後,postrotate和endscript裡面指定的命令將被執行。在這種情況下,rsyslogd 程序將立即再次讀取其配置並繼續執行 mail address : 把轉儲的日誌檔案傳送到指定的 E-mail 地址 sharedscripts postrotate只會執行一次,在舊日誌被壓縮一次後 dateext 使用當期日期作為命名格式,如果指定的rotate大於1,預設rotate之後的檔名是:xx.log.1
3. 執行
/usr/sbin/logrotate -vf /etc/logrotate.conf logrotate命令格式解釋: logrotate [OPTION...] < configfile > -d, --debug :debug模式,測試配置檔案是否有錯誤,並不會真正進行 rotate 或者 compress 操作 -v, --verbose :顯示轉儲過程 -f, --force :強制轉儲檔案 -m, --mail=command :傳送日誌到指定郵箱。 -s, --state=statefile :使用指定的狀態檔案。 logrotate -vf –s /var/log/logrotate-status /etc/logrotate.d/log-file
4. Logrotate定時任務
logrotate需要的cron任務應該在安裝時就自動建立了
# cat /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
# cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
# 使用 anacrontab 輪轉的配置檔案,日誌切割的生效時間是在凌晨 3 點到 22 點之間,而且隨機延遲時間是 45 分鐘,隨機難以控制
mv /etc/anacrontab /etc/anacrontab.bak
# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
59 23 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
# 或自定義計劃任務
*/30 * * * * /usr/sbin/logrotate /etc/logrotate.d/rsyslog > /dev/null 2>&1 &
-
PS
Linux日誌檔案總管——logrotate
Linux日誌切割神器logrotate原理介紹和配置詳解
Linux自帶神器logrotate詳解