- 論壇徽章:
- 0
|
登錄檔的輪替(logrotate)
‘syslog 利用的是 demand 的方式來啟動的, 當有需求的時候立刻就會被執(zhí)行的,但是 logrotate 卻是在規(guī)定的時間到了之后才來進行登錄檔的輪替, 所以這個 logrotate 程序當然就是掛在 cron 底下進行的呦!’ 仔細看一下 /etc/cron.daily/ 里面的檔案 ,看到了吧! /etc/cron.daily/logrotate 就是記錄了每天要進行的登錄檔輪替的行為啦!
logrotate 的設定檔
/etc/logrotate.conf
/etc/logrotate.d/
在 /etc/logrotate.d/ 里面的檔案中,如果沒有規(guī)定到的一些細部設定,
則以 /etc/logrotate.conf 這個檔案的規(guī)定來指定為預設值!
登錄檔進行 logrotate 的結果
![]()
預設的 logrotate 的內(nèi)容
[root@www ~]# vim /etc/logrotate.conf
# 底下的設定是 "logrotate 的預設設定值" ,如果個別的檔案設定了其他的參數(shù),
# 則將以個別的檔案設定為主,若該檔案沒有設定到的參數(shù)則以這個檔案的內(nèi)容為預設值!
weekly 登錄檔的絕對路徑檔名 ... {
個別的參數(shù)設定值,如 monthly, compress 等等
}
實際測試 logrotate 的動作
[root@www ~]# logrotate [-vf] logfile
選項與參數(shù):
-v :啟動顯示模式,會顯示 logrotate 運作的過程喔!
-f :不論是否符合設定檔的資料,強制每個登錄檔都進行 rotate 的動作!
范例一:執(zhí)行一次 logrotate 看看整個流程為何?
[root@www ~]# logrotate -v /etc/logrotate.conf
reading config file /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 假設你已經(jīng)建立了 /var/log/admin.log 這個檔案, 現(xiàn)在,你想要將該檔案加上 +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 內(nèi)就對了!
[root@www ~]# vi /etc/logrotate.d/admin
# This configuration is from VBird 2009/04/08
/var/log/admin.log {
monthly ....(前面省略)....
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
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
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u3/108545/showart_2131889.html |
|