Spring/ETC

LogRotate Process

YoonJong 2023. 5. 28. 17:43
728x90

서버를 운영하다보면 로그를 보고 서버의 상태, 장애를 대비합니다.

대부분의 로그 기록을 남기도록 설정하는데, 시간이 지남에 따라 엄청난 크기로 커지게 됩니다.

이러한 것을 대비하기 위해 로그를 잘 관리하도록 설정하는 기능을 logrotate 라고 합니다.

 

기본 프로세스와 개념만 확인하고, 이후 스터디와 실무 작성은 기존에 작성되어있는 회사의 LogRotate 스크립트를 보고 참고해 작성할 예정입니다.

 

Logratate 실행순서

  1. crontab
  2. Cron.daily
  3. Logrotate
  4. Logrotate.conf
  5. Logrotate.d

Logrotate 파일구조

  1. 데몬 프로그램 : /usr/sbin/logrotate
  2. Logrotate 데몬 설정파일 : /etc/logrotate.conf
  3. Logrotate 프로세스 설정파일 : /etc/logrotate.d
  4. Logrotate 작업내역 로그 : /etc/cron.dailylogrotate
1. Logrotate 설치 되어있는지 확인 - OS 설치시 기본적으로 설치되어 있다.
rpm -qa | grep logrotate 

2. 설치 
yum -y install logrotate  -CentOs7
apt install logrotate - Ubuntu

3. 옵션
rotate [숫자] : log파일이 5개 이상 되면 삭제
ex) rotate 5

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

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

create [권한][유저] [그룹] : rotate 되는 로그파일 권한 지정
ex) create 644 root root

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

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

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

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

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

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

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

Logrotate.conf 설정 → Logrotate 실행의 모든 설정을 담당

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

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

# 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

Logrotate.d → Logroate 를 실행하는 개별 프로세스들에 대한 설정을 지정

1. 경로로 들어가기
cd /etc/logrotate.d

2. 위의 경로에서 log 생성을 원하는 config 파일을 작성

/var/log/maillog /var/log/freshclam.log {
	// 일 단위로 실행
	daily
    	// 회전 주기 파일 개수
        rotate 7
        // log 파일 내용 없을 시 rotate 하지 않음
        notifempty
        // log 파일 없을 경우 error 메시지 출력 후 다음 실행
        missingok
        // 로그 파일 압축
        compress
        // 여러개 log 파일을 script로 공유하여 실행
        sharedscripts
        // logrotate 실행 후 스크립트 실행(스크립트 파일 경로가 와도 됨)
        postrotate
                /bin/kill -HUP `cat /var/run/syslogd.pid 2>/dev/null` 2> /dev/null || true
        endscript
}

Logrotate 실행

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

* 주기적으로 Logrotate 를 실행하려면 crontab 을 활용한다.
#매주 일요일 자정 Logrotate 를 실행
00 00 * * 7 /usr/sbin/logrotate -f /etc/logrotate.conf

# logrotate 전체 실행
/usr/sbin/logrotate -d /etc/logrotate.conf

# 특정 logrotate process 실행
/usr/sbin/logrotate -d /etc/logrotate.d/apache

# logrotate 디버그 모드
/usr/sbin/logrotate -d /etc/logrotate.conf

# 실행 과정 화면 출력
/usr/sbin/logrotate -v /etc/logrotate.conf

 

 

728x90