LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to automate archiving of log files using tar? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-automate-archiving-of-log-files-using-tar-871691/)

profi81 03-29-2011 04:37 AM

How to automate archiving of log files using tar?
 
i would need help ,
i need to tar this logs, but i dont how to make it simplier to me. Everyday there are created this five logs. I need to make five tar files from every day from this files at the end of the month
for example
wlp-process_dump.log.tar.gz ( will contain every wlp-process_dump.log from this month )
wlp-cxf.log.tar.gz
wlp-jamon.log.tar.gz
wlp-refresher.log.tar.gz
wlp.log.tar.gz

33819 Mar 21 19:20 wlp-process_dump.log.2011-03-21
5144882 Mar 21 23:52 wlp-cxf.log.2011-03-21
1106593 Mar 21 23:59 wlp-jamon.log.2011-03-21
2192832 Mar 21 23:59 wlp-refresher.log.2011-03-21
2405037 Mar 21 23:59 wlp.log.2011-03-21


till now i have tar it manualy ( copied every file )

Thanks in advance

corp769 03-29-2011 08:03 AM

How about something like this?
Code:

#!/bin/sh
input=/home/casper/logdir/
output=/home/casper/TEMP/
logfile=""
tempdir=`pwd`
cd $input
for filename in *
do
  logfile=$output$filename".tar.gz"
  tar -cvzpf $logfile $filename
done
cd $tempdir

I coded it so you can run it from any directory, and it will tar the log files located in the locations in the script. You just need to modify the directories where the log files are located. Hope this helps, and let me know if it does what you need it to do or not.

Cheers,

Josh

chrism01 03-29-2011 06:39 PM

You could use logrotate to not only rotate new log files eg each day, but to also tar & gzip the old files automatically.
http://linux.die.net/man/8/logrotate


All times are GMT -5. The time now is 08:50 AM.