본문 바로가기
IT/Linux

로그관리 Logrotate

by 어느 개발자의 블로그 2022. 2. 7.
728x170

사용 목적

Logrotate는 Linux에서 계속 쌓이는 log를 주기적으로 관리해주는 툴로 특정 log 파일이 한 파일로 계속해서 크기가 커지면 확인이 필요한 경우 너무 방대해 확인이 어렵고, 디스크 용량이 낭비되어 크기가 커지며 저장되는 걸 분산시켜주기 위하여 사용

 

Logrotate 설치

Logrotate는 OS 설치시 기본적으로 설치되어 있고, 아래 명령어로 확인 가능.

rpm -qa | grep logrotate

 

Logrotae 실행 순서

 

Logrotate 구조

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

 

Logrotate 개별 설정

/etc/logrotate.d 에는 Logrotate를 실행하는 개별 프로세스들에 대한 설정을 지정할 수 있고, 해당 경로에서 log 생성을 원하는 config 파일을 작성해 주면됨

vi /etc/logrotate.d/httpd
/engn001/apaadm/apache24/servers/svc_01/logs/access_log/*.log
/engn001/apaadm/apache24/servers/svc_01/logs/error_log/*.log
/engn001/apaadm/apache24/servers/svc_01/logs/modjk_log/*.log
{
    daily
    rotate 20
    copytruncate
    delaycompress
    compress
    dateext
    notifempty
    missingok
}

 

Logrotate 사용 Option

  • rotate [숫자] : log파일이 [숫자]개 이상되면 삭제 ( ex - rotate 4 )
  • maxage [숫자] : log파일이 [숫자]일 이상되면 삭제 ( ex - maxage 30 )
  • size : 지정된 용량보다 클 경우 rotate 실행 ( ex - size +100k )
  • minsize 1M : 1M 가 되면 rotate 
  • create [권한] [유저] [그룹] : rotate 되는 log파일 권한 지정 ( ex - create 755 root root )
  • notifempty : 로그 내용이 없으면 rotate 하지 않음
  • ifempty : 로그 내용이 없이도 roate 진행
  • yearly, monthly, weekly, daily : 년, 월, 주, 일 rotate 진행
  • compress : rotate 되는 로그파일 gzip 압축
  • compressext 확장자명 : 압축된 백업로그파일에 지정할 확장자 설정
  • compresscmd 압축명 : gzip이외의 압축파일 지정 
  • cpmpressoptions 옵션 : 압축프로그램에 대한 옵션 설정(-9: 압축률 최대)
  • nocompress : rotate 되는 로파일 gzip 압축하지 않음
  • missingok : 로그 파일이 발견되지 않은 경우 에러처리 하지 않음
  • dateext : 백업 파일의 이름에 YYYYMMDD형식의 날짜가 들어가도록 함
  • copytruncate : 현재로그를 복사하고나면 원본은 truncate
  • errors 메일주소 : 에러발생시 지정된 메일주소로 메일 발송
  • extention 확장자명 : 순환된 로그파일의 확장자 지정
  • mail 메일주소 : 순환 후 이전 로그파일을 지정된 메일주소로 발송
  • prerotate / endscript : 순환작업 전에 실행할 작업 설정
  • postrotate / endscript : 순환작업 후에 실행할 작업 설정
  • sharedscripts : prerotate, postrotate 스크립트를 한번만 실행
  • su root root : 실행시 권한 에러 발생하는 경우 설정

 

Logrotate 실행

config 파일을 작성한 후 아래 명령어를 통해 Logrotate를 싱행

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

주기적으로 Logrotate를 실행하기 원하면, crontab을 활용

// 매일 자정 logrotate 실행
00 00 * * * /usr/sbin/logrotate -f /etc/logrotate.conf

아래는 Logrotate 명령어

// 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
옵션 의미
-v, --verbose Turn on verbose mode
-d, --debug Turns on debug mode and implies -v.  In debug mode, no changes will be made to the logs or to the logrotate state file
-f, --force Tells logrotate to force the rotation, even if it doesn’t think this is necessary. Sometimes this is useful  after adding new entries to logrotate, or if old log fileshave been removed by hand, as the new files
will be created, and logging will continue correctly.
-s, --state <statefile></statefile> Tells logrotate to use an alternate state file. This is useful if logrotate is being run as a different user
for various sets of log files. The default state file is/var/lib/logrotate.status
반응형
그리드형

'IT > Linux' 카테고리의 다른 글

Apache MPM(Multi-Processing Module)  (0) 2022.02.08

댓글