Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
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.
I would like to know how I should go about rotating files that end with a date stamp. This is the configuration I have to rotate my Apache access files, but it is not working:
/var/log/httpd/access_log.* {
compress
daily
rotate 1
copytruncate
missingok
notifempty
}
The files are created with a date extension like the ones below:
A couple of years ago, I wrote a script to rotate my log files daily. At the time I was self-hosting using the lampp server with all security enabled.
I don't have a copy of the script any more, but this is what I did:
1. Stopped my server services.
2. Declared a variable for date and time and created a directory to hold the backup named by date and time.
Code:
#Define now as today’s day-month-year-hour-minute.
NOW=$(date +%m-%d-%Y-%H-%M)
#Make the directory to hold the backup file.
mkdir /root/backups/log/$NOW
3. Gzipped the files into the directory /root/backups/log/$NOW. Had I needed to, I could have added an mv command to rename the files including the date and time, but having them separated in the named directories suited my needs.
for files in $LISTACC $LISTERR
do
if [[ $files != *.bz2 ]] && [[ -f $files ]]
then
bzip2 $files
fi
done
for files1 in $LISTACC1 $LISTERR1
do
#if [[ $files1 == *.bz2 ]]
#then
#echo "files ones are: $files1"
$newfilenames=`echo $files1 | sed 's/bz2/1.bz2/g'`
#echo "the new names are $newfilenames"
#mv $files1 $newfilenames
#fi
done
However, for the second for loop, it is not working. These are my log files:
When I run the script, it gives me and error as follows:
[root@scripts willie]# bash newnanme.sh
access_log.073010-00_00.1.bz2
access_log.073010-00_00.2.bz2
access_log.073010-00_00.3.bz2
newnanme.sh: line 38: =access_log.073010-00_00.1.bz2: command not found
newnanme.sh: line 38: =access_log.082810-00_00.1.bz2: command not found
newnanme.sh: line 38: =access_log.083010-00_00.1.bz2: command not found
newnanme.sh: line 38: =access_log.1.bz2: command not found
newnanme.sh: line 38: =error_log.042310-00_00.1.bz2: command not found
newnanme.sh: line 38: =error_log.070910-00_00.1.bz2: command not found
newnanme.sh: line 38: =error_log.080510-00_00.1.bz2: command not found
newnanme.sh: line 38: =error_log.1.bz2: command not found
[root@scripts willie]#
Frankbell, this part is working properly as far as I know, but now that you are asking the question, I will double check to make sure. I don't do it at this moment because I am kind of in a hurry.
Works (but I will double check):
for files in $LISTACC $LISTERR
do
if [[ $files != *.bz2 ]] && [[ -f $files ]]
then
bzip2 $files
fi
done
This is the part that I think is not working:
$newfilenames=`echo $files1 | sed 's/bz2/1.bz2/g'`
It fails when it tries to rename the file. The funny story is that it works if I run it manually on the command line.
sleddog:
The situation is that the person who wants these logs rotated is using the apache rotatelogs utility. This person is not using /etc/logroate.d. Also, I tested logrotate -f, and tried it, but it did not work. However, I will try it again. I will try ogrotate -dv /path/to/configfile to see what it says.
It fails when it tries to rename the file. The funny story is that it works if I run it manually on the command line.
This is good news and here's why: You know the issue is something to do with the "mv" command and the associated elements of the script. 95% of trouble-shooting is finding the trouble.
I can only tell you what I would do:
Set up a test directory.
Run the script. See what fails (not just in the error messages, but also in the results on the hard drive).
Make ONE change.
Try again.
Repeat, one chance at a time.
By the way, "make one change" is why I declared the my $NOW variable to the minute. I could make one change, wait a minute, try again, and retest.
One change at a time.
If you make two changes, you will not know which one worked.
I wish I knew enough about scripting to just look at the script and say, "do this," but I don't. But I do know trouble-shooting.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.