If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > script to remove archivelog files

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-29-10, 07:10
newdbaxchange newdbaxchange is offline
Registered User
 
Join Date: Jun 2010
Posts: 81
script to remove archivelog files

Hello,

I created a line in the crontab entries to remove all files and leave the last 7 days worth of data in the same directory. I thought I had it sussed until I checked today and to my horror there were no files and no current directory. What could be the issue with my syntax? When I edited the crontab - there was a directory 'archivelog' which also contained files similair to this:

$ cd /u03/app/oracle/arrepos/flash_recovery_area/ARREPOS/archivelog
$ ls -ltr

total 51
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_06
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_01
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_12
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_05
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_02
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_08
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_11
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_13
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_07
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_10
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_09
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_03
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_06_04
drwxr-x--- 2 oracle dba 2 Jun 21 11:10 2010_05_31
drwxr-x--- 2 oracle dba 2 Jun 24 04:29 2010_06_15
drwxr-x--- 2 oracle dba 2 Jun 24 04:29 2010_06_14
drwxr-x--- 2 oracle dba 2 Jun 25 04:29 2010_06_16
$

The line in crontab is as follows:-

# -- [Remove Archive Logs] ----
29 4 * * 1-5 find /u03/app/oracle/arrepos/flash_recovery_area/ARREPOS/archivelog -name '*' -mtime +7 -exec rm -Rf {} \;

$ cd /u03/app/oracle/arintdb/flash_recovery_area/ARINTDB
$ ls -ltr
ls -ltr
total 6
drwxr-x--- 2 oracle dba 2 Jun 16 08:48 onlinelog
Reply With Quote
  #2 (permalink)  
Old 06-29-10, 20:55
sco08y sco08y is offline
Registered User
 
Join Date: Oct 2002
Location: Baghdad, Iraq
Posts: 697
cron may not be calling your shell. echo $SHELL to find out your shell, and put SHELL=/bin/whatever in your first line.

A few notes:

-name '*': Why have this at all?

-mtime: read the fine print... it only matches if it is exactly 7 days old. Yeah, I have no idea when that would be useful, but there is a practical solution.

-exec rm -Rf {} \; : Doing a recursive deletion while find is recursively searching may screw up find, as in, it might be expecting those subdirectories to be there. I'd put -prune after it so find doesn't try to do that. It's also *so* easy to blow up your whole system, be careful.

find generally: Do you really need to search every file, or are you just trying to delete the directories right under archivelog? You can use -maxdepth 1 if you really want to use find, but don't need recursive searching.

Since -mtime isn't doing what you want, you could replace it with -not \( -mtime 7 -o mtime 6 -o -mtime 5 ... \)

But I'd suggest:

Code:
#!/bin/bash
lastweek=`date -v-7d '+%Y%m%d'`
for d in *
do if [ `stat -t '%Y%m%d' -f '%Sm' "$d"` -lt "$lastweek" ]
    then echo rm -rf "$d"
    fi
done
The echo command will simply show what it's trying to delete. This, incidentally, doesn't hunt through directories, making it a good bit safer.

The -v command to date tells it to report the date minus 7 days. You'll notice the %Y%m%d format string; that's using a standard library call to format so both date and stat are producing the same thing. By putting (4-digit) year first, month second, day last, you get a value that can be compared numerically.

The stat command gets information about a file, in this case, the formatted modification time.

If you wanted, alternately, to scan based on file name, this would work:


Code:
#!/bin/bash
lastweek=`date -v-7d '+%Y_%m_%d'`
for d in ????_??_??
do if [ "$d" < "$lastweek" ]
    then echo rm -rf "$d"
    fi
done
Here I'm using the ????_??_?? pattern to make sure I'm only matching entries that make sense. And the < does a string comparison, which also happens to work with dates formatted that way.
Reply With Quote
  #3 (permalink)  
Old 06-29-10, 21:53
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
You must have GNU date for the date calculations to work.
If the above syntax fails, see here for help GNU tar 1.23: 7. Date input formats
Reply With Quote
  #4 (permalink)  
Old 06-30-10, 03:42
newdbaxchange newdbaxchange is offline
Registered User
 
Join Date: Jun 2010
Posts: 81
Thanks for your reply - but that explanation was well over my head

The code you provided, do I need to create a script and include that?
Is that the entire script or do I add the location of where I need to
remove the archivelog files from?

Is it possible to word the syntax that will be required to remove files
only longer than 7 days old

many thanks, from the newbie who is always eager to learn
Reply With Quote
  #5 (permalink)  
Old 07-07-10, 03:40
newdbaxchange newdbaxchange is offline
Registered User
 
Join Date: Jun 2010
Posts: 81
Feedback please

Is there a solution to the request please

many thanks
Reply With Quote
  #6 (permalink)  
Old 07-07-10, 08:37
kitaman kitaman is offline
Papabi's friend
 
Join Date: Sep 2009
Location: Ontario
Posts: 629
There is an old Chinese expression: he who play in root eventually kill tree.

First of all, create a test environment in your home directory. Create the required directories, and fill them with test log files.
Use the 'touch' command to set the time and date of the last modification.
Write a script to list the files that are over 7 days old.
On my system the find command would be
find ./ -mtime +7 -print
When you are satisfied that you have listed the correct files, you can add the code to remove the files.
Part two.
Add a crontab entry for a test script to just save the output of the 'env' command to a log file in your home directory.
Compare this file to the output of the 'env' command from a regular terminal session for the same user id.
Adjust your log removal script accordingly (add PATH statement, use full path names, etc)
Test your cron job without actually removing files, just echo the rm command into a log file.
When you are satisfied that all this works, change it to remove the actual oracle log files.
Reply With Quote
  #7 (permalink)  
Old 07-15-10, 15:57
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool rman of fire

Quote:
Originally Posted by newdbaxchange View Post
Is there a solution to the request please

many thanks
Why not just use RMAN to backup and delete the archive logs? That way you don't have to worry about your shell script removing "other" files and/or directories.
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On