The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Batch delete specific folder from user home directories nipodrom Shell Programming and Scripting 2 05-09-2008 06:22 AM
how to archive logs older than 5 days & then delete them? timus1980 UNIX for Advanced & Expert Users 1 02-08-2008 06:53 AM
Delete logs every 3 hours tungaw2004 UNIX for Dummies Questions & Answers 3 12-09-2007 08:22 AM
Delete old logs chiru_h Shell Programming and Scripting 4 09-14-2007 06:33 PM
I need to delete the content out of a number of logs Jbolin01 Shell Programming and Scripting 4 09-30-2003 09:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 01-22-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Script to delete logs or take backups under specific user

I have to write a shell script like this--

1) Utility will be run under the directory owner.
2) This utility will clean files in ABC/logs. And following logs will be backed up or deleted.

Dispatcher Logs
Middle tier Logs
Sage log
Sage monitor log
Sage db clean up result log
Core files

3) mt_clean -a<action> [-t <time>]* -L <backup location>

-a<action> - has two values "delete" or "backup”. If not specified by default "delete" action will*occur. If*"backup" option is given, files will be backed up on the location specified by the -L, -L is mandatory if back up option is specified.

-t<time>* - The time is given with the -t option and its default is 1am
A time of 'now' means to execute the cleanup operation now
A time of 'never' means that the cleanup operation should not be scheduled and any existing cleanup should be cancelled.

-L <backup location> -*this field is mandatory if backup action is selected.
Note: As middle tier process are running and not stopped while this utility is running, the utility should create an empty*file with same name which has deleted or backed up. Otherwise middle tier logs will not be generated until the middle tier process is restarted. Any core files will be backed up or deleted according to the action chosen.*

Note-> mt_clean is the utility name.it can be run like this.

mt_clean -a delete/backup now somelocation.
it will take the backup or delete the log files now only or if backup needed it will do it in backuplocation.
If i can clarify any doubt please let me know.
i will appericiateur help.

I have to use getopts in this,i am a newbie in shell scripting and learning it so would appericiate your help.
Experts help me in this as i have to do it urgently.

Last edited by namishtiwari; 01-23-2008 at 05:28 AM..
  #2 (permalink)  
Old 01-23-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Experts can you have a look on this script also so that i can get some idea that will be a great help.If anyone has any doubt i am ready to clarify that but need help in this matter.
  #3 (permalink)  
Old 01-23-2008
adderek adderek is offline
Registered User
  
 

Join Date: Sep 2007
Location: Poland
Posts: 110
man chmod - see sticky bit
google suid
man find
man (language you would like to write the script in)

You should not expect that someone will do it for you (but maybe someoe would...).

Regards
  #4 (permalink)  
Old 01-23-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
I have wrote the script but that is not working properly for me..

