Linux系統使用logrotate進行記錄備份

來源:互聯網
上載者:User

標籤:linux logrotate配置

Linux系統上記錄檔很多很多,系統日誌message mail cron 等等以及應用程式的日誌和訪問日誌,在大量的訪問下,這些記錄檔會飛速增長,佔用大量私盤空間,同時消耗IO,目前線上使用shell指令碼對nginx和tomcat的access_log以及catalina.out檔案通過crontab進行日誌分割。



今天偶然想起logrotate,小試了下,還是不錯的,記錄下。。。

查看下log目錄下的日誌,可以看到關於crontab的日誌被分割了,我們沒有定義這個,是什麼實現的呢?就是logrotate咯。

ll /var/log/-rw-------  1 root  root        0 2月  22 03:21 cron-20150301-rw-------  1 root  root        0 3月   1 03:20 cron-20150308-rw-------  1 root  root        0 3月   8 03:19 cron-20150315-rw-------  1 root  root        0 3月  15 03:35 cron-20150322-rw-------  1 root  root     5740 3月  28 22:10 cron-20150328

看下logrotate設定檔

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/5B/F1/wKiom1UWt-aDYiegAAIGmQPAWG8645.jpg" title="設定檔.jpg" alt="wKiom1UWt-aDYiegAAIGmQPAWG8645.jpg" />

設定檔非常簡單,也通俗易懂,一會在說下每個參數的意思,可以看到include /etc/logrotate.d,看看這個目錄下有個咩?

[email protected] /var/log# ll /etc/logrotate.d/總用量 40-rw-r--r--. 1 root root  103 10月  3 2012 dracut-rw-r--r--  1 root root  185 8月  15 2014 httpd-rw-r--r--. 1 root root  173 6月  22 2012 iscsiuiolog-rw-r--r--  1 root root  112 3月  28 22:05 market-rw-r-----  1 root named 170 10月 15 21:28 named-rw-r--r--  1 root root  136 8月  23 2010 ppp-rwxr-xr-x  1 root root  452 11月  8 03:43 salt-rw-r--r--  1 root root  210 12月 10 18:05 syslog-rw-r--r--  1 root root  100 2月   4 2013 wpa_supplicant-rw-r--r--  1 root root   87 10月 16 23:15 yum

關於crontab的log是寫在syslog裡得,所以我們繼續看下syslog的設定檔吧

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/5B/EB/wKioL1UWueGj0S1cAADwNxEXTJ8773.jpg" title="2.jpg" alt="wKioL1UWueGj0S1cAADwNxEXTJ8773.jpg" />我們都知道syslog管理者cron mail message secure這些日誌

貌似剛才logrotate的主設定檔沒截完整 哈哈,那說一個實力吧 一個tomcat的日誌分割,一個nginx的日誌分割

vim /etc/etc/logrotate.d/tomcat/data/market_tomcat/logs/catalina.out {   #定義日誌的位置,當然也可以寫多個就像syslog那樣子    rotate 30                             #保留歸檔30天~不解釋,你懂得 就是分割之後保留一個月    daily                                 #每天分割    copytruncate                          #表示此方法採用的是先拷貝再清空的方式    missingok                             #表示如果找不到log檔案也沒OK    notifempty                            #表示如果log檔案是空的,就不進行rotate}

儲存退出,我們執行下/usr/sbin/logrotate -f /etc/logrotate.conf在看下tomcat目錄下的記錄檔

[email protected] /data/market_tomcat/logs# ll catalina.out-20150328-rw-r--r-- 1 root root 233079086 3月  28 22:05 catalina.out-20150328

nginx的例子:

   /data/logs/qa.qding.qdingnet_accesslog {           daily           missingok           rotate 5           notifempty           sharedscripts           postrotate                   [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`          endscript  }

無非就是加了一個shell命令,重啟nginx滴。你懂得


參數說明

參數 功能  

  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 當記錄檔到達指定的大小時才轉儲,Size 可以指定 bytes (預設)以及KB (sizek)或者MB (sizem).  



哦對了。我們可以看下/etc/cron.daily/logrotate指令碼, 這個每天會主動幫忙進行日誌的備份

本文出自 “秋小秋” 部落格,請務必保留此出處http://heygirl32895.blog.51cto.com/7623150/1626043

Linux系統使用logrotate進行記錄備份

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.