![]() |
Files don't move back to /var/log and keep getting renamed.
I have this script that I use to find log files in the /var/log directory that are 2 days old, move them to /var/log/tmp, rename them to the system date.filename and move them back to /var/log. Everything seems to work as planned, except that the files don't get moved out of temp, and they keep getting rename. This leads to very long filenames such as:
2010-02-22.user_040.2010-03-05.user_040.2010-03-07.user_040.gz What is it about this script that isn't moving it back to /var/log? Also, is there a better way of doing this than what I'm doing? Basically, I'm just trying to set up an audit trail on some of the files in /var/log, so that at the end of the month I can tar them, and then have our syslog server pick up the one giant monthly log. Code:
# Create variables. |
Have you run this script with the -x flag enabled in your shell parser to use step through so you can see where you logic is failing?
ie: bash -x <script name> |
Check out logrotate. It does what you are looking for expect moving things to /tmp. It just rotates the logs inside /var/log directory.
It quite got some power. And if you find it's missing some checkout the postrotate and prerotate option of the config files. There in you can run any shell code to suite your needs. Cheers Zhjim |
No I haven't, so I'll do that now. Here's the output:
Code:
+ alias 'rm=rm -i' |
The script comment says # First, mv most of the files but the command run by find is cp not rm ???
Try putting an echo in front of mv "$file" "$dir/$(date +%F).$hostname.$file" and you will see why it doesn't work -- no such directory. |
zhjim,
I am wanting to use logrotate, but since I haven't really figured it out too well, I was trying this approach to make it more tangible to me. Do you have any helpful hints and/or suggestions in configuring and using logrotate? |
Quote:
LOL, You're right, I never noticed that before. I guess it should be the 'mv' command, however, and not the 'mv' command as you suggested. |
Quote:
Best way to start would be to check out /etc/logrotate.conf and /etc/logrotate.d/* logrotate.conf just has overall settings. logrotate.d is a directory which gets included by logrotate and normaly holds application specific files. Most of the time you should just get the frequence of rotation (daily, weekly, monthly..) and the number of files to keep right. Also when you want to transfer the files to another machine you can do it with logrotate. I guess when you play around with it a few hours or a day you'll get the hang off it. Just ask if you're stuck. Just make up some rules and look at the output of logrotate -d your_trial_config_file Maybe this one suites http://www.debian-administration.org/articles/117 |
Quote:
You better replace $file with $(basename $file). But why do you need a temporary directory? You can do something like this: Code:
find /var/log -name "*.log*" -maxdepth 1 -type f -mtime +2 | while read lfile; do gzip -c <$lfile >$(dirname $lfile)/$(date +%F).$(hostname).$(basename $lfile).gz; done |
| All times are GMT -5. The time now is 04:26 PM. |