Using cron job to copy access logs

software development

#1

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?


#2

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

[/quote]

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


#3

The changes worked. Thank you very much.