LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-02-2003, 01:32 PM   #1
Supp0rtLinux
Member
 
Registered: Dec 2001
Location: Carlsbad
Distribution: LFS 4.0 (www.linuxfromscratch.org)
Posts: 44

Rep: Reputation: 15
Question Monthly Archiving Script... help with "date" & "cron"


My f/w emails logs to me daily. Those logs are trapped, parsed, and spit out into an HTML page on our monitoring page. This happens daily at 8:00am. For liability reasons, i have to keep all my firewall logs. I've written a script that creates a new dir in the format of Dec02 and copies all the logs from that month into the new dir, thus archiving them monthly. Only one prob... the script is in cron on the 1st of the month... so instead of making a Dec02 dir and copying the Dec files over... it created a Jan03 dir and copied nothing over.

I see two alternatives... A) I make cron run it the last day of the month... but this is problematic as that can vary from the 28 to the 31st. Or B) I make it 'shift' a month... so that when the output of `date` says Jan, it shifts back to Dec.

Any gurus out there have any thoughts on this? Can a "-" be sent to date in some fashion to shift back a month? How do I tell cron to run on the last day of the month when I that varies each month? How would I do this differently?

FYI... this cannot be done in perl. For reasons I won't explain, this must be done in standard shell tools such as date, awk. sed, etc.

My script is below:

>>> begin script <<<
DIR=/home/www/status/bb/firewall/archive

export DIR

# First we set up our new directory and set perms on it
/bin/mkdir $DIR/`date +%b%y`
/bin/chown fwemail:apache $DIR/`date +%b%y`
/bin/chmod 755 $DIR/`date +%b%y`

# Now we move the relevant files to the new directory
/bin/mv $DIR/firewall_log.`date +%m`??`date +%y`.html $DIR/`date +%b%y`
>>> end script <<<
 
Old 01-02-2003, 08:54 PM   #2
speck
Member
 
Registered: Nov 2001
Location: US
Distribution: Slackware 14.2
Posts: 375

Rep: Reputation: 115Reputation: 115
You should be able to do something like:

LAST_MONTH=`date "+%b%y" --date='1 month ago'`
mkdir $LAST_MONTH

If you definitely run the cron job only on the first day of the month, you could change it to "--date='1 day ago'" or "--date='yesterday'". Do a "info date" and look at the examples page if you want to see more options that you can use with "--date". Good Luck.

Speck
 
Old 01-03-2003, 03:17 AM   #3
Supp0rtLinux
Member
 
Registered: Dec 2001
Location: Carlsbad
Distribution: LFS 4.0 (www.linuxfromscratch.org)
Posts: 44

Original Poster
Rep: Reputation: 15
Thank you so much! That's exactly what I was looking for. The man pages don't do the 'date' utility justice... I wouldn't have found this if it weren't for your reply.

Here's what I just tested and this will work perfectly for what I need:

bash-2.05a$ date
Fri Jan 3 01:11:29 PST 2003
bash-2.05a$ date +%b
Jan
bash-2.05a$ date +%b --date='last month'
Dec

And since my original post revealed another issue to me about archiving something after midnite for the previous days logs, but having the current days date stamp... I can now fix it with this:

bash-2.05a$ date +%D
01/03/03
bash-2.05a$ date +%D --date='yesterday'
01/02/03
 
Old 01-03-2003, 09:29 PM   #4
speck
Member
 
Registered: Nov 2001
Location: US
Distribution: Slackware 14.2
Posts: 375

Rep: Reputation: 115Reputation: 115
Great! That --date has made life easier when I had to modify someone elses shell script. Although nothing is greater than the all powerful "Date::Calc" perl module

Speck
 
  


Reply



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
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 07:42 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
Does "ac97 & RH8" imply "buy a Sound Blaster Live"? nicktoop Linux - Hardware 3 02-13-2004 08:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:36 AM.

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
Open Source Consulting | Domain Registration