Sponsored Content
Top Forums Shell Programming and Scripting Script to delete logs or take backups under specific user Post 302161008 by namishtiwari on Wednesday 23rd of January 2008 10:26:47 AM
Old 01-23-2008
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.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Batch delete specific folder from user home directories

Hi! Need your help. How can I delete the cache folder of multiple user home directories via automatically executed shell script on a Mac OS X Server? Example: The userdata are stored on a Xsan Volume like this: /Volumes/Xsan/userdata/mike /Volumes/Xsan/userdata/peter... (2 Replies)
Discussion started by: nipodrom
2 Replies

2. UNIX for Dummies Questions & Answers

Script to change/find/delete/install a specific file

Hi Very much a newbie to UNIX & scripting, but have identified an area within work that would benefit from being automated, as its repeated manually very often, and it looks like the ideal first script! What I need to do is change directory to a users home (cd ~), and then find and remove a... (6 Replies)
Discussion started by: Great Uncle Kip
6 Replies

3. Shell Programming and Scripting

Create a script that executes when a user attempts to delete history logs

Hi, I have a linux redhat 9 server and I am concerned about the security on that server. I would like to be able to write a script that records all the commands that were typed at the command prompt before the user calls the 'history -c' command and deletes all the history. I was thinking about... (4 Replies)
Discussion started by: mishkamima
4 Replies

4. Shell Programming and Scripting

How to automatically delete old backups

I'm creating nightly backups using cygwin bash with this one line script: zip -r "K:/backup/outlook/`date "+%y%m%d%M"`" C:/Documents\ and\ Settings/siegfried/Local\ Settings/Application\ Data/Microsoft/Outlook How can I enhance my script using the "rm" command so I never have more than 4 zip... (2 Replies)
Discussion started by: siegfried
2 Replies

5. Shell Programming and Scripting

Need to run a bash script that logs on as a non-root user and runs script as root

So I have a script that runs as a non-root user, lets say the username is 'xymon' . This script needs to log on to a remote system as a non-root user also and call up a bash script that runs another bash script as root. in short: user xymon on system A needs to run a file as root user and have... (2 Replies)
Discussion started by: damang111
2 Replies

6. Shell Programming and Scripting

How to make a script that logs bandwidth on a specific port?

I have a script that runs right before a daily reboot that captures the amount of MB transmitted up and pulled down since last reboot. echo `date +"%m-%d-%y"``grep eth0 /proc/net/dev | awk '{print ","$2/1024/1024","$10/1024/1024}'`>>/home/nick/bandwidth.logIn looking at the raw output of... (1 Reply)
Discussion started by: nbsparks
1 Replies

7. Shell Programming and Scripting

Script to append user details as soon as he logs in

I'm wishing to create a log myself where I wish to: 1.Write a script in /bin folder to make a separate log in var/log folder & 2.Call the script in bashrc each time a user logs in, I'm trying to avoid CRONJOB as it would take too much of my memory after iterations. So I just need to append the... (2 Replies)
Discussion started by: Lionking93
2 Replies

8. Shell Programming and Scripting

Script to compress and delete the older logs

Hello Guys, Can you please help me with a script which zips the older log files(1-2 weeks) and delete them? I want to run the script manually instead of setting it up in a cron job. Appreciate your help. Regards, Kris (6 Replies)
Discussion started by: kriss.gv
6 Replies

9. Shell Programming and Scripting

Shell script to delete empty files from specific locations

Hi, I need help in regard to developing a shell script to delete empty files from multiple specific locations. The directory paths will be stored in a text file. So the requirement is to read the text file for one specific path and then remove empty files from that particular path. Looping through... (4 Replies)
Discussion started by: Khan28
4 Replies

10. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies
All times are GMT -4. The time now is 09:04 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy