茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1669027 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_DPP_0081.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

小企鵝開談 : [轉貼]使用 logrotate 來管理及備份日誌檔

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]使用 logrotate 來管理及備份日誌檔
做為一個系統的管理者,時時注意日誌檔是一個非常重要的工作。而日誌檔的備份工作也是個非常重要的,那麼在 FreeBSD 中有無自動備份日誌檔的軟體呢?

我們可以使用 /usr/local/sysutils/logrotate 來達到這個目的..

● logrotate 工作原理說明

1. logrotate 是配合 cron 來使用
2. 一般是以 daily (每日)來執行,可將定時執行的程序放在 /etc/periodic/daily/ 或是使用 crontab 來做
3. 在 logrotate 中我們可設定 daily weekly 或是 monthly 的時間間隔(可視日誌檔的大小及性質來設定)
4. 一般在 FreeBSD 中,有些日誌檔是自動備份的,如

‧console.log
‧cron
‧maillog
‧messages
‧wtmp
.....

有些則不會自動備份及壓縮,其中最大的可能就是 httpd-access.log 這個檔案了。以下我們就以這個檔案做說明

● 安裝 logrotate
# cd /usr/ports/sysutils/logrotate/
# make install

安裝後會產生以下檔案

1. /usr/local/sbin/logrotate 這個執行檔
2. /var/lib/ 這個目錄,在這個目錄中執行 logrotate 後會產生一個 logrotate.status 的報告檔案
3. logrotate 的 man file,你可用 man logrotate 來看相關用法

● 語法設定
logrotate [-f] logrotate.conf

=> -f 一般是在測試時使用,代表 force 的意思
=> logrotate.conf 代表其設定檔,這個一定要設定,檔名可自取,我們可在 /usr/local/etc/ 下設定這個檔案

● logrotate.conf 的設定說明

我們可將 logrotate.conf 放在 /usr/local/etc/ 目錄下,檔名取為 logrotate.conf 以利管理

一個典型的 logrotate.conf 如下:
errors root
compress

/var/log/httpd-access.log {
copytruncate
create 644 root wheel
weekly
rotate 7
uncompress
size=1000k
}

說明:

1. errors => 有錯誤時會 mail 一封信給 root
2. compress => 代表要使用 gzip 來壓縮
3. /var/log/http-access.log { .... } 代表一組設定
4. 在 {...} 外的表示內定值,如 compress 代表要透過 gzip 壓縮,但在 /var/log/httpd-access.log {....} 中,我們使用了 uncomress 則表示不壓縮 httpd-access.log 的備份檔
5. copytruncate => 先將日誌檔 copy 到另一個檔案儲存再清空
6. create ... => 產生一個新的 httpd-access.log 檔,644 root wheel 是這個檔案的權限設定
7. weekly 表示每個星期執行一次,另外尚有 daily 及 monthly 可用
8. rotate => 表示備份檔的數目,如 7 則最多會產生 1-7 個備份檔
9. uncompress => 不壓縮備份檔
10. size=1000k => 代表檔案大於 1000k 時就備份,另外也可使用 m

● 測試
# logrotate -f /usr/local/etc/logrotate.conf

過一會,應該會產生以下檔案

1. 在 /var/log/ 下產生 httpd-access.log.1
2. 新的(空的) httpd-access.log
3. 在 /var/lib/ 下產生一個 logrotate.ststus 的記錄檔

● 定時啟動你的 logrotate

1. 假設我們使用 /etc/periodic/daily/ (每日定時執行的設定目錄)
# cd /etc/periodic/daily

2. 編輯一個定時啟動的檔案及設定為可執行,假設檔名為 101.logrotate
# vi 101.logrotate

=> 加入以下內容並存檔
/usr/local/sbin/logrotate /usr/local/etc/logrotate.conf

=> 設為可執行
# chmod +x 101.logrotate

如此,以後只要時間一到 logrotate 就會自動備份你的日誌檔了..

● 參考資料

man logrotate

原文出處:小紅帽技術論壇 - [教學]使用 logrotate 來管理及備份日誌檔
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]鳥哥的 Linux 私房菜 - 登錄檔的輪替(logrotate)
logrotate 的設定檔

既然 logrotate 主要是針對登錄檔來進行輪替的動作,所以囉,他當然必須要記載『 在什麼狀態下才將登錄檔進行輪替』的設定啊!那麼 logrotate 這個程式的參數設定檔在哪裡呢?呵呵!那就是:
/etc/logrotate.conf
/etc/logrotate.d/
那個 logrotate.conf 才是主要的參數檔案,至於 logrotate.d 是一個目錄, 該目錄裡面的所有檔案都會被主動的讀入 /etc/logrotate.conf 當中來進行!另外,在 /etc/logrotate.d/ 裡面的檔案中,如果沒有規定到的一些細部設定,則以 /etc/logrotate.conf 這個檔案的規定來指定為預設值!

由上面的圖示我們可以清楚的知道,當第一次執行完 rotate 之後,原本的 messages 會變成 messages.1 而且會製造一個空的 messages 給系統來儲存登錄檔。而第二次執行之後,則 messages.1 會變成 messages.2 而 messages 會變成 messages.1 ,又造成一個空的 messages 來儲存登錄檔!那麼如果我們僅設定保留三個登錄檔而已的話,那麼執行第四次時,則 messages.3 這個檔案就會被刪除,並由後面的較新的保存登錄檔所取代!基本的工作就是這樣啦!

那麼多久進行一次這樣的 logrotate 工作呢?這些都記錄在 logrotate.conf 裡面,我們來看一下預設的 logrotate 的內容吧!
[root@www ~]# vim /etc/logrotate.conf

# 底下的設定是 "logrotate 的預設設定值" ,如果個別的檔案設定了其他的參數,
# 則將以個別的檔案設定為主,若該檔案沒有設定到的參數則以這個檔案的內容為預設值!

weekly    <==預設每個禮拜對登錄檔進行一次 rotate 的工作
rotate 4  <==保留幾個登錄檔呢?預設是保留四個!
create    <==由於登錄檔被更名,因此建立一個新的來繼續儲存之意!
#compress <==被更動的登錄檔是否需要壓縮?如果登錄檔太大則可考慮此參數啟動

include /etc/logrotate.d
# 將 /etc/logrotate.d/ 這個目錄中的所有檔案都讀進來執行 rotate 的工作!

/var/log/wtmp {       <==僅針對 /var/log/wtmp 所設定的參數
    monthly           <==每個月一次,取代每週!
    minsize 1M        <==檔案容量一定要超過 1M 後才進行 rotate (略過時間參數)
    create 0664 root utmp <==指定新建檔案的權限與所屬帳號/群組
    rotate 1          <==僅保留一個,亦即僅有 wtmp.1 保留而已。
}
# 這個 wtmp 可記錄登入者與系統重新開機時的時間與來源主機及登入期間的時間。
# 由於具有 minsize 的參數,因此不見得每個月一定會進行一次喔!要看檔案容量。
# 由於僅保留一個登錄檔而已,不滿意的話可以將他改成 rotate 5 吧!

由這個檔案的設定我們可以知道 /etc/logrotate.d 其實就是由 /etc/logrotate.conf 所規劃出來的目錄,所以,其實我們可以將所有的資料都給他寫入 /etc/logrotate.conf 即可,但是這樣一來這個檔案就實在是太複雜了,尤其是當我們使用很多的服務在系統上面時, 每個服務都要去修改 /etc/logrotate.conf 的設定也似乎不太合理~ 所以,如果獨立出來一個目錄,那麼每個以 RPM 打包方式所建立的服務的登錄檔輪替設定, 就可以獨自成為一個檔案,並且放置到 /etc/logrotate.d/ 當中即可,真是方便又合理的做法啊! ^_^

一般來說,這個 /etc/logrotate.conf 是『預設的輪替狀態』而已, 我們的各個服務都可以擁有自己的登錄檔輪替設定,你也可以自行修改成自己喜歡的樣式啊! 例如,如果你的系統的空間夠大,並且擔心除錯以及駭客的問題,那麼可以:

將 rotate 4 改成 rotate 9 左右,以保存較多的備份檔案;
大部分的登錄檔不需要 compress 囉!但是空間太小就需要 compress !尤其是很佔硬碟空間的 httpd 更需要 compress 的!
好了,上面我們大致介紹了 /var/log/wtmp 這個檔案的設定,現在你知道了 logrotate.conf 的設定語法是:

登錄檔的絕對路徑檔名 ... {
個別的參數設定值,如 monthly, compress 等等
}

