天天看點

Linux 下使用 Logrotate 旋轉和壓縮日志檔案

作者:鸠摩智首席音效師
Linux 下使用 Logrotate 旋轉和壓縮日志檔案

Linux-Rotate-And-Compress-Logs

在 Linux 系統中,當涉及到檢查和故障排除錯誤時,幾乎所有系統日志檔案都非常重要。它們提供了關于各種系統服務在失敗之前可能出現的問題的重要線索。

Linux 系統上安裝的任何服務,例如 Apache 伺服器或 MySQL 伺服器生成通常存儲在 /var/log 目錄。如果您檢查此目錄的内容,您将看到類似于下面的内容:

Linux 下使用 Logrotate 旋轉和壓縮日志檔案

随着時間的推移,日志檔案的大小會增加,并占用更多的空間。在您意識到這一點之前,日志檔案的大小已經膨脹,稍有疏忽,很容易耗盡磁盤空間。

考慮到這一點,謹慎的做法是将日志檔案保持在可管理的大小,并删除占用磁盤空間的舊日志,這就是 log rotation 的由來。

什麼是 log rotation ?

Log rotation 是一個建立新日志檔案和存檔&删除舊檔案以節省磁盤空間的過程。例如:apport.log 變為 apport.log.1,并建立一個新的 apport.log 日志檔案記錄新的日志項。舊的日志檔案通常被壓縮,并顯示為 apport.log.2.gz,apport.log.3.gz,apport.log.4.gz,等等。

當日志檔案的大小增長并超過某個限制時,通常會激活日志輪換。

輪換程式如何工作 ?

在我們講解輪換程式如何工作之前,請確定在您的系統上安裝了 logrotate。

For Debian / Ubuntu System:

$ sudo apt-get install logrotate -y           

For CentOS / RHEL / Fedora System:

$ sudo yum install logrotate -y
or 
$ sudo dnf  install logrotate -y
           

運作以下指令檢查 logrotate 版本

linuxtechi@ubuntu-server:~$ logrotate --version
logrotate 3.14.0
    Default mail command:       /usr/bin/mail
    Default compress command:   /bin/gzip
    Default uncompress command: /bin/gunzip
    Default compress extension: .gz
    Default state file path:    /var/lib/logrotate/status
    ACL support:                yes
    SELinux support:            yes
linuxtechi@ubuntu-server:~$           

預設情況下,logrotate 在現代 Linux 發行版中是預安裝的,是以不需要安裝它。

logrotate 配置檔案

logrotate 作為 cron 作業每天運作,周遊各種日志檔案,輪換它們,并清除配置檔案中定義的舊日志檔案,您需要密切關注兩個主要的配置源。

(1)/etc/logrotate.conf

這是 logrotate 工具的主要配置檔案,它使用 include 指令來包含位于 /etc/logrotate.d 目錄中的配置,讓我們看一下配置檔案。

$ cat /etc/logrotate.conf           
Linux 下使用 Logrotate 旋轉和壓縮日志檔案

(2)/etc/logrotate.d

這是一個目錄,包含已安裝包的日志配置,這些包的日志檔案需要日志輪換。您還可能找到系統工具的配置檔案,例如 apt & dpkg (用于 Debian 系統)、rsyslog、ufw 和 cups-daemon。

linuxtechi@ubuntu-server:~$ ls -l /etc/logrotate.d/
total 60
-rw-r--r-- 1 root root 120 Sep  5  2019 alternatives
-rw-r--r-- 1 root root 126 Dec  4 20:25 apport
-rw-r--r-- 1 root root 173 Apr  9 11:21 apt
-rw-r--r-- 1 root root  91 Apr  1 10:49 bootlog
-rw-r--r-- 1 root root 130 Jan 21  2019 btmp
-rw-r--r-- 1 root root 181 Feb 17 08:19 cups-daemon
-rw-r--r-- 1 root root 112 Sep  5  2019 dpkg
-rw-r--r-- 1 root root 329 Feb  4  2019 nginx
-rw-r--r-- 1 root root  94 Feb  8  2019 ppp
-rw-r--r-- 1 root root 501 Mar  7  2019 rsyslog
-rw-r--r-- 1 root root 677 Nov 29 02:08 speech-dispatcher
-rw-r--r-- 1 root root 119 Mar 30 21:49 ubuntu-advantage-tools
-rw-r--r-- 1 root root 178 Jan 21 22:16 ufw
-rw-r--r-- 1 root root 235 Apr 13 23:37 unattended-upgrades
-rw-r--r-- 1 root root 145 Feb 19  2018 wtmp
linuxtechi@ubuntu-server:~$           

讓我們看一下 dpkg 包管理器工具的配置檔案

$ cat -n /etc/logrotate.d/dpkg           
Linux 下使用 Logrotate 旋轉和壓縮日志檔案

自定義 logrotate 配置檔案示例

