LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to make a cp script (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-make-a-cp-script-887879/)

chris1234 06-23-2011 02:09 AM

how to make a cp script
 
Hello; i am new in using Linux and i installed a tacacs server using centos 5 distribution, but now my boss asked me to make a periodic backup of the log file of this tacacs+ server the easiest way that i found is to use this command : cp /var/log/tacacs.log /home/tacacs1/backup/taclog`date +%y%m%d` to copy the file in another file and then create a script to run it automatically and put it in a cron to run it every week ,can someone help me on how to proceed i just have the idea but not the practical skills to do it.

Thank you

acid_kewpie 06-23-2011 02:13 AM

That's not a good way to do it. Use logrotate as that is *exactly* what it's there for.

/etc/logrotate.d/tacacs:
Code:

/var/log/tacacs.log {
  rotate 10
  daily
  compress
  }

this will keep the last 10 days log files, automatically purging older ones, and compressing them all as it goes. I'm sure you can fine tune it from there.

chris1234 06-23-2011 04:42 AM

get log with date
 
Thank you very much but do you mean that i have to access the logrotate file then modify it ? and another question if i want to get the tacacs.log file of each week without deleting or compressing the last one and on each new file be named with the date do you think that this logrotate should do that?

Thank you

TB0ne 06-23-2011 09:50 AM

Quote:

Originally Posted by chris1234 (Post 4393557)
Thank you very much but do you mean that i have to access the logrotate file then modify it ? and another question if i want to get the tacacs.log file of each week without deleting or compressing the last one and on each new file be named with the date do you think that this logrotate should do that?

Thank you

Why don't you read the docs on logrotate, which answer those questions?? Also, since you know the cp command that you need, what's preventing you from just putting it into a script file, and running it with cron??? Seems like you've got the answers to your questions already.

chris1234 06-23-2011 10:16 AM

i have the answer in theory but i don't know how to put it in practice

lithos 06-23-2011 11:26 AM

well, since this is a trivial question, even I could provide you some help :D

Create a script file and make it executable, let's say myscript.sh (chmod 755 myscript.sh):
Code:

#!/bin/sh
cp /var/log/tacacs.log /home/tacacs1/backup/taclog-$(date +"%Y-%m-%d(%H-%M-%S)")

#output is like: taclog-2011-06-23(11-11-55)

now put it into Crontab to run at desired time/day of week ... you choose when (schedule task in crontab or crontab-quick-reference)
edit Crontab:
crontab -e
and if you want to execute every Friday at 1.00 AM put the following line into Crontab:
Code:

0 1 * * 5 /path/to/this/myscript.sh
save & exit with ":x"

It should do it just fine and then you will get an email with output of the execution time (or possibly error if something wrong). If you don't want emails then add >/dev/null at the end of cp line in myscript.sh


There you have it, but that's a quick and lazy way to do it, since you don't know what is done, so go for some Docs about shell scripting and the commands you want to use.

good luck...

TB0ne 06-23-2011 03:03 PM

Quote:

Originally Posted by chris1234 (Post 4393778)
i have the answer in theory but i don't know how to put it in practice

lithos got it, but really???

The cp command was the hard part, and you already had it. You should check out one of the many, MANY bash scripting tutorials, and read the man page for crontab.

chrism01 06-23-2011 05:55 PM

@lithos: are you sure you want () chars in a filename? It may cause issues later. If you want date & time, I usually go with this format: YYYYMMDD_HHMMSS, although going down to the 'seconds' level is usually overkill.

lithos 06-24-2011 04:14 PM

@chrism01:
you're right, it's better like that "YYYYMMDD_HHMMSS"
so the myscript.sh should have cp line:
Code:


cp /var/log/tacacs.log /home/tacacs1/backup/taclog-$(date +"%Y%m%d_%H%M%S")

tested and "ls" prints out 'wtmp-20110614_131245'

Thank you for noticing that.


All times are GMT -5. The time now is 04:23 AM.