I have an application that rotate its log once it reaches 100mb and it keeps a total of 24 logs. I am trying to write a script to run daily to tar up the previous day logs files and move them to a different directory. here is a long listing of the logs in the directory:
-rw-r--r-- 1 user1 user1 100000150 Apr 19 10:46 log.24
-rw-r--r-- 1 user1 user1 100004207 Apr 19 14:21 log.23
-rw-r--r-- 1 user1 user1 100004463 Apr 19 18:22 log.22
-rw-r--r-- 1 user1 user1 100003277 Apr 19 22:33 log.21
-rw-r--r-- 1 user1 user1 100000833 Apr 20 03:03 log.20
-rw-r--r-- 1 user1 user1 100002192 Apr 20 09:07 log.19
-rw-r--r-- 1 user1 user1 100004056 Apr 20 12:45 log.18
-rw-r--r-- 1 user1 user1 100000851 Apr 20 16:16 log.17
-rw-r--r-- 1 user1 user1 100001805 Apr 20 20:21 log.16
-rw-r--r-- 1 user1 user1 100003432 Apr 21 00:50 log.15
-rw-r--r-- 1 user1 user1 100002232 Apr 21 06:23 log.14
-rw-r--r-- 1 user1 user1 100004079 Apr 21 10:38 log.13
-rw-r--r-- 1 user1 user1 100004782 Apr 21 14:13 log.12
-rw-r--r-- 1 user1 user1 100000374 Apr 21 17:50 log.11
-rw-r--r-- 1 user1 user1 100004002 Apr 21 21:29 log.10
-rw-r--r-- 1 user1 user1 100001480 Apr 22 02:15 log.09
-rw-r--r-- 1 user1 user1 100002608 Apr 22 07:12 log.08
-rw-r--r-- 1 user1 user1 100000332 Apr 22 11:24 log.07
-rw-r--r-- 1 user1 user1 100002060 Apr 22 15:12 log.06
-rw-r--r-- 1 user1 user1 100000087 Apr 22 18:45 log.05
-rw-r--r-- 1 user1 user1 100000197 Apr 22 22:41 log.04
-rw-r--r-- 1 user1 user1 100000436 Apr 23 03:26 log.03
-rw-r--r-- 1 user1 user1 100004005 Apr 23 08:54 log.02
-rw-r--r-- 1 user1 user1 100003351 Apr 23 12:29 log.01
-rw-r--r-- 1 user1 user1 55085889 Apr 23 14:20 log
for example, if we run the script on the 23rd, it should tar up all the logs for the 22nd ( log.04-log.09) and move then to a different directory.
If I try to use the find command with the mtime option, I does not provide the all the logs for the previous day.
Your help is very much appreciated.
Have you considered logrotate?
I do not think Solaris has logrotate.
Did you try -mtime 1
?
nowdate=`date +%Y-%m-%d`
tarfile=/backups/$nowdate.tar
> $tarfile
find logdir/ -type f -mtime 1 -exec echo tar rf $tarfile {} +
Remove the echo
if you think it's okay.
---------- Post updated at 05:18 PM ---------- Previous update was at 04:55 PM ----------
If you want to immediately delete the tar'ed files you can do the following (must use the slower \;
):
...
find logdir/ -type f -mtime 1 -exec echo tar rf $tarfile {} \; -exec echo rm -f {} \;
Solaris uses logadm.
You can find some information on its usage here.
No need to reinvent the wheel.
It seems that you code does not grab all the files for the previous day. here is an example of the log die with the time stamp:
-rw-r--r-- 1 user1 user1 100001759 Apr 23 16:04 log.16
-rw-r--r-- 1 user1 user1 100004819 Apr 23 19:21 log.15
-rw-r--r-- 1 user1 user1 100000059 Apr 23 22:48 log.14
-rw-r--r-- 1 user1 user1 100001107 Apr 24 02:11 log.13
-rw-r--r-- 1 user1 user1 100000180 Apr 24 05:35 log.12
-rw-r--r-- 1 user1 user1 100002642 Apr 24 11:43 log.11
-rw-r--r-- 1 user1 user1 100001593 Apr 24 15:07 log.10
-rw-r--r-- 1 user1 user1 100002728 Apr 24 18:09 log.09
-rw-r--r-- 1 user1 user1 100003788 Apr 24 21:24 log.08
-rw-r--r-- 1 user1 user1 100003583 Apr 25 00:34 log.07
-rw-r--r-- 1 user1 user1 100002551 Apr 25 04:19 log.06
-rw-r--r-- 1 user1 user1 100004682 Apr 25 06:04 log.05
-rw-r--r-- 1 user1 user1 100001351 Apr 25 06:42 log.04
-rw-r--r-- 1 user1 user1 100004036 Apr 25 07:05 log.03
-rw-r--r-- 1 user1 user1 100001128 Apr 25 10:09 log.02
-rw-r--r-- 1 user1 user1 100002646 Apr 25 14:53 log.01
-rw-r--r-- 1 user1 user1 37367279 Apr 25 15:59 log
I f I run your code, I only get the follwoing:
find log* -type f -mtime 1 -exec echo tar -rf $tarfile {} +
tar -rf log.10 log.11 log.12 log.13 log.14 log.15 log.16
The code should return all the files with the time stamp of the previous day which are log.8-log-13.
Does the time aone matter here. My server is in CDT time zone.
Your help is very much appreciated
---------- Post updated at 11:17 AM ---------- Previous update was at 11:16 AM ----------
The application I use already rotate the logs files once they are reached 100 mb. I need to archive and tar up those logs files on a daily bases. The logadm is not useful in this case.
Hmm yes, -mtime 1
is between 24 and 48 hours ago.
Run it at midnight! No problem if you intend to use cron.
--
The thread title is misleading. "rotation" should be "archiving".