Files don't move back to /var/log and keep getting renamed.
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
dir="/var/log"
tmp="$dir/tmp"
hostname="$(uname -n)"
# Create the temporary directory.
mkdir -p "$tmp"
# First, mv most of the files:
for log in kdm kernel cron ksyms messages rpmpkgs vmke vmkw secure; do
find "$dir" -name "$log.*" -maxdepth 1 -type f -mtime +2 -exec cp "{}" "$tmp" +
done
# Now mv any files that were not mv'd the first time.
# NOTE: next line will possibly mv files that were already mv'd above!
#find "$dir" -name "*.log*" -maxdepth 1 -type f -mtime +2 -exec cp "{}" "$tmp" +
# mv all files to new filename, and put back in $dir:
for file in "$tmp"/*; do
mv "$file" "$dir/$(date +%F).$hostname.$file"
#gzip -c -9 "$file" > "$dir/$(date +%F).$(hostname).$(file).gz"
done
# uncomment next line to actually permanently delete $tmp in production
# rm -Rf "$tmp"
Any help you can provide would be greatly appreciated. I look forward to your thoughts and suggestions.
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.
No I haven't, so I'll do that now. Here's the output:
Code:
+ alias 'rm=rm -i'
+ alias 'cp=cp -i'
+ alias 'mv=mv -i'
+ '[' -f /etc/bashrc ']'
+ . /etc/bashrc
+++ id -gn
+++ id -un
+++ id -u
++ '[' root = root -a 0 -gt 99 ']'
++ umask 077
++ '[' '' ']'
+ dir=/var/log
+ tmp=/var/log/tmp
++ uname -n
+ hostname=esx5.trdm
+ mkdir -p /var/log/tmp
+ find /var/log -name 'kdm.*' -maxdepth 1 -type f -mtime +2 -exec cp '{}' /var/log/tmp +
find: missing argument to `-exec'
+ find /var/log -name 'kernel.*' -maxdepth 1 -type f -mtime +2 -exec cp '{}' /var/log/tmp +
find: missing argument to `-exec'
+ find /var/log -name 'cron.*' -maxdepth 1 -type f -mtime +2 -exec cp '{}' /var/log/tmp +
find: missing argument to `-exec'
+ find /var/log -name 'ksyms.*' -maxdepth 1 -type f -mtime +2 -exec cp '{}' /var/log/tmp +
find: missing argument to `-exec'
+ find /var/log -name 'messages.*' -maxdepth 1 -type f -mtime +2 -exec cp '{}' /var/log/tmp +
find: missing argument to `-exec'
+ find /var/log -name 'rpmpkgs.*' -maxdepth 1 -type f -mtime +2 -exec cp '{}' /var/log/tmp +
find: missing argument to `-exec'
+ find /var/log -name 'vmke.*' -maxdepth 1 -type f -mtime +2 -exec cp '{}' /var/log/tmp +
find: missing argument to `-exec'
+ find /var/log -name 'vmkw.*' -maxdepth 1 -type f -mtime +2 -exec cp '{}' /var/log/tmp +
find: missing argument to `-exec'
+ find /var/log -name 'secure.*' -maxdepth 1 -type f -mtime +2 -exec cp '{}' /var/log/tmp +
find: missing argument to `-exec'
++ date +%F
+ mv '/var/log/tmp/*' '/var/log/2010-03-15.esx5.trdm./var/log/tmp/*'
mv: cannot stat `/var/log/tmp/*': No such file or directory
So this looks to me like it is thinking that I am wanting to send the files to /var/log/tmp/*, which is what I'm not trying to do. There also appears to be a missing argument to `-exec' command according to the output, but if you look at the script, there is a copy command.
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?
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?
man logrotate
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
# mv all files to new filename, and put back in $dir:
for file in "$tmp"/*; do
mv "$file" "$dir/$(date +%F).$hostname.$file"
#gzip -c -9 "$file" > "$dir/$(date +%F).$(hostname).$(file).gz"
done
The error is in this command: the variable $file in the while loop will contain the full path of the temporary file. Thus the mv command will fail, since the slashes in the full path will be regarded as subdirectories, which do no exist.
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
The errors you mention about find are due to the fact, that -exec ... + only allows {} at the end. So try "-exec cp -t $tmp {} +" instead.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.