Bash script to copy apache log files to client directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to copy apache log files to client directory
# 1  
Old 09-06-2013
Bash script to copy apache log files to client directory

Our Apache log files are written to a location on the server that we as clients have no access. Don't ask.

Every month, I have to e-mail the administrator to have him manually copy our Apache log files to a directory in our file space. You can probably guess how efficient it is to do things this way. I'm hoping to come up with a bash script the admin will run as a cron job on the second day of each month to transfer the gzipped files automatically.

He tried using this script, but it ignores the first nine days of the month, and sometimes the last day, too:

Code:
#!/usr/local/bin/bash

OFFSET=1;
eval `date "+day=%d; month=%m; year=%Y"`
# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
day=`expr $day - $OFFSET`
if [ $day -le 0 ] ;then
month=`expr $month - 1`
if [ $month -eq 0 ] ;then
year=`expr $year - 1`
month=12
fi
set `cal $month $year`
xday=${$#}
day=`expr $xday + $day`
fi
# echo $year-$month-$day
/bin/cp /usr/home/www_logs/client.$year$month$day.gz /usr/www/users/client/logs/client.com.$year$month$day.gz

My bash scripting experience doesn't reach far enough to see why this doesn't work, although I'm a little surprised it works at all.

I'd appreciate any thoughts on how to fix it, or a suggestion of an alternate script that actually works to begin with. Smilie

Thanks in advance.
# 2  
Old 09-06-2013
With your file naming convention it should be easy to pick up the most recent file:

Example:
Code:
$ touch client.20130731.tar.gz
$ touch client.20130831.tar.gz
$ touch client.20130630.tar.gz
$ set -- client.*
$ shift $(($#-1))
$ echo $1
client.20130831.tar.gz
cp $1 /some_location

Then run a simple cronjob:
Code:
0 0 2 * * /myscripttocopyfile

Where /myscripttocopyfile is:
Code:
cd /usr/home/www_logs                # cd to source directory - optional, you can 
set -- client.*                      # prepend it to the set command (i.e. set -- /var/.../client.*)
                                     # set -- puts the filenames into positional parameters $1, $2, etc.
shift $(($#-1))                      # remove all ($#) but 1 (-1) of the positional parameters
cp "$1" /usr/www/users/client/logs   # $1 is now the latest file (alphabetically); copy it to destination


Last edited by Scott; 09-06-2013 at 02:15 PM.. Reason: Edited prompt from # to $; added explanation of script
This User Gave Thanks to Scott For This Post:
# 3  
Old 09-06-2013
Wow, that looks simple. Still don't understand it, though. Sorry.

The set and shift commands look to be commented out, yet it seems they have to be executed to make the thing work.

What am I missing?

---------- Post updated at 09:57 AM ---------- Previous update was at 09:56 AM ----------

Ah, I'm missing the bottom code section. Let me try that and see what happens.

Thanks again.
# 4  
Old 09-06-2013
The first section was an example, run from the command prompt (I have changed the # to a $ - since I was running as root, the prompt is #, not $ Smilie).

The second is the cronjob, and the third an example script you could use.
This User Gave Thanks to Scott For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to copy subfolder and files to matching directory

The bash executes but returns no results and the set -xv showed while the $run variable in blue, was extracted correctly, the $match value in green, was not, rather both values in home/cmccabe/Desktop/f1 were extracted not just the matching. There will always be an exact match from the $run to... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Need a shell script which takes two inputs and copy the files from one directory to other

Hi, I am using solari 10 OS which is having bash shell. I need a shell script which takes user home directory and name of the file or directory as a input and based on that copy the files accordingly to the other directory. example:I hava a machine1 which is having some files in a... (8 Replies)
Discussion started by: muraliinfy04
8 Replies

3. UNIX for Dummies Questions & Answers

Bash script to rename files in a directory

Dear friends, I have created a script to rename all files in a directory by appending the file name with username (who created the file), the date it was created. For example, "apple.doc" should be renamed to "johnFeb23apple.doc" where "john" is the owner and "Feb23" is file created date. It... (4 Replies)
Discussion started by: djsnifer
4 Replies

4. Shell Programming and Scripting

Bash script to copy timestamps of multiple files

Hi, I have a bunch of media files in a directory that have been converted (from MTS to MOV format), so my directory contains something like this: clip1.mts clip1.mov clip2.mts clip2.mov The problem is that the .mov files that have been created have the timestamps of the conversion task,... (2 Replies)
Discussion started by: Krakus
2 Replies

5. Shell Programming and Scripting

bash script to copy files

hey everyone, new here i have arch setup and i am using smbnetfs to mount some windows shares in /mnt/smbnet what i want to do is copy files from my home dir to a dir in /mnt/smbnet but i also need it to remove files if i have deleted them from my home dir seems that cp would be the... (8 Replies)
Discussion started by: dodgefan67
8 Replies

6. Shell Programming and Scripting

Script to Poll Directory and Copy files

Hi all, I'm looking for a script to poll a specified directory and copy new files to another location. The script should only copy new files so, I based on mtime I guess? Can anyone point me in the right direction of a script which could do this? My scripting skills aren't too bad, but... (1 Reply)
Discussion started by: JayC89
1 Replies

7. Shell Programming and Scripting

error in sh script while copy files to a backup directory

I am trying to copy files from one directory to another using shell script. Can anyone please troubleshoot the code. thanks in advance... #!C:\Shell\sh.exe files_dir="C:\Documents and Settings\scripts\files" backup_dir="C:\Documents and Settings\scripts\ztest" echo cding to... (2 Replies)
Discussion started by: sureshcisco
2 Replies

8. Web Development

bash script editing my apache config files

okay i'm going to try to say this uber-simple: I use dropbox (file-sync service). in order for dropbox sync files, they must be its children eg. somewhere under /home/jzacsh/Dropbox]. I want to now use it to keep my development files in sync across my machines: easy: just move my dev. files... (2 Replies)
Discussion started by: jzacsh
2 Replies

9. Shell Programming and Scripting

Simple BASH shell script to rename webcam jpg and copy into a new directory.

System: Ubuntu Intrepid Ibex I'm running webcamd as a sort of "security" program, but I need a script that will archive my webcam.jpg files. So, take the following file: /home/slag/www/webcam.jpg Rename it--preferably with a time stamp. Place it in say: /home/slag/www/history/ ... (4 Replies)
Discussion started by: robfindlay
4 Replies

10. Shell Programming and Scripting

shell script to find and copy the files creted in the year 2006 to another directory

Hi All, I am new to UNIX. I will be thankful if some one helps me. I have to write a shell script for one of the requirement. I have files created from Jan 2006 to March 2008. My requirement is to write a script in such a way that 1) To find and copy(not Moving) the files created in the... (2 Replies)
Discussion started by: manas6
2 Replies
Login or Register to Ask a Question