Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select and move files using a script 1

Status
Not open for further replies.

terrassa5

IS-IT--Management
Feb 22, 2005
40
GB
I need to create a script for Unix but I have no idea about scripting language, so I need a lot of help.

We have an Oracle database that generates archivelog files in folder /oracle/arch. This files have name arch_nnnnnn.dbf where nnnnnn is a consecutive number (i.e.: arch_435623.dbf). When this folder is 80% full we move first 100 files to folder /temp/arch, but we must first delete first 100 files of folder /temp/arch to have enough space for the new files.

I need a script that checks when folder /oracle/arch is full at 80% (it's mounted on a filesystem, so I check it manually using bdf command) and then makes these movings:

- delete first 100 files of folder /temp/arch
- move first 100 files from /oracle/arch to /temp/arch

Can you help me with this script?

Thank you very much
 
man awk (for parsing the bdf's output)
man ls, man head and man xargs

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I just love the way PHV points out the solution without actually providing it. Star awarded.

-------------------------
The trouble with doing something right the first time is that nobody appreciates how difficult it was - Steven Wright
 
Have you thought of just using RMAN if it's available.

If your going down the scripting route you need to be aware of potential gotta's what if the archive file your trying to move is still being written to Checkpoints, switchlogs, shutdowns, etc. can alter the standard size dramatically.

You could wait until another file shows up and assume that the prior file is finished. But that's not necessarily true either. At peak loads, you could be writing multiple of them at a time, finishing some later files before the first ones are done.

When an archive file is completed it's written in the Oracle database alert log.

You could write a program to watch that alert log and then move the file.

Mike

Unix *is* user friendly. It's just selective about who its friends are.
 
For MRN

This is not a problem for me. In /oracle/arch there are about 700 files and I move only 100 oldest each time, so I'm completely sure they are not in use.

I'm already doing this movement manually 2 or 3 times per day, and I just like to automatize it.

For PHV

Tomorrow in the office I will read man docs you say, but I know nothing about scripting and I'm not sure if I will be able of doing the script by myself.

Thanks
 
Here is a script I wrote only recently to do something similar, it may give you some ideas:

Code:
#!/bin/ksh
#
# Remove archive logs until % used is below THRESHOLD

ARCHIVE_DIR=/some/archive/dir
THRESHOLD=80

if [[ ! -d ${ARCHIVE_DIR} ]]
then
        print -u2 "${ARCHIVE_DIR} does not exist."
        exit 1
fi

# Get the filesystem usage from 'df' and strip out the %.
USAGE=$( df -Pk ${ARCHIVE_DIR} | awk '/^\// {sub("%","",$5);print $5}')

while [[ "${USAGE}" -gt "${THRESHOLD}" ]]
do
        OLDESTLOG=$(ls -rt ${ARCHIVE_DIR}/*.arc | head -1)

        print "Filesystem usage is ${USAGE}%, removing ${OLDESTLOG}"
        rm ${OLDESTLOG}

        # Give the OS some time to free up the filesystem space.
        sleep 15

        USAGE=$( df -Pk ${ARCHIVE_DIR} | awk '/^\// {sub("%","",$5);print $5}')
done

Annihilannic.
 
A very simple script based on number of files in /oracle/arch

Code:
#!/usr/ksh

ls -Alt /oracle/arch | tail -100 | awk '{print $9}' > /tmp/move_archive_files

a=`cat /tmp/move_archive_files`

count=`ls -Al /oracle/arch | wc -l | awk '{print $1}`

if [ $count -le "800" ];then

  for i in $a ; do
    mv /oracle/arch/$i /temp/arch
  done
fi

Or based on percent free

Code:
#!/usr/ksh

ls -Alt /oracle/arch | tail -100 | awk '{print $9}' > /tmp/move_archive_files

a=`cat /tmp/move_archive_files`

DISK=`df -k /oracle/arch | grep / | awk '{print $4}' | sed -e 's/\%//'`

if [ $DISK -le "80" ];then

  for i in $a ; do
    mv /oracle/arch/$i /temp/arch
  done
fi

Mike

Unix *is* user friendly. It's just selective about who its friends are.
 
Sorry this line

if [ $count -le "800" ];then

should read

if [ $count -ge "800" ];then



-le = less or equal to

-ge = greater than or equal to

Mike

Unix *is* user friendly. It's just selective about who its friends are.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top