Code:
#Get the INTAS release number
RELEASE=${PATH##*/OCU_}
RELEASE=${RELEASE%%/bin:*}

DEBUGGING=''
WhenToRun="01:00"

FilesToBackup='*.track* *.xml *.vm* *.gz Trace* TRACE* *.core *.out fcif_data_* esi_error_* *.rollback *.sed R.* APStatus_*log* *.output* send_mail* downenv* check_env* intaspurge_db_* sqlnet.log *.rpt *.html *.csv *.log*'
FilesToDelete='*.log* *_log*'
FileLocation='$INTAS_INSTALL_DIR/envsw/logs'

PID_Templates='Trace\([0-9][0-9][0-9][0-9][0-9]\)_.* TRACE_\([0-9][0-9][0-9][0-9][0-9]\)_.* .*_\([0-9][0-9]*\).out'
Progname=$(basename $0)

Usage="Usage:$Progname"'[-a <delete or backup] [-t <time>] [-l|-L <backup location>]' 
     IsCronJob=0
if [[ "$1" = "cron" ]]; then
	. ~/.kshrc
	IsCronJob=1
	if [[ -z "$INTAS_INSTALL_DIR" ]]; then
		echo "You must be logged in as the application owner to delete the middle tier logs"
		return 1
        fi	
fi

BackupLocation="$INTAS_INSTALL_DIR/mt_backup"
TEMPDIR=/tmp
if [[ -d $INTAS_INSTALL_DIR/envsw/tmp ]]; then
	TEMPDIR=$INTAS_INSTALL_DIR/envsw/tmp
fi

mt_clean_errfile="$INTAS_INSTALL_DIR/envsw/logs/mt_clean.$(date +%a)"

cfile=$(/bin/ls -1t $(find $INTAS_INSTALL_DIR -follow -name 'CommonScriptSetup.ksh') | head -1)

if [[ -x $cfile ]]; then
	export PATH=$PATH:$(dirname $cfile)
	. $cfile
else
	echo "Unable to find common include file needed for execution"
        if [[ $IsCronJob -eq 1 ]]; then
	  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
	fi
	return 1
fi
   
while getopts a:t:l:L:d optvar
do
	case $optvar in

	a)option="$OPTARG"
            echo "Delete or Backup option"
            if [[ $option = "delete" ]]; then
              for files in $FileLocation
               do
                touch $files $files.$(date +%a)
                cd /intasmut2/envsw/logs
                rm -f $FilesToDelete
                echo " Middletier is cleaned up" 
                done
            fi
             if [[ $option = "backup" ]]; then
             cd /intasmut2/envsw/logs
             mkdir -p $BackupLocation >/dev/null 2>&1
             cp $FilesToDelete $BackupLocation 
             fi
             ;; 
	t) WhenToRun="$OPTARG"
		if [[ $WhenToRun = "never" ]]; then
			WhenToRun='24:00'
		fi
		if [[ $WhenToRun = "now" ]]; then
			IsCronJob=1
			WhenToRun='24:00'
		fi
		if /bin/echo $WhenToRun | egrep '^[0-9]{1,2}:[0-5][0-9]$' >/dev/null; then
		  : Time is correct format
		else
			echo "Incorrect time format, time must be in HH:MM format"
			return $E_INT_INVALID_ARGS
		fi
		;;
	l) BackupLocation="$OPTARG"
		if [[ ! -d $BackupLocation ]]; then
			echo "Non-existant directory specified"
        		if [[ $IsCronJob -eq 1 ]]; then
	  		  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
			fi
			return $E_INT_MISSING_DIR
		fi
		if [[ $BackupLocation != *backup ]]; then
			echo "Appending backup subdirectories"
			BackupLocation=$BackupLocation/backup
			mkdir -p $BackupLocation >/dev/null 2>&1
  		if [[ ! -d $BackupLocation ]]; then
    		echo "Unable to make backup directory: $BackupLocation"
        		if [[ $IsCronJob -eq 1 ]]; then
	  		  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
			fi
			return $E_INT_MISSING_DIR
  		fi
		fi
		l_flag=Y
		Llcron=l
		;;
	L) BackupLocation="$OPTARG"
		Parent=$(expr $BackupLocation : '\(.*\)/.*' \| $BackupLocation)
		if [[ ! -d $Parent ]]; then
			echo "Non-existant directory specified"
        		if [[ $IsCronJob -eq 1 ]]; then
	  		  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
			fi
			return $E_INT_MISSING_DIR
		fi
		if [[ $IsCronJob -eq 1 ]]; then
			BackupLocation=$BackupLocation$(date +%w)
			if [[ ! -d $BackupLocation ]]; then
				mkdir -p $BackupLocation >/dev/null 2>&1
	  		if [[ ! -d $BackupLocation ]]; then
	    		echo "Unable to make backup directory: $BackupLocation"
			  if [[ $IsCronJob -eq 1 ]]; then
	  		    SendIntasNotifyMail "Middletier error message" $mt_clean_errfile
			  fi
			  return $E_INT_MISSING_DIR
	  		fi
			fi
			fi
		L_flag=Y
		Llcron=L
		;;
	d) DEBUGGING='/bin/echo'
		;;
        
	*) echo $Usgae
	   return 1
		;;
	esac
done
(( optcount = OPTIND - 1 ))
shift $optcount

if [[ -z "$1" ]]; then
  echo $Usage
if [[ "$L_flag" = "Y" && "$l_flag" = "Y" ]]; then
	echo 'You can not specify both -l and -L options.'
	if [[ $IsCronJob -eq 1 ]]; then
	    SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
	fi
	return 1;
fi

if [[ ! -d $BackupLocation && -w $BackupLocation ]]; then
  echo 'The backup location does not exist or is not writable'
  if [[ $IsCronJob -eq 1 ]]; then
    SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
  fi
  return 1
fi

cd /
if whence $Progname >/dev/null; then
	: We found this program in the path
else
	echo "Unable to locate $Progname in the PATH, check the .kshrc for correctness"
        if [[ $IsCronJob -eq 1 ]]; then
    	  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
   	fi
	return $E_INT_NO_FILE
fi

arg1=$1
arg2=$2

cd $INTAS_INSTALL_DIR/envsw
if [[ $? != 0 ]]; then
	echo "Unable to cd to $INTAS_INSTALL_DIR/envsw, exiting..."
        if [[ $IsCronJob -eq 1 ]]; then
    	  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
   	fi
	return $E_INT_MISSING_DIR
fi

dirlist=""
for dir in $DirCleanupList
do
	if [[ -d $dir ]]; then
		dirlist="$dirlist $dir"
	fi
done
DirCleanupList=$dirlist

if [[ -z "$DirCleanupList" ]]; then
	echo "No directories to clean up"
        if [[ $IsCronJob -eq 1 ]]; then
    	  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
   	fi
	return 1
fi

echo "Start middletierclean at $(date)"

