[Linux] logrotate

HYEOB KIM·2022년 5월 31일
1

Linux

목록 보기
2/11

본 포스팅은 아래 링크의 포스팅을 참고하여 작성되었습니다.
https://velog.io/@gillog/logrotate

logrotate

Linux에서 로그를 저장하며 관리 할 때 특정 로그 파일이 한 파일로 계속해서 저장되는 것이 아닌 여러 파일로 분산 저장 시켜줄 때 사용합니다.

Logrotate 구조

Logrotate를 구성하는 파일들은 아래와 같은 구조를 가진다.

  • /usr/sbin/logrotate : Logrotate 데몬 프로그램
  • /etc/logrotate.conf : Logrotate 데몬 설정 파일
  • /etc/logrotate.d/ : Logrotate 프로세스 설정 파일
  • /etc/cron.daily/logrotate : Logrotate 작업내역 로그

Logrotate 설치

기본적으로 OS 설치 시 포함되어 설치 되어 있습니다.

아래 명령을 통해 확인할 수 있습니다.

% rpm -qa | grep logrotate
logrotate-3.8.6-15.amzn2.x86_64

만약 설치되어 있지 않다면 아래 명령어로 설치할 수 있습니다.

% yum -y install logrotate

logrotate 사용

옵션

logrotate.conf 또는 logrotate.d/ 에서 사용할 수 있는 옵션들에 대해 알아봅시다.

  • rotate [숫자] : log파일이 5개 이상 되면 삭제
    ex) rotate 5

  • maxage [숫자] : log파일이 30일 이상 되면 삭제
    ex) maxage 30

  • size : 지정된 용량보다 클 경우 rotate 실행
    ex) size +100k

  • create [권한] [유저] [그룹] : rotate 되는 로그파일 권한 지정(디폴트: 600 root root)
    ex) create 644 root root

  • notifempty : 로그 내용이 없으면 rotate 하지 않음

  • ifempty : 로그 내용이 없어도 rotate 진행

  • monthly(월) , weekly(주) , daily(일) rotate 진행

  • compress : 로테이트 되는 로그파일 gzip 압축

  • nocompress : 로테이트 되는 로그파일 gzip 압축 X

  • missingok : 로그 파일이 발견되지 않은 경우 에러처리 하지 않음

  • dateext : 백업 파일의 이름에 날짜가 들어가도록 함

logrotate.conf

/etc/logrotate.conf 에는 logrotate 관련 config를 설정할 수 있습니다.

/usr/sbin/logrotate -f /etc/logrotate.conf 로 실행하면
logrotate.conf 설정에 따라 logrotate가 실행됩니다.

# rotate log files weekly
# log 회전 주기 yearly : 매년, monthly : 매월, weekly : 매주, daily : 매일
weekly

# keep 4 weeks worth of backlogs
# log 파일 개수, 해당 개수가 넘어가면 logrotate의 주기에 따라 실행됨
rotate 4

# create new (empty) log files after rotating old ones
# 새로운 log 파일 생성 여부, create : log 파일 생성, empty : log 파일 생성 안함
create

# use date as a suffix of the rotated file
# 파일명 날짜 여부, logrotate 실행 후 log파일에 날짜를 부여
dateext

# uncomment this if you want your log files compressed
# log파일 압축 여부, 로그 파일 크기 조절 용도
# compress

# RPM packages drop log rotation information into this directory
# 개별 로그 process 설정 경로
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.

logrotate.d

/etc/logrotate.d 아래에는 개별 로그 경로(path)에 대한 logrotate config를 설정할 수 있습니다.

예를 들어, 아파치의 로그 경로는 /app/apache/logs 입니다.
해당 경로 아래에 아파치 관련 로그들이 쌓입니다.

여기에 쌓이는 로그들에 대해 logrotate config를 설정하고 싶다면 /etc/logrotate.d 아래에 다음과 같은 파일을 만들면 됩니다.

% vim /etc/logrotate.d/apache

/app/apache2/logs/*log {
    daily
    rotate 5
    notifempty
    missingok
    compress
    sharedscripts
    postrotate
        /server/apache2/bin/apachectl graceful
    endscript
}

그리고 아래와 같이 logrotate 명령을 실행하면,

% /usr/sbin/logrotate -f /etc/logrotate.conf

아파치 로그 경로에 끝이 log로 끝나는 파일들에 대해서 logrotate 설정이 적용됩니다.
(logrotate.conf 파일에 logrotate.d 경로를 참조하라는 내용이 있습니다)

/etc/logrotate.d/apache 상세설명

  • daily : 일단위로 실행합니다
  • rotate 5 : 회전 주기를 설정합니다
  • notifempty : 로그파일의 내용이 없을경우 rotate 하지 않습니다
  • missingok : 로그파일이 없을경우 에러메시지를 출력하고 다음으로 실행합니다
  • compress : 로그파일을 압축합니다
  • sharedscripts : 여러 개의 로그 파일을 스크립트로 공유하여 실행합니다
  • postrotate : logrotate 실행 후 해당 스크립트 파일을 실행합니다
    /server/apache2/bin/apachectl graceful
  • endscript : 실행 후 스크립트 파일 실행합니다

기존 로그 파일 탐색

/etc/logrotate.d/ 경로에 보면 아래와 같이 개별 로그 경로에 대한 설정 파일들이 존재합니다.

% cd /etc/logrotate.d
% ls
bootlog  chrony  httpd  psacct  syslog  yum

syslog

/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    missingok
    sharedscripts
    postrotate
	/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

/var/log 경로로 들어가서 파일 리스트를 보면 logrotate가 작동 중인 것을 확인할 수 있습니다.

logrotate.conf, logrotate.d에 설정된 대로 동작하며, 기존 파일에 있던 내용이 분리되어 저장됩니다.
(기존 파일에는 분리된 내용이 남아 있지 않게 됩니다)

logrotate 관련 에러 로그

리눅스를 다루다보면 logrotate 관련 아래와 같은 에러 로그가 발생하는 것을 볼 수 있습니다.

logrotate: ALERT exited abnormally with [1]

앞서 별다른 실행이 없었음에도 서버에 logrotate가 작동하고 있었던 이유는 cron.daily 에 등록되어 있기 때문입니다.

cron.daily에 등록된 logrotate 스크립트는 아래와 같습니다.

% cd /etc/cron.daily
% ls
logrotate  man-db.cron  mlocate
% cat logrotate 
#!/bin/sh

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

스크립트를 보면 logrotate를 실행하는 도중에 비정상적으로 종료되면 예외처리에 의해 logrotate: "ALERT exited abnormally with [종료 코드]" 로그가 남습니다.

  • /var/lib/logrotate.status : 파일별 최근 rotate 시각이 기록되어 있음.
profile
Devops Engineer

0개의 댓글