Something that I'd like to share with you!

Thursday, August 05, 2010

Using logrotate to rotate and archive log

No comments :
Previously I found out that rotatelogs cannot replace the rotated logs, I try to find other option.

I always come across logrotate whenever trying to search for rotatelogs.

So I give them a try.

1st step is to find it. BTW, I'm on SuSE Linux Enterprise 10.

$ logrotate
-ksh: logrotate: not found [No such file or directory]

Since it is not in the path, try to find it by doing this.

$ whereis logrotate
logrotate: /usr/sbin/logrotate /etc/logrotate.d /etc/logrotate.conf /usr/share/man/man8/logrotate.8.gz

Now try to run it with full path.

$ /usr/sbin/logrotate

Again, an error occurs.

error: error creating state file /var/lib/logrotate.status: Permission denied

This is because the user that we are currently on don't have write access /var/lib/logrotate.status.

-rw-r--r--  1 root   root  1246 2010-07-28 09:30 logrotate.status

Ask help from system root to change it...

# chmod 666 logrotate.status

...to

-rw-rw-rw-  1 root   root  1246 2010-07-28 09:30 logrotate.status

So, now try again.

$ /usr/sbin/logrotate
logrotate 3.7.3 - Copyright (C) 1995-2001 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License

Usage: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail command]
[-s|--state statefile] [-v|--verbose] [-?|--help] [--usage]
[OPTION...] 

At last. Now create a simple conf like below.

$ cat ./logrotate.conf
/home/username/test/mylog {
rotate 5
daily
copytruncate
notifempty
compress
}


From my observation by triggering it manually (based on configuration above), I found out that it start by compressing the log file and name it mylog.1.gz.
When I trigger it again, it will rename the mylog.1.gz to mylog.2.gz and create a new mylog.1.gz. It will continue to do that but will not exceed mylog.5.gz (rotate 5 from conf file).

-rw-r--r-- 1 user group  348 2010-07-28 15:32 mylog
-rw-r--r-- 1 user group   54 2010-07-28 15:32 mylog.1.gz
-rw-r--r-- 1 user group   59 2010-07-28 15:32 mylog.2.gz
-rw-r--r-- 1 user group   58 2010-07-28 15:32 mylog.3.gz
-rw-r--r-- 1 user group   51 2010-07-28 15:32 mylog.4.gz
-rw-r--r-- 1 user group   57 2010-07-28 15:32 mylog.5.gz

This is just like what I wanted. It will keep only 5 gz files. Now you can schedule (cron) them accordingly.

No comments :