Operating System - HP-UX
1831199 Members
2821 Online
110021 Solutions
New Discussion

Re: Script to backup log file and open new log file

 
A Tamhane
New Member

Script to backup log file and open new log file

Hi

I want to add script in cron job file which should take care of ..
Check the size of the log file if it exceed 1GB then
Backup my log file with that day's date and time.
gzip backedup log file
Create new log file.
Restart process.

I am new to HP-UX. if anyone could help.

Thanks

11 REPLIES 11
baiju_3
Esteemed Contributor

Re: Script to backup log file and open new log file


Hi ,

Use the below script.

script.sh
------------
#!/usr/bin/sh

log_file=/path/file

size=`ls -al $logfile |awk '{print $5}'`

if [ $size -ge 1000000 ]
then

#backup log file
cp -p $log_file /new_location

#compress file
/path/gzip $log_file

#create new log file
touch $log_file

#Restart process , add your command below.

Restart command

fi





Good things Just Got better (Plz,not stolen from advertisement -:) )
ASH_27
Valued Contributor

Re: Script to backup log file and open new log file

Hi

Is the same script can we add date and time option ? And how ?

Thanks
Raj D.
Honored Contributor

Re: Script to backup log file and open new log file

Hi Tamhane ,

You can write a script and put it in cron to check in every 10 minute or so :
Here how it should b look like:

############################################
# script for backin up log file if larger than 1GB size. and create new logfile.

cd /logfile_dir
size=" `ls -l | grep logfile.log | awk '{print $5}'` "

echo " `echo "$size/1024" | bc` MB Current size AT: `date` "


sizemb="`echo "$size/1024" | bc ` "
##Condition :
if [ $sizemb -ge 1000 ]
then
echo " Logfile size is 1GB now at `date` "
echo "Compressing the file ...."
gzip logfile.log
mv logfile.log.gz logfile` date +%d%m%y.log
touch logfile.log
echo "Logged file moved after compress " |

## Sending email notification.
mailx -s "Log file compresseed and moved AT: `date`" yourmailid@domain.com

else
echo "Logfile not reached 1GB"

fi
##########################################



Hope this will help ,

Cheers,
Raj.

" If u think u can , If u think u cannot , - You are always Right . "
ASH_27
Valued Contributor

Re: Script to backup log file and open new log file

Hi Raj

Thanks for reply.So how do I restart the process in the script ?
Example. If I want to restart following process ...

root 571 1 1 Oct 28 ? 364:34 /usr/sbin/syslogd -D

Thanks


Geoff Wild
Honored Contributor

Re: Script to backup log file and open new log file

I would never delete and/or compress the original logfile - if it is open at the time - you could have corruption - or - logging will fail...

Instead, cp -p the log file to logfile.1 (or something like that) and then zero out the original:

> logfile

Beter yet, why not download and install logrotate - that way everything is taken care of for you:

http://hpux.ee.ualberta.ca/hppd/hpux/Sysadmin/logrotate-2.5/

Rgds...Geoff

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Raj D.
Honored Contributor

Re: Script to backup log file and open new log file

Hi ASH ,

To restart the syslogd after , the file is compressed, and moved use this command , to the very next line:

kill -HUP `cat /var/run/syslog.pid`


Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: Script to backup log file and open new log file

Hi Tamhane ,

Geoff is correct , the it should copy the file first to different place , then gzip the copied file , and then nullyfy the original file .

For setting date to the compressed file , you can use this string included in the script.


You can put it in cron to check in every 10 minute or so :
Here how it should be look like(again):

############################################
# script for backin up log file if larger than 1GB size. and create new logfile.

cd /logfile_dir
size=" `ls -l | grep logfile.log | awk '{print $5}'` "

echo " `echo "$size/1024" | bc` MB Current size AT: `date` "


sizemb="`echo "$size/1024" | bc ` "
##Condition :
if [ $sizemb -ge 1000 ]
then
echo " Logfile size is 1GB now at `date` "

### Copying to diff place & compressing. ##

dt="`date +%m%d%y`.`date +%R | awk -F : '{print $1"."$2}'`"

cp logfile.log /backupdir/logfile.$dt.log

####### Compressing the file in /backupdir...."

gzip logfile.$dt.log

####### Nullyfying the orginal log file.
cd /logfiledir
cat /dev/null > logfile.log

####### Sending email notification.
mailx -s "Log file compresseed and nullyfied the original AT: `date`" yourmailid@domain.com

####### Restarting the log.
# you can put here kill -HUP


else
echo "Logfile not reached 1GB"

fi
##########################################



Cheers,

Raj.






" If u think u can , If u think u cannot , - You are always Right . "
Bill Thorsteinson
Honored Contributor

Re: Script to backup log file and open new log file

Consider installing logrotate.
It is designed to perform regular log
rotations.
It is available at the porting centre
http://hpux.cs.utah.edu/
ASH_27
Valued Contributor

Re: Script to backup log file and open new log file

I tried running following script

cd /home/test
#size=`ls -al $logfile |awk '{print $5}'`
size=" `ls -l | grep test.log | awk '{print $5}'` "
echo " `echo "$size/1024" | bc` MB Current size AT: `date` "
sizemb="`echo "$size/1024" | bc ` "
if [ $sizemb -ge 1 ]
then
#echo " Logfile size is 1GB now at `date` "
#if [ $size -ge 1000000 ]
#then
#backup log file
cp -p $log_file /home/test/test.log

Received following error.

# ./logrotate
1973 MB Current size AT: Mon Dec 19 08:05:43 PST 2005
./logrotate[6]: Syntax error at line 7 : `then' is not matched.

If anyone helps.
Thanks
Doug O'Leary
Honored Contributor

Re: Script to backup log file and open new log file

Hey;

You need a fi at the end of your script...

if [ test condtion ]
then
...
fi

HTH

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Jeff_Traigle
Honored Contributor

Re: Script to backup log file and open new log file

Actually, for open log files, it's better to mv, not cp. That way the process that has it open can continue writing to it. You can then touch a new file with the original name, if necessary, and then restart the process so it starts using the new file. This is exactly what logrotate does when you use the "create" option. If you cp the file and the null the original, you potentially lose data.

I've been playing with logrotate on an HP-UX system. I was disappointed by the version at the Porting Center. It doesn't have as many of the options as the latest versions available as source from http://packages.qa.debian.org/l/logrotate.html, but if you have no way to compile (or don't want to bother with compiling), it'll do the basics well enough.
--
Jeff Traigle