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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-28-2012, 09:27 PM
|
#1
|
LQ Newbie
Registered: Apr 2012
Posts: 5
Rep: 
|
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.
|
|
|
04-28-2012, 09:42 PM
|
#2
|
Member
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190
Rep:
|
I am thinking that you need to create a shell script than include it to in crontab.
|
|
|
04-28-2012, 09:43 PM
|
#3
|
LQ Newbie
Registered: Apr 2012
Posts: 5
Original Poster
Rep: 
|
And I wold have NO idea how to do that.
|
|
|
04-28-2012, 09:51 PM
|
#4
|
Member
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190
Rep:
|
read bash scripting on google/other website. I will try to create it later on.
|
|
|
04-30-2012, 07:30 AM
|
#5
|
Member
Registered: Apr 2012
Location: /root
Distribution: Ubuntu, Redhat, Fedora, CentOS
Posts: 190
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 10:06 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|