1827324 Members
4522 Online
109962 Solutions
New Discussion

archive script

 
SOLVED
Go to solution
Ragni Singh
Super Advisor

archive script

Hi all, attached is a script but I need a little bit more help.

ls -l

postgresql-2006-01-15_000000.log postgresql-2006-01-22_000000.log
postgresql-2006-01-16_000000.log postgresql-2006-01-23_000000.log
postgresql-2006-01-17_000000.log postgresql-2006-01-24_000000.log


find /var/log/pgsql/* -type f -mtime 0 > /tmp/today_files.txt

for a in `find /var/log/pgsql/* -type f -mtime +7`;

do

IS_TODAY=`grep -c $a /tmp/today_files.txt`
if [ "$IS_TODAY" = "1" ]; then

echo "skipping $a"

else

echo "zipping $a"
gzip $a

fi

done


INstead of doign it for all files, I just need to be able to do it to the *.logs. I don't want to keep re-archiving all files.

Also, I would like all files for a single week zipped or tarred into a common file so that we can use logrotate to handle management of these weekly archives for the rotations.

Any help will be greatly appreciated and points will be assigned.
8 REPLIES 8
Ivan Ferreira
Honored Contributor

Re: archive script

Try something like this:


> /tmp/filelist.txt
find /var/log/pgsql/* -type f -mtime 0 > /tmp/today_files.txt

for a in `find /var/log/pgsql/* -type f -mtime +7`;

do

IS_TODAY=`grep -c $a /tmp/today_files.txt`
if [ "$IS_TODAY" = "1" ]; then

echo "skipping $a"

else

echo $a >> /tmp/filelist.txt
fi
done


tar zcvf files.tgz -T /tmp/filelist.txt
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ragni Singh
Super Advisor

Re: archive script

Ivan, but is that just going to do it to the *.logs. My whole goal here is so that it doesn't keep re-archiving the same files.
Ivan Ferreira
Honored Contributor

Re: archive script

Simply, replace your find commad with:

find "/var/log/pgsql/*.log"
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ivan Ferreira
Honored Contributor

Re: archive script

OH MY!!!! Ignore my last output, the find command sould be like this:

find /var/log/pgsql/ -type f -name "*.log"


I don't know what I was thinking.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ivan Ferreira
Honored Contributor

Re: archive script

I was checking your sample script, your are searching for files older than 7 days, and verifying if the files where created today. You'll never get a match. You should use -mtime -7 instead.

You can also use:

find /var/log/pgsql -name "*.log" -mtime +1 -mtime -7
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ivan Ferreira
Honored Contributor
Solution

Re: archive script

So, your script could be:

tar zcvf backup.tgz $(find /var/log/pgsql/ -type f -name "*.log" -mtime +1 -mtime -7)
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ragni Singh
Super Advisor

Re: archive script

that will not work becasue what I am doing is...searching for all files *.log, except for the one currently being written to and zipping everything up except that one.
Ivan Ferreira
Honored Contributor

Re: archive script

How do you know that is in use?, check that "-mtime +1 and mtime -7" will compress all files created since one day until seven days ago, so, today files or used files won't be saved. Isn't that what you where looking for? You can also try using the fuser command to verify if the file is used. Or you can modify your script and mix the commands showed in this thread.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?