echo "Cleaning the following dirs:"
echo "$DirCleanupList" | perl -ne 'print join("\n", split(" ")) . "\n"; '

if [[ $IsCronJob -ne 1 ]]; then
  # We are not a cron job, install a line into the crontab
	if (crontab -l 2>&1 1>/dev/null | grep 'not authorized'); then
		echo "Unable to use cron, contact your system administrator"
		echo "Check that $(whoami) has an entry in cron.allow"
		return 1
	fi
	cronfile="$TEMPDIR/cur_crontab$$"
	crontab -l 2>&1 | grep -v '^crontab' >$cronfile
	if grep "$Progname cron .* $arg1 $arg2" $cronfile >/dev/null; then
		tmpvar=$(grep -v "$Progname cron.*$arg1 $arg2" $cronfile)
		echo "$tmpvar" >$cronfile
	fi
	if [[ $WhenToRun != 24:00 ]]; then
		Llcron=l
		minute=$(/bin/echo $WhenToRun | cut -d: -f2)
		hour=$(/bin/echo $WhenToRun | cut -d: -f1)
		if [[ -z "$DEBUGGING" ]]; then
			echo "$minute $hour" '* * *' ". ~/.kshrc; $(whence $Progname) cron -e $FileLocation -$Llcron $BackupLocation $arg1 $arg2" >>$cronfile
		else
			echo "$minute $hour" '* * *' ". ~/.kshrc; $(whence $Progname) cron -d -e $FileLocation -$Llcron $BackupLocation $arg1 $arg2" >>$cronfile
		fi
	fi
	crontab $cronfile >/dev/null 2>&1
	if crontab -l | diff - $cronfile >/dev/null; then
		if [[ $WhenToRun != 24:00 ]]; then
			echo crontab has been installed correctly
		else
			echo The crontab entry for \"$arg1 $arg2\" has been removed
		fi
	else
	  echo "Error installing crontab file"
	fi
	/bin/rm -f $cronfile
  
echo "End middletierclean at $(date)"
return $E_NO_ERROR
Here if i am taking a backup option then i need to take it from the command line like

mt_clean -a backup now -L some location

but that is not done by this.I am trying to debug it but finding it difficult as some of the portion is preexisting in this.
I need the experts help to solve it.
if it can be done in a diffrent way that will also be fine.
so give me some ideas in this.
  #5 (permalink)  
Old 01-23-2008
ennstate ennstate is offline
Registered User
  
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
which shell do you use? If its korn shell try enabling debugging and see it what happens.
Code:
 ksh -x yourScript
Thanks
Nagarajan G
  #6 (permalink)  
Old 01-24-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
The problem here is related to the backuplocation,when i am specifying the backuplocation its not taking that location.delete part is working fine.
if option backup is specified it should go to that directory if already existing and take backup else craetr a directory and take the backup.
I need help in this regard.
Thanks in advance.
  #7 (permalink)  
Old 01-24-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
i have done some changes in the script,the problem here is it not picking up the directory.Kindly let me know the things i am doing is right or a better apporoach can be taken.I have a bit change in the backup option.whole script can be referred above.

Code:
if [[ $option = "backup" ]]; then
             BackupLocation="$OPTARG"
             echo "Specify the backup Location"
                if [[ $BackupLocation != *backup ]]; then
                        echo "Appending backup subdirectories"
                        BackupLocation=$BackupLocation/backup
                        mkdir -p $BackupLocation >/dev/null 2>&1
                if [[ ! -d $BackupLocation ]]; then
                echo "Unable to make backup directory: $BackupLocation"
                        if [[ $IsCronJob -eq 1 ]]; then
                          SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
                        fi
                        return $E_INT_MISSING_DIR
                fi
                else
                Parent=$(expr $BackupLocation : '\(.*\)/.*' \| $BackupLocation)
                if [[ ! -d $Parent ]]; then
                        echo "Non-existant directory specified"
                        if [[ $IsCronJob -eq 1 ]]; then
                          SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
                        fi
                        return $E_INT_MISSING_DIR
                fi
                if [[ $IsCronJob -eq 1 ]]; then
                        BackupLocation=$BackupLocation$(date +%w)
                        if [[ ! -d $BackupLocation ]]; then
                                mkdir -p $BackupLocation >/dev/null 2>&1
                        if [[ ! -d $BackupLocation ]]; then
                        echo "Unable to make backup directory: $BackupLocation"
                          if [[ $IsCronJob -eq 1 ]]; then
                            SendIntasNotifyMail "Middletier error message" $mt_clean_errfile
                          fi
                          return $E_INT_MISSING_DIR
                        fi
                        fi
                        fi
                fi
                l_flag=Y
                Llcron=l
            fi
             ;;
the script can be run like this----

mt_clean -a backup somelocation now

the problem coming is->Non-existant directory specified
it is taking the directory path or when i am specifying it is not creating that.so need your help in this regard.
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0