Using cron job to copy access logs

software development


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


Set today’s date variable in yyyymmdd format

set tdy=date %Y%m%d
#copy the logs
cp logs/ ~/lgbackup
cp logs/ ~/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]/

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


[quote]set tdy=date %Y%m%d


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


The changes worked. Thank you very much.