Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copy file question

Status
Not open for further replies.
Oct 18, 2000
72
US
Our IT auditors would like us to start backing up the /var/log/up2date.log files on a weekly basis. Does anyone know if there is a command that will allow me to copy a file to another directory AND add a timestamp in the file name. So that the file being copied would be called up2dateDD/MM/YY.log?

Thanks!
 
logrotate (confs in /etc/logrotate.d) probably can be changed to accomodate this. BTW, you probably don't want to use '/' in the name because that is different directory!
 
an entry in /etc/crontab like this will do

Code:
30 10 * * 0 root cat /var/log/up2date.log > /some_directory/"`date +%Y%m%d_%H`_up2date.log"

ths above command will copy the up2date.log file every sunday at 10:30 and will be executed as root.

QatQat


If I could have sex each time I reboot my server, I would definitely prefer Windoz over Linux!
 
Sorry, i got it out of a similar script on my server. you may want to remove the hour (_%H) part from the script.

Code:
30 10 * * 0 root cat /var/log/up2date.log > /some_directory/"`date +%Y%m%d`_up2date.log"

QatQat

If I could have sex each time I reboot my server, I would definitely prefer Windoz over Linux!
 
Not sure if this is the case with Linux, but on Solaris, cron takes exception to the % unless it's escaped, like:

`/usr/bin/date +\%d-\%b-\%y`

I want to be good, is that not enough?
 
Yep you are right, it needs escaping.

Thanks for the correction.


QatQat

If I could have sex each time I reboot my server, I would definitely prefer Windoz over Linux!
 
Perfect, just what I was looking for guys. Thanks for the great replies. So if I edit my cron it would look something like this...?

30 10 * * 0 root cat /var/log/up2date.log > /tmp/"`/usr/bin/date +\%Y-\%m-\%d_`_up2date.log"

I understand that cron will have issues with % but I'm not sure what you mean by "escaped".

Thanks again!
 
No worries, I'll just create a script file and point cron to kick it off. Thanks for all your help! [smile]
 
Just to clarify - 'Escaped' means an insert which instructs the command interpreter to treat the next character as a literal, ie with no 'special meaning' as % does have in cron otherwise. In this (and many others), the \ is the 'escape' character.

I want to be good, is that not enough?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top