假設我們有一個以 linuxtechi 使用者運作的應用程式,生成的日志檔案存儲在 /home/linuxtechi/logs 目錄中,日志檔案需要每周輪換旋轉。

首先,我們在主目錄中建立一個 logrotate 配置檔案

$ vim /home/linuxtechi/logrotate.conf           

寫入如下配置内容

/home/linuxtechi/logs/*.log {
    weekly
    missingok
    rotate 14
    compress
    create
}           

日志檔案将每周輪換一次,如果有任何日志檔案丢失,則會抑制任何錯誤消息。在這個月中,将備份 14 個日志檔案,并在輪換目前日志檔案後建立一個新的日志檔案。

Linux 下使用 Logrotate 旋轉和壓縮日志檔案

接下來,我們建立一個 logs 目錄,其中将包含應用程式的日志檔案,然後建立一個名為 app.log 的日志檔案。

linuxtechi@ubuntu-server:~$ mkdir logs && cd logs
linuxtechi@ubuntu-server:~/logs$ touch app.log
linuxtechi@ubuntu-server:~/logs$ ls
app.log
linuxtechi@ubuntu-server:~/logs$           

接下來,我們運作 logrotate 指令在主目錄中建立一個 logrotate 狀态檔案,以驗證日志條目是否已經建立。

$ logrotate /home/linuxtechi/logrotate.conf --state /home/linuxtechi/logrotate-state --verbose           
Linux 下使用 Logrotate 旋轉和壓縮日志檔案

從輸出來看,日志檔案沒有被輪換,因為輪換每周發生一次,而日志檔案隻有一個小時。

檢查日志記錄檔案,以驗證是否記錄了關于日志旋轉運作的任何資訊。

linuxtechi@ubuntu-server:~$ cat logrotate-state
logrotate state -- version 2
"/home/linuxtechi/logs/app.log" 2020-5-24-17:0:0
linuxtechi@ubuntu-server:~$           

從輸出中,我們可以看到 logrotate 程式确認最後一次考慮日志檔案進行旋轉,并列印時間戳。

我們強制 logrotate 旋轉日志檔案,否則它現在不會旋轉日志檔案。

$ logrotate /home/linuxtechi/logrotate.conf --state /home/linuxtechi/logrotate-state --verbose --force           
Linux 下使用 Logrotate 旋轉和壓縮日志檔案

傳回 logs 目錄,您将看到一個額外的日志檔案,該檔案已被旋轉和壓縮。

linuxtechi@ubuntu-server:~$ cd logs/
linuxtechi@ubuntu-server:~/logs$ ls
app.log  app.log.1.gz
linuxtechi@ubuntu-server:~/logs$           

根據大小對日志檔案進行壓縮和旋轉

有時,日志檔案會在指定的輪換間隔 (每天 / 每周 / 每月) 之前變得更大并占用空間。

解決該問題的一種方法是指定檔案的最大尺寸,超過該大小時将觸發日志檔案的旋轉。要實作這一點,請在 logrotate 檔案中指定 maxsize 選項。

假設我們在 /etc/logrotate.d 目錄下為應用程式建立一個自定義日志旋轉配置檔案

linuxtechi@ubuntu-server:~$ cd /etc/logrotate.d/
linuxtechi@ubuntu-server:/etc/logrotate.d$ sudo vi custom-app
/home/linuxtechi/logs/app-access.log
{
    daily
    missingok
    maxsize 40M
    rotate 4
    compress
    create
}           

可以使用 -d 選項對日志檔案進行 logrotate 的試運作

$ logrotate -d /etc/logrotate.d/custom-app           
Linux 下使用 Logrotate 旋轉和壓縮日志檔案

除了增強基于大小的日志檔案的旋轉之外,謹慎的做法是確定使用 cron 作業定期調用 logtate 配置檔案。這對于日志檔案來說尤其重要,因為它們的大小會迅速膨脹,有可能會填滿磁盤空間。

您可以将 logrotate 腳本從 /etc/cron.daily 目錄複制到 /etc/cron.hourly 位置。這将把日志旋轉轉換為一個小時,而不是每日的日志旋轉。

另一種方法是在 /etc /crontab 檔案中指定 cron 作業,這樣每隔 10 分鐘就會觸發旋轉。

*/10 * * * * /etc/cron.daily/logrotate           

除了使用 maxsize 指令指定最大大小外,還使用 crontab 調用 logrotate 腳本,這是一個完美的組合,可以確定及時旋轉日志檔案,以避免填滿硬碟驅動器。

有關 logrotate 工具的其他選項,請檢視手冊頁

Linux 下使用 Logrotate 旋轉和壓縮日志檔案

我的開源項目

Linux 下使用 Logrotate 旋轉和壓縮日志檔案
  • course-tencent-cloud(酷瓜雲課堂 - gitee 倉庫)
  • course-tencent-cloud(酷瓜雲課堂 - github 倉庫)

繼續閱讀