linux中logrotate對日誌進行切割壓縮(nginx,mysql)

來源:互聯網
上載者:User

logrotate簡介
日誌輪轉特別適用於具有固定檔案名稱的記錄檔,比如MySQL的出錯日誌、常規查詢日誌、慢查詢日誌等。Linux系統有一個非常好用的根據logratate可以實現自動輪轉,本文介紹它的原理和用法。

logrotate是管理記錄檔的工具,在CentOS系統中,命令的位置在/usr/sbin/logrotate,常用的操作如:
  -d, --debug               Don't do anything, just test (implies -v)
  -f, --force               Force file rotation
注意:帶有-d參數,並不會產生新日誌.

logrotate一般每天由cron運行一次.標準的設定檔是/etc/logrotate.conf,而/etc/logrotate.d目錄也是儲存設定檔的位置.

logrotate常見選項:
/-----------------------------------------------
選項        |        含義
-----------------------------------------------
compress    |    壓縮記錄檔的所有非目前的版本
copy    |    複製當前的記錄檔,忽略create參數
copytruncate    |    複製當前的記錄檔,共置空當前檔案
daily    |    每天輪記錄檔i
dateext    |    輪換的日誌尾碼為-YYYYMMDD格式
delaycompress    |    壓縮除了當前和最近之外的所有其他版本
missingok    |    如果日誌不存在,不會報錯
notifempty    |    如果日誌為空白,則不輪換
rotate n    |    在輪換方案中包含n個版本的日誌
size=logsize    |    如果記錄檔大於logsize才輪換
-----------------------------------------------/

  預設情況下,logrotate部署為每天啟動並執行cronjob,你可以在目錄/etc/cron.daily裡找到名為logrotate的設定檔。那麼它是在每天的上面時候啟動並執行呢?開啟檔案/etc/crontab就知道了,下面是我機器上的情況:

 代碼如下 複製代碼

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
     
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

從上面的配置我們可以知道,/etc/cron.daily是在每天淩晨4:02執行。也就是說,每天4:02分/etc/cron.daily/logrotate將會自動執行,下面是它的內容:

 代碼如下 複製代碼

#!/bin/sh
     
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

  從上面我們可以知道,logratate預設的設定檔是/etc/logrotate.conf,下面是它的內容:

 代碼如下 複製代碼

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
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
}

# system-specific logs may be also be configured here.

從/etc/logrotate.conf配置可以知道,這個預設的設定檔將讀取目錄/etc/logrotate.d,所以我們只要把自己寫的設定檔放到/etc/logrotate.d目錄下即可。

mysql 配置篇

MySQL本省提供了一個rotate的參考設定檔,在mysql安裝目錄下的support-files目錄裡,檔案名稱為mysql-log-rotate,內容如下:

 代碼如下 複製代碼

# This logname can be set in /etc/my.cnf
# by setting the variable "err-log"
# in the [safe_mysqld] section as follows:
#
# [safe_mysqld]
# err-log=/usr/local/mysql/data/mysqld.log
#
# If the root user has a password you have to create a
# /root/.my.cnf configuration file with the following
# content:
#
# [mysqladmin]
# password = <secret>
# user= root
#
# where "<secret>" is the password.
#
# ATTENTION: This /root/.my.cnf should be readable ONLY
# for root !

/usr/local/mysql/data/mysqld.log {
        # create 600 mysql mysql
        notifempty
        daily
        rotate 3
        missingok
        compress
    postrotate
        # just if mysqld is really running
        if test -x /usr/local/mysql/bin/mysqladmin &&
           /usr/local/mysql/bin/mysqladmin ping &>/dev/null
        then
           /usr/local/mysql/bin/mysqladmin flush-logs
        fi
    endscript
}

從上面的注釋資訊可以看到步驟:
1.建立MySQL root密碼檔案
  vi /root/.my.cnf
  編輯如下內容
 
 

 代碼如下 複製代碼
[mysqladmin]
  password = <secret>
  user= root

  註: <secret>是你的root 密碼
2.給root 讀的許可權
  chmod 600 /root/.my.cnf
3.把mysql-log-rotate拷貝至/etc/logrotate.d目錄下,修改其內容為:

 代碼如下 複製代碼
/usr/local/mysql/data/*.log {
        create 600 mysql mysql
        notifempty
        daily
        rotate 7
        missingok
        compress
        postrotate
        # http://www.111cn.net        # just if mysqld is really running
        if test -x /usr/local/mysql/bin/mysqladmin &&
           /usr/local/mysql/bin/mysqladmin ping &>/dev/null
        then
           /usr/local/mysql/bin/mysqladmin flush-logs
        fi
    endscript
}

4.執行以下命令測試

 代碼如下 複製代碼

/usr/sbin/logrotate -f /etc/logrotate.d/mysql-log-rotate

nginx 配置篇
將下面代碼 寫入 /etc/logrotate.d/nginx

 代碼如下 複製代碼
/usr/local/nginx/log/*.log {
        create 600 www www
        notifempty
        daily
        rotate 7
        missingok
        compress
    postrotate
        #servic nginx reload
    endscript
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.