Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have logrotate set up to archive logs for 30 days; how do I set up my cron job to rotate them automatically from /var/log/net to the long-term storage I have mounted on /mnt/backup?

I do not need to mangle the name when I archive them.

EDIT:

Example of file naming... wireless.log-20120916.gz... there is no fixed log name to key from (which is the assumption in Nikolaidis Fotis' first answer)

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You could create a second logrotate configuration and use as post script something like

#!/bin/bash
LogDate=$(date +"%s")
mv /var/log/messages.1 /data/logs/local_backup/var/log/messages/messages.$LogDate
gzip /data/logs/messages.$LogDate
exit

like here http://www.ashishnepal.com/logrotate-and-move-to-backup-directory/

EDIT

New approach ...

/bin/find $path -mtime 29 -exec cp -p {} /newPath/ \;

you can either execute it from cron job or post process in logrotate

share|improve this answer
    
i like this solution better as it does not use any further 3rd party languages or packages then what is already installed. i found another useful link here. –  au_stan Oct 16 '12 at 13:18
    
So ... you want to merge the loglines of the last 30 days into a single file called October_2012 (or similar name) ? Do you want to place them under a dir called october_2012/ ? What I understood is that you want to move files older than 30 days to another filesystem. (of course the above was just a sample, which you can modify based on your needs) –  Nikolaidis Fotis Oct 16 '12 at 16:37
    
@NikolaidisFotis, I think I understand where the disconnect is now... please see my edit –  Mike Pennington Oct 16 '12 at 16:54
    
find <path> -mtime +29 is not correct, that should be find <path> -mtime 29 (at least on my CentOS system) –  Mike Pennington Oct 16 '12 at 19:25

EDIT:

This was my inefficient approach until I realized I should be using find ... -exec. Anyone finding this question via search engines should use Nikolaidis Fotis' answer.


Original answer for historical purposes...

I am using this in my cron job and it works well...

/usr/bin/python -c "from subprocess import call;from datetime import datetime;import os;today=datetime.today();dir='/var/log/net/';[call('cp -p %s /mnt/backup/' % (dir+file), shell=True) for file in os.listdir(dir) if (today - datetime.fromtimestamp(os.stat(dir+file).st_mtime)).days==29]"

The reason I look for a file modification time of 29 days (st_mtime) is because that is how old the modification time is before logrotate deletes files from the /var/log/net directory.

If you don't like enormous one-liners above, you could save this as a file and run it from cron

from subprocess import call
from datetime import datetime
import os
today=datetime.today()
dir='/var/log/net/'
longterm = '/mnt/backup/'
[call('cp -p %s %s' % (dir+file, longterm), shell=True) \
    for file in os.listdir(dir) if (today - \
    datetime.fromtimestamp(os.stat(dir+file).st_mtime)).days==29]

I am using cp -p to ensure I save file modification times so I can use a similar script to delete them from long-term storage after a couple of years.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.