using cron job to copy access logs

using cron job to copy access logs

Posted by: anks329
Posted on: 2005-12-30 19:16:00

I'm trying to set up a way to automatically copy my access logs to a backup folder so that I don't lose them. So far, I've managed to create a script that copies access.log.0 into a different folder and then adds the current date to the end of it. It works fine when i run it from the shell. So the next step was the automate with a cron job. The cron job copied the files, but didn't rename them correctly.

Here is the code for script (its called script1.sh):

#!/bin/bash

# Set today's date variable in yyyymmdd format
set tdy=`date %Y%m%d`
#copy the logs
cp logs/anksconsulting.com/http/access.log.0 ~/lgbackup
cp logs/anksconsulting.com/http/error.log.0 ~/lgbackup
# rename the logs
mv lgbackup/access.log.0 lgbackup/access.log.$tdy
mv lgbackup/error.log.0 lgbackup/error.log.$tdy

unset tdy


and then the cron job calls the script as follows:
0 19 * * * /home/[username]/script1.sh


any suggestions as to what I should do to fix this?

Edited by anks329 on 12/30/05 07:16 PM (server time).

Re: using cron job to copy access logs

Posted by: will
Posted on: 2005-12-31 00:04:00

> set tdy=`date %Y%m%d`

That should be just:
TDY=`date +%Y%m%d`
[...]
mv lgbackup/access.log.0 lgbackup/access.log.${TDY}

Re: using cron job to copy access logs

Posted by: anks329
Posted on: 2006-01-01 07:20:00

The changes worked. Thank you very much.

Re: using cron job to copy access logs

Posted by: ardco
Posted on: 2006-01-01 08:10:00

A couple thoughts, FWIW.

You could do one copy step rather than copy, move. Also, since the x.log.0 files are already linked to a file with date appended, you could use something like:

cp /home/user/logs/example.com/http/`readlink /home/user/logs/example.com/http/error.log.0` ~/lgbackup/

(assuming readlink works in cron)

Cheers,

BobS

Tags: cron jobaccess loglogshttpbackup folderaddsshellerror log