底下我們再以 /etc/logrotate.d/syslog 這個輪替 syslog 服務的檔案,來看看該如何設定他的 rotate 呢?
[root@www ~]# vi /etc/logrotate.d/syslog
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler \
/var/log/boot.log /var/log/cron {
  sharedscripts
  postrotate
    /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true
  endscript
}


在上面的語法當中,我們知道正確的 logrotate 的寫法為:

檔名:被處理的登錄檔絕對路徑檔名寫在前面,可以使用空白字元分隔多個登錄檔;
參數:上述檔名進行輪替的參數使用 { } 包括起來;
執行腳本:可呼叫外部指令來進行額外的命令下達,這個設定需與 sharedscripts .... endscript 設定合用才行。至於可用的環境為:

prerotate:在啟動 logrotate 之前進行的指令,例如修改登錄檔的屬性等動作;
postrotate:在做完 logrotate 之後啟動的指令,例如重新啟動 (kill -HUP) 某個服務!
Prerotate 與 postrotate 對於已加上特殊屬性的檔案處理上面,是相當重要的執行程序!
那麼 /etc/logrotate.d/syslog 內設定的六個檔案的輪替功能就變成了:

該設定只對 /var/log/ 內的 messages, secure, maillog, spooler, boot.log, cron 有效;
登錄檔輪替每週一次、保留四個、且輪替下來的登錄檔不進行壓縮(未更改預設值);
輪替完畢後 (postrotate) 取得 syslog 的 PID 後,以 kill -HUP 重新啟動 syslogd
假設我們有針對 /var/log/messages 這個檔案增加 chattr +a 的屬性時, 依據 logrotate 的工作原理,我們知道,這個 /var/log/messages 將會被更名成為 /var/log/messages.1 才是。但是由於加上這個 +a 的參數啊,所以更名是不可能成功的! 那怎麼辦呢?呵呵!就利用 prerotate 與 postrotate 來進行登錄檔輪替前、後所需要作的動作啊! 果真如此時,那麼你可以這樣修改一下這個檔案喔!
[root@www ~]# vi /etc/logrotate.d/syslog
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler \
/var/log/boot.log /var/log/cron {
  sharedscripts
  prerotate
    /usr/bin/chattr -a /var/log/messages
  endscript
  sharedscripts
  postrotate
    /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true
    /usr/bin/chattr +a /var/log/messages
  endscript
}


看到否?就是先給他去掉 a 這個屬性,讓登錄檔 /var/log/messages 可以進行輪替的動作, 然後執行了輪替之後,再給他加入這個屬性!請特別留意的是,那個 /bin/kill -HUP ... 的意義,這一行的目的在於將系統的 syslogd 重新以其參數檔 (syslog.conf) 的資料讀入一次!也可以想成是 reload 的意思啦! 由於我們建立了一個新的空的紀錄檔,如果不執行此一行來重新啟動服務的話, 那麼記錄的時候將會發生錯誤呦!(請回到第十七章讀一下 kill 後面的 signal 的內容說明)

實際測試 logrotate 的動作

好了,設定完成之後,我們來測試看看這樣的設定是否可行呢?給他執行底下的指令:
[root@www ~]# logrotate [-vf] logfile

選項與參數:
-v :啟動顯示模式,會顯示 logrotate 運作的過程喔!
-f :不論是否符合設定檔的資料,強制每個登錄檔都進行 rotate 的動作!

