logrotate 使用詳解

logrotate是個十分有用的工具,它能夠自動對日誌進行截斷(或輪循)、壓縮以及刪除舊的日誌文件。例如,你能夠設置logrotate,讓/var/log/foo日誌文件每30天輪循,並刪除超過6個月的日誌。配置完後,logrotate的運做徹底自動化,沒必要進行任何進一步的人爲干預。node

主流Linux發行版上都默認安裝有logrotate包,若是出於某種緣由,logrotate沒有出如今裏頭,你可使用apt-get或yum命令來安裝。vim

在Debian或Ubuntu上:dom

# apt-get install logrotate cron

在Fedora,CentOS或RHEL上:ide

# yum install logrotate crontabs

logrotate的配置文件是/etc/logrotate.conf,一般不須要對它進行修改。日誌文件的輪循設置在獨立的配置文件中,它(們)放在/etc/logrotate.d/目錄下。工具

Logrotate有許多可配置參數,也可以使用man命令來查詢:post

compress                        經過gzip壓縮轉儲之後的日誌
nocompress                      不壓縮
copytruncate                    用於還在打開中的日誌文件,把當前日誌備份並截斷
nocopytruncate                  備份日誌文件可是不截斷
create mode owner group         轉儲文件,使用指定的文件模式建立新的日誌文件
nocreate                        不創建新的日誌文件
delaycompress 和 compress        一塊兒使用時,轉儲的日誌文件到下一次轉儲時才壓縮
nodelaycompress                 覆蓋 delaycompress 選項,轉儲同時壓縮。
errors address                   專儲時的錯誤信息發送到指定的Email 地址
ifempty                         即便是空文件也轉儲,這個是 logrotate 的缺省選項。
notifempty                      若是是空文件的話,不轉儲
mail address                    把轉儲的日誌文件發送到指定的E-mail 地址
nomail                          轉儲時不發送日誌文件
olddir directory                轉儲後的日誌文件放入指定的目錄,必須和當前日誌文件在同一個文件系統
noolddir                        轉儲後的日誌文件和當前日誌文件放在同一個目錄下
prerotate/endscript             在轉儲之前須要執行的命令能夠放入這個對,這兩個關鍵字必須單獨成行
postrotate/endscript            在轉儲之後須要執行的命令能夠放入這個對,這兩個關鍵字必須單獨成行
daily                           指定轉儲週期爲天天
weekly                          指定轉儲週期爲每週
monthly                         指定轉儲週期爲每個月
rotate count                    指定日誌文件刪除以前轉儲的次數,0 指沒有備份,5 指保留5 個備份
tabootext [+] list 讓logrotate   不轉儲指定擴展名的文件,缺省的擴展名是:.rpm-orig, .rpmsave, v, 和 ~ 
size size                       當日志文件到達指定的大小時才轉儲,bytes(缺省)及KB(sizek)或MB(sizem)

樣例一

在第一個樣例中,咱們將建立一個10MB的日誌文件/var/log/log-file。咱們將展現怎樣使用logrotate來管理該日誌文件。ui

咱們從建立一個日誌文件開始吧,而後在其中填入一個10MB的隨機比特流數據。spa

# touch /var/log/log-file

# head -c 10M < /dev/urandom > /var/log/log-file

因爲如今日誌文件已經準備好,咱們將配置logrotate來輪循該日誌文件。讓咱們爲該文件建立一個配置文件。命令行

# vim /etc/logrotate.d/log-file
/var/log/log-file {
    monthly
    rotate 5
    compress
    delaycompress
    missingok
    notifempty
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}

樣例二

在本例中,咱們只想要輪循一個日誌文件,然而日誌文件大小能夠增加到50MB。日誌

# vim /etc/logrotate.d/log-file
/var/log/log-file {
    size=50M
    rotate 5
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}

 樣例三

咱們想要讓舊日誌文件以建立日期命名,這能夠經過添加dateext常熟實現。

# vim /etc/logrotate.d/log-file
/var/log/log-file {
    monthly
    rotate 5
    dateext
    create 644 root root
    postrotate
        /usr/bin/killall -HUP rsyslogd
    endscript
}

這將讓歸檔文件在它們的文件名中包含日期信息。

排障

這裏提供了一些logrotate設置的排障提示。

1. 手動運行logrotate

logrotate能夠在任什麼時候候從命令行手動調用。

要調用爲/etc/lograte.d/下配置的全部日誌調用logrotate

# logrotate /etc/logrotate.conf

要爲某個特定的配置調用logrotate:

# logrotate /etc/logrotate.d/log-file

2. 演練

排障過程當中的最佳選擇是使用‘-d’選項以預演方式運行logrotate。要進行驗證,不用實際輪循任何日誌文件,能夠模擬演練日誌輪循並顯示其輸出。

# logrotate -d /etc/logrotate.d/log-file

正如咱們從上面的輸出結果能夠看到的,logrotate判斷該輪循是沒必要要的。若是文件的時間小於一天,這就會發生了。

3. 強制輪循

即便輪循條件沒有知足,咱們也能夠經過使用‘-f’選項來強制logrotate輪循日誌文件,‘-v’參數提供了詳細的輸出。

# logrotate -vf /etc/logrotate.d/log-file
reading config file /etc/logrotate.d/log-file
reading config info for /var/log/log-file
 
Handling 1 logs
 
rotating pattern: /var/log/log-file  forced from command line (5 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/log-file
  log needs rotating
rotating log /var/log/log-file, log->rotateCount is 5
dateext suffix '-20140916'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /var/log/log-file.5.gz to /var/log/log-file.6.gz (rotatecount 5, logstart 1, i 5),
old log /var/log/log-file.5.gz does not exist
renaming /var/log/log-file.4.gz to /var/log/log-file.5.gz (rotatecount 5, logstart 1, i 4),
old log /var/log/log-file.4.gz does not exist
. . .
renaming /var/log/log-file.0.gz to /var/log/log-file.1.gz (rotatecount 5, logstart 1, i 0),
old log /var/log/log-file.0.gz does not exist
log /var/log/log-file.6.gz doesn't exist -- won't try to dispose of it
renaming /var/log/log-file to /var/log/log-file.1
creating new /var/log/log-file mode = 0644 uid = 0 gid = 0
running postrotate script
compressing log with: /bin/gzip

4. Logrotate的記錄日誌

logrotate自身的日誌一般存放於/var/lib/logrotate/status目錄。若是處於排障目的,咱們想要logrotate記錄到任何指定的文件,咱們能夠指定像下面這樣從命令行指定。

# logrotate -vf –s /var/log/logrotate-status /etc/logrotate.d/log-file

5. Logrotate定時任務

logrotate須要的cron任務應該在安裝時就自動建立了

相關文章
相關標籤/搜索