Logrotate

From 탱이의 잡동사니
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Overview

Linux logrotate 내용 정리

Basic

Logrotate 는 로그를 관리하는 유틸리티이다. 각각의 로그파일마다 다른 방식의 로테이팅 옵션을 적용할 수 있다. <source lang=bash> $ ls -l /etc/cron.daily/logrotate -rwxr-xr-x 1 root root 372 Jan 22 2014 /etc/cron.daily/logrotate </source>

Options

-?, --help

도움말 메시지를 표시한다.

-v, --verbose

Turns on verbose mode, ie. display messages during rotation.

-d, --debug

Debug 모드를 활성화한다. -v 옵션을 포함한다. In debug mode, no changes will be made to the logs or to the logrotate state file.

-f, --force

강제로 logrotate 를 작동하도록 한다. 아직 동작 시간이 아니어도 강제로 동작시킨다. 새롭게 로그로테이트 규칙을 추가하고 테스트를 하고자 할 경우, 유용하게 사용할 수 있다.

-m, --mail <command>

메일링 전송을 할 때 사용할 명령어를 지정한다. 입력되는 명령어는 반드시 최소한 다음 두개의 파라미터는 입력받을 수 있어야 한다.

- 메일 제목
- 메일 수신자

기본값은 /usr/bin/mail -s 이다

-s, --state <statefile>

logrotate 상태 파일을 지정한다. 기본값은 /var/lib/logrotate/status 이다.

Conf options

  • daily
로그를 일 단위로 백업한다.
  • weekly
로그를 주 단위로 백업한다. 마지막 백업을 실행한 실행할 날로 일주일이 넘었을 경우 백업을 한다.
  • monthly
로그를 월 단위로 백업한다. 보통은 달의 첫날에 백업을 한다.
  • rotate <count>
백업된 로그 파일을 몇개까지 보관할 것인지 결정한다. 설정된 갯수 이상의 로그 백업이 있을 경우, 오래된 것부터 삭제한다.
  • missingok
만약 해당 로그파일이 없더라도 별도의 에러 메시지 없이 그냥 넘어간다.
  • nomissingok
만약 해당 로그파일이 없다면 에러 메시지를 표시한다. (기본설정)
  • create <mode> <owner> <group>, create <onwer> <group>
백업파일을 생성한 후, 설정한 <mode>, <owner>, <group> 으로 변경한다. 만약 nocreate 옵션이 설정되어 있다면 적용되지 않는다.
  • compress
백업 파일 생성 후, gzip 으로 압축한다.
  • dateext
로그 로테이트 작동 후, 파일명에 날짜 정보를 표시한다.(sample_program.log-20170615.gz)
  • sharedscripts
...

Sample

mysql-server logrotate sample

$ cat /etc/logrotate.d/mysql-server

# - I put everything in one block and added sharedscripts, so that mysql gets 
#   flush-logs'd only once.
#   Else the binary logs would automatically increase by n times every day.
# - The error log is obsolete, messages go to syslog now.
/var/log/mysql.log /var/log/mysql/*log {
        daily
        rotate 7
        missingok
        create 640 mysql adm
        compress
        sharedscripts
        postrotate
                test -x /usr/bin/mysqladmin || exit 0
                # If this fails, check debian.conf! 
                MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
                if [ -z "`$MYADMIN ping 2>/dev/null`" ]; then
                  # Really no mysqld or rather a missing debian-sys-maint user?
                  # If this occurs and is not a error please report a bug.
                  #if ps cax | grep -q mysqld; then
                  if killall -q -s0 -umysql mysqld; then
                    exit 1
                  fi
                else
                  $MYADMIN flush-logs
                fi
        endscript
}