範例一:執行一次 logrotate 看看整個流程為何?
[root@www ~]# logrotate -v /etc/logrotate.conf
reading config file /etc/logrotate.conf <==讀取主要設定檔
including /etc/logrotate.d              <==呼叫外部的設定
reading config file acpid               <==就是外部設定啊!
....(中間省略)....
Handling 21 logs                        <==共有 21 個登錄檔被記錄
....(中間省略)....
rotating pattern: /var/log/messages /var/log/secure /var/log/maillog \
/var/log/spooler /var/log/boot.log /var/log/cron  weekly (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/messages       <==開始處理 messages
  log does not need rotating            <==因為時間未到,不需要更動!
....(底下省略)....

範例二:強制進行 logrotate 的動作
[root@www ~]# logrotate -vf /etc/logrotate.conf
....(前面省略)....
rotating log /var/log/messages, log->rotateCount is 4
renaming /var/log/messages.4 to /var/log/messages.5 (rotatecount 4, logstart 1, i 4),
renaming /var/log/messages.3 to /var/log/messages.4 (rotatecount 4, logstart 1, i 3),
renaming /var/log/messages.2 to /var/log/messages.3 (rotatecount 4, logstart 1, i 2),
renaming /var/log/messages.1 to /var/log/messages.2 (rotatecount 4, logstart 1, i 1),
renaming /var/log/messages.0 to /var/log/messages.1 (rotatecount 4, logstart 1, i 0),
old log /var/log/messages.0 does not exist
....(底下省略)....
# 看到否?整個 rotate 的動作就是這樣一步一步進行的~

[root@www ~]# ll /var/log/messages*; lsattr /var/log/messages
-rw------- 1 root root    63 Apr  8 15:19 /var/log/messages
-rw------- 1 root root   670 Apr  8 14:22 /var/log/messages.1
-rw------- 1 root root 24984 Apr  1 19:26 /var/log/messages.2
-rw------- 1 root root  1911 Mar 28 11:32 /var/log/messages.3
-rw------- 1 root root 25193 Mar 22 04:02 /var/log/messages.4
-----a------- /var/log/messages <==主動加入 a 的隱藏屬性囉!

上面那個 -f 具有『強制執行』的意思,如果一切的設定都沒有問題的話,那麼理論上,你的 /var/log 這個目錄就會起變化囉!而且應該不會出現錯誤訊息才對!嘿嘿!這樣就 OK 了!很棒不是嗎?!

由於 logrotate 的工作已經加入 crontab 裡頭了!所以現在每天系統都會自動的給他查看 logrotate 囉!不用擔心的啦!只是要注意一下那個 /var/log/messages 裡頭是否常常有類似底下的字眼:

Apr 8 15:19:47 www syslogd 1.4.1: restart (remote reception).
這說明的是 syslogd 重新啟動的時間啦 (就是因為 /etc/logrotate.d/syslog 的設定之緣故!) 底下我們來進行一些例題的練習,讓你更詳細的瞭解 logrotate 的功用啊!

自訂登錄檔的輪替功能

假設前提是這樣的,前一小節當中,假設你已經建立了 /var/log/admin.log 這個檔案, 現在,你想要將該檔案加上 +a 這個隱藏標籤,而且設定底下的相關資訊:

登錄檔輪替一個月進行一次;
該登錄檔若大於 10MB 時,則主動進行輪替,不需要考慮一個月的期限;
保存五個備份檔案;
備份檔案需要壓縮
那你可以怎麼樣設定呢?呵呵~很簡單啊!看看底下的動作吧!

# 1. 先建立 +a 這個屬性啊!
[root@www ~]# chattr +a /var/log/admin.log
[root@www ~]# lsattr /var/log/admin.log
-----a------- /var/log/admin.log
[root@www ~]# mv /var/log/admin.log /var/log/admin.log.1
mv: cannot move `/var/log/admin.log' to `/var/log/admin.log.1':
Operation not permitted
# 這裡確定了加入 a 的隱藏屬性!所以 root 無法移動此登錄檔!

# 2. 開始建立 logrotate 的設定檔,增加一個檔案在 /etc/logrotate.d 內就對了!
[root@www ~]# vi /etc/logrotate.d/admin
# This configuration is from VBird 2009/04/08
/var/log/admin.log {
        monthly   <==每個月進行一次
        size=10M  <==檔案容量大於 10M 則開始處置
        rotate 5  <==保留五個!
        compress  <==進行壓縮工作!
        sharedscripts
        prerotate
                /usr/bin/chattr -a /var/log/admin.log
        endscript
        sharedscripts
        postrotate
                /usr/bin/killall -HUP syslogd
                /usr/bin/chattr +a /var/log/admin.log
        endscript
}

# 3. 測試一下 logrotate 相關功能的資訊顯示:
[root@www ~]# logrotate -v /etc/logrotate.conf
....(前面省略)....
rotating pattern: /var/log/admin.log  10485760 bytes (5 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/admin.log
  log does not need rotating
not running prerotate script, since no logs will be rotated
not running postrotate script, since no logs were rotated
....(底下省略)....
# 因為還不足一個月,檔案也沒有大於 10M,所以不需進行輪替!

# 4. 測試一下強制 logrotate 與相關功能的資訊顯示:
[root@www ~]# logrotate -vf /etc/logrotate.d/admin
reading config file /etc/logrotate.d/admin
reading config info for /var/log/admin.log

Handling 1 logs

rotating pattern: /var/log/admin.log  forced from command line (5 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/admin.log
  log needs rotating
rotating log /var/log/admin.log, log->rotateCount is 5
renaming /var/log/admin.log.5.gz to /var/log/admin.log.6.gz (rotatecount 5, logstart 1, i 5),
old log /var/log/admin.log.5.gz does not exist
renaming /var/log/admin.log.4.gz to /var/log/admin.log.5.gz (rotatecount 5, logstart 1, i 4),
old log /var/log/admin.log.4.gz does not exist
renaming /var/log/admin.log.3.gz to /var/log/admin.log.4.gz (rotatecount 5, logstart 1, i 3),
old log /var/log/admin.log.3.gz does not exist
renaming /var/log/admin.log.2.gz to /var/log/admin.log.3.gz (rotatecount 5, logstart 1, i 2),
old log /var/log/admin.log.2.gz does not exist
renaming /var/log/admin.log.1.gz to /var/log/admin.log.2.gz (rotatecount 5, logstart 1, i 1),
old log /var/log/admin.log.1.gz does not exist
renaming /var/log/admin.log.0.gz to /var/log/admin.log.1.gz (rotatecount 5, logstart 1, i 0),
old log /var/log/admin.log.0.gz does not exist
log /var/log/admin.log.6.gz doesn't exist -- won't try to dispose of it
running prerotate script
renaming /var/log/admin.log to /var/log/admin.log.1
running postrotate script
compressing log with: /bin/gzip


[root@www ~]# lsattr /var/log/admin.log*
-----a------- /var/log/admin.log
------------- /var/log/admin.log.1.gz  <==有壓縮過喔!

看到了嗎?透過這個方式,我們可以建立起屬於自己的 logrotate 設定檔案, 很簡便吧!尤其是要注意的, /etc/syslog.conf 與 /etc/logrotate.d/* 檔案常常要搭配起來,例如剛剛我們提到的兩個案例中所建立的 /var/log/admin.log 就是一個很好的例子~建立後,還要使用 logrotate 來輪替啊! ^_^


原文出處:鳥哥的 Linux 私房菜 -- 登錄檔的分析啦!
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Linux日誌文件總管——logrotate

Linux日誌文件總管——logrotate

作者: Sarmed Rahman

日誌文件包含了關於系統中發生的事件的有用信息,在排障過程中或者系統性能分析時經常被用到。對於忙碌的服務器,日誌文件大小會增長極快,服務器會很快消耗磁盤空間,這成了個問題。除此之外,處理一個單個的龐大日誌文件也常常是件十分棘手的事。

logrotate是個十分有用的工具,它可以自動對日誌進行截斷(或輪循)、壓縮以及刪除舊的日誌文件。例如,你可以設置logrotate,讓/var/log/foo日誌文件每30天輪循,並刪除超過6個月的日誌。配置完後,logrotate的運作完全自動化,不必進行任何進一步的人為干預。另外,舊日誌也可以通過電子郵件發送,不過該選項超出了本教程的討論範圍。

主流Linux發行版上都默認安裝有logrotate包,如果出於某種原因,logrotate沒有出現在裡頭,你可以使用apt-get或yum命令來安裝。

在Debian或Ubuntu上:


# apt-get install logrotate cron

在Fedora,CentOS或RHEL上:


# yum install logrotate crontabs

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

樣例一

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

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


# 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
}

這裡:

  • monthly: 日誌文件將按月輪循。其它可用值為‘daily’,‘weekly’或者‘yearly’。
  • rotate 5: 一次將存儲5個歸檔日誌。對於第六個歸檔,時間最久的歸檔將被刪除。
  • compress: 在輪循任務完成後,已輪循的歸檔將使用gzip進行壓縮。
  • delaycompress: 總是與compress選項一起用,delaycompress選項指示logrotate不要將最近的歸檔壓縮,壓縮將在下一次輪循週期進行。這在你或任何軟件仍然需要讀取最新歸檔時很有用。
  • missingok: 在日誌輪循期間,任何錯誤將被忽略,例如“文件無法找到”之類的錯誤。
  • notifempty: 如果日誌文件為空,輪循不會進行。
  • create 644 root root: 以指定的權限創建全新的日誌文件,同時logrotate也會重命名原始日誌文件。
  • postrotate/endscript: 在所有其它指令完成後,postrotate和endscript裡面指定的命令將被執行。在這種情況下,rsyslogd 進程將立即再次讀取其配置並繼續運行。

上面的模板是通用的,而配置參數則根據你的需求進行調整,不是所有的參數都是必要的。

樣例二

在本例中,我們只想要輪循一個日誌文件,然而日誌文件大小可以增長到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任務應該在安裝時就自動創建了,我把cron文件的內容貼出來,以供大家參考。


# cat /etc/cron.daily/logrotate



#!/bin/sh
# Clean non existent log file entries from status file
cd /var/lib/logrotate
test -e status || touch status
head -1 status > status.clean
sed 's/"//g' status | while read logfile date
do
[ -e "$logfile" ] && echo "\"$logfile\" $date"
done >> status.clean
mv status.clean status
test -x /usr/sbin/logrotate || exit 0
/usr/sbin/logrotate /etc/logrotate.conf

小結一下,logrotate工具對於防止因龐大的日誌文件而耗盡存儲空間是十分有用的。配置完畢後,進程是全自動的,可以長時間在不需要人為干預下運行。本教程重點關注幾個使用logrotate的幾個基本樣例,你也可以定制它以滿足你的需求。

希望本文對你有所幫助。


via: http://xmodulo.com/2014/09/logrotate-manage-log-files-linux.html

作者: Sarmed Rahman 譯者: GOLinux 校對: wxy

本文由 LCTT 原創翻譯, Linux中國 榮譽推出


原文出處:系统运维|Linux日志文件总管——logrotate
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]使用 logrotate 來整理 wildfly 日誌檔

[CENTOS7] 使用 logrotate 來整理wildfly日誌檔

Unix 系統中的日誌檔存放在 /var/log/  中,可供我們查錯及觀察。 日誌檔最大的問題是會不斷的長大。

但是其他的日誌檔如果不定時去清空,也許就會長大到非常的可怕的地步,像我曾看過的網頁記錄,才三個月就達3G。

手動清空

如果要將記錄內容清空,移除再建立是一個方法,但不是很好的方法,可以採用下面這個指令

例如:

# cat /dev/null > /var/log/httpd-error.log

或是

# echo '' > /var/log/httpd-error.log

就可以 輕鬆的將記錄檔清空。

 

因為架設的 wildfly伺服器的log不斷長大,已經塞爆我的空間,於是採用 logrotate來處理我的記錄檔。

使用 logrotate來定時壓縮及分檔備份

 

一、安裝

系統 centos7

# yum install logrotate

二、設定

logrotate的設定檔為 /etc/logrotate.conf,這裡設定的是全域參數,可以 保留不動

# 執行週期
weekly

# 保存幾份資料,4代表會有4+1個
rotate 4

# 壓縮完是否會建一個新的空檔?
create

# 備份檔附檔名加註日期
dateext

# 是否用gz壓縮,預設無
#compress

# 下面是預設的不必動


include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

各別的設定檔放在 /etc/logrotate.d/

因為wildfly的log檔在 /var/log/wildfly/console.log

建立一個新的設定檔

# cd /etc/logrotate.d/
# cp yum wildfly
# vi wildfly

修改內容如下

/var/log/wildfly/console.log {
    missingok
    notifempty

    copytruncate
    rotate 10
    compress
    size 300M
    daily
    create 0600 root root
}

missingok 如果找不到 log 檔也沒關係
notifempty 如果檔案為0就略過
copytruncate 先複製 log 檔的內容再清空,因為wildfly會寫到原檔,這個參數一定要設定。
rotate 10  建立10個備份,注意這不算入原本那個
compress  壓縮成gz
size 300M 至少要300m才建立備份

 

三、測試

# logrotate -f /etc/logrotate.conf

<正確的話沒任何的回應>

# logrotate -f /etc/logrotate.d/wildfly

 

四、加入排程

不必加入排程,因為yum 裝完後就已經有在 /etc/cron.daily/ 中存在

除非你要改成自己的排程

10 0 * * 0 /usr/sbin/logrotate /etc/logrotate.conf

 

錯誤排除

error: error creating output file /var/log/wildfly/console.log.X: File exists

建立時已經有檔案,把該檔移除即可

 

參考連結

[1] [FREEBSD] 使用 logrotate 來管理及備份日誌檔@新精讚

[2] https://ihower.tw/blog/archives/3565

[3] https://www.jianshu.com/p/ea7c2363639c 這裡有參數的說明


原文出處:[CENTOS7] 使用 logrotate 來整理wildfly日誌檔 @新精讚
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|