LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 04-28-2012, 10:27 PM   #1
ShadowWizard
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Rep: Reputation: Disabled
using cron to backup logs in unusual circumstances


I don't know much about linux to be honest, I am just learning because I have a server running on one. What i am doing is looking for a way to automatically backup the log files, compress them, and put them somewhere else. Here is the thing, the server creates a new log file every time it is restarted with the date and time of the restart in the file name. So if it is not restarted for days, the log file will indicate a day several days ago, because that is when it was originally created. Next problem, if that log file disappears, the server doesn't create another one, it just stops logging.
So, basically I need to find a way to make a command for Cron to "Backup and delete all but the most recent log file." And I have no idea where to begin.
Just to clarify in case I am not totally clear. We will say today's date is well, today. 4-28. I have the following log files:
4-18.log
4-22.log
4-23.log
4-27.log
I need to backup and delete 4-18, 4-22, and 4-23. But I need to leave 4-27 alone.
Now, keep in mind that the most recent log file is not necessarily for yesterday. In the above example 4-23.log contains information from the 23rd, 24th, 25th, 26th and part of the 27th. The only reason 4-27 was created was because the server was restarted on the 27th, and thus because today is the 28th 4-27 will contain log information frm the rest of the 27th and the 28th.
I was thinking (if it is even possible) to create a cron entry that will do this for files that had there last access date yesterday. But again, I have NO idea how to do any of this.

Thanks in advance.
 
Old 04-28-2012, 10:42 PM   #2
em31amit
Member
 
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190

Rep: Reputation: 55
I am thinking that you need to create a shell script than include it to in crontab.
 
Old 04-28-2012, 10:43 PM   #3
ShadowWizard
LQ Newbie
 
Registered: Apr 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
And I wold have NO idea how to do that.
 
Old 04-28-2012, 10:51 PM   #4
em31amit
Member
 
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190

Rep: Reputation: 55
read bash scripting on google/other website. I will try to create it later on.
 
Old 04-30-2012, 08:30 AM   #5
em31amit
Member
 
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190

Rep: Reputation: 55
here is one script, i have created for you. You need to change some variables values accordingly to your need

this script will backup and delete file except last reboot date file.

try this, I am pretty much sure that it will work.

Quote:
#!/bin/bash

# Created By : Amit M.
# em31amit


#Last Reboot date
RDATE=4-28

BackupName="backup-`date +%s`.tgz"

#Backup Directory Location
BackupDir=/var

#Log Files location
LogDir=/var/tmp

echo " Changing Directory to $LogDir"

cd $LogDir

echo " Taking backup to All Log file to $BackupDir Directory"

/bin/tar -czvf $BackupDir/$BackupName *.log --exclude=$RDATE.log

echo " Deleting files all except $RDATE.log (Latest Preserving scheme)"

find $LogDir -type f \( -iname "*.log" ! -iname "$RDATE.log" \) -print -exec rm -rf {} \;

echo " All Done "

echo " Backup file name is $BackupDir/$BackupName "

If you found this information is usefull and working than please mark is yes and solved.
 
  


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
debian cron logs jancat Linux - Server 2 09-07-2008 03:54 PM
firewall logs and cron yongitz Linux - Security 5 06-05-2006 02:30 AM
Unusual ssh cron message jburford Linux - Security 4 10-31-2005 08:57 AM
Mailing daily logs via cron? ladyath Linux - General 3 03-30-2005 12:51 AM
Apache Logs - Showing unusual domains mdavis Linux - Security 14 04-22-2004 11:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:51 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