LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-23-2011, 03:09 AM   #1
chris1234
LQ Newbie
 
Registered: Jun 2011
Posts: 3

Rep: Reputation: Disabled
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
 
Old 06-23-2011, 03:13 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974Reputation: 1974
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.
 
Old 06-23-2011, 05:42 AM   #3
chris1234
LQ Newbie
 
Registered: Jun 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-23-2011, 10:50 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,318

Rep: Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879
Quote:
Originally Posted by chris1234 View Post
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.
 
Old 06-23-2011, 11:16 AM   #5
chris1234
LQ Newbie
 
Registered: Jun 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
i have the answer in theory but i don't know how to put it in practice
 
Old 06-23-2011, 12:26 PM   #6
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

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

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...

Last edited by lithos; 06-23-2011 at 12:35 PM.
 
1 members found this post helpful.
Old 06-23-2011, 04:03 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,318

Rep: Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879Reputation: 3879
Quote:
Originally Posted by chris1234 View Post
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.

Last edited by TB0ne; 06-23-2011 at 04:04 PM.
 
Old 06-23-2011, 06:55 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,258

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
@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.
 
1 members found this post helpful.
Old 06-24-2011, 05:14 PM   #9
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
@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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 02:27 PM
[SOLVED] How to make a bash script keep running in same terminal after it calls second script? JohnRock Linux - Newbie 4 06-25-2010 10:16 AM
script executed inside another script dont make changes potraike Linux - Newbie 4 03-05-2008 06:27 AM
How do I make a simple script and make it run on bootup?? coolblue Programming 5 12-18-2006 10:55 PM
can i make a script or program make a phone call? nephish Linux - Software 2 08-15-2005 10:02 PM


All times are GMT -5. The time now is 12:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration