Compressing previous log automatically

I want to create a script that will zip the previous log.

Example.

abc.log.2012.12.02
abc.log.2012.12.01.gzip
abc.log

If today is 2012.12.03 , my current log is abc.log and my previous date is 2012.12.02, i want abc.log.2012.12.02 to compress everytime I run the script.

I can manually run compress the file
gzip abc.log.2012.12.02 but I want the log to automate every 2am in the morning.
Every 1200midnight the file is rotating and it creates a previous date, and I want this previous date file to be compress.

I now how to use the cronjob but i dont have script to do it. Please help.

You don't need a script for this. You can put this entry in cron: gzip /home/logs/myApp/abc.log.$(date -d "-1 day" +%Y.%m.%d)

1 Like
 
#!/bin/bash
YEST=`TZ="GMT+24" date +'%Y.%m.%d'`
echo "Yesterday : $YEST"
#gzip the Yesterday file
gzip -f abc.log.${YEST}

if you have gnu date, then you can use it as below

 
YEST=$(date -d "yesterday" +%Y.%m.%d)

Can't you compress the file in the same job that it's created in?

Don't you have logrotate (or something alike) in your system?
What's wrong with it?
--
Bye

1 Like

thanks a lot for your kindness it works perfectly