Script to delete or backup logs at a specific time and under specific user

Reply

Join Date: Aug 2007
Posts: 49
Reputation: larryperl is an unknown quantity at this point 
Solved Threads: 0
larryperl larryperl is offline Offline
Light Poster

Script to delete or backup logs at a specific time and under specific user

 
0
  #1
Jan 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 49
Reputation: larryperl is an unknown quantity at this point 
Solved Threads: 0
larryperl larryperl is offline Offline
Light Poster

Re: Script to delete or backup logs at a specific time and under specific user

 
0
  #2
Jan 24th, 2008
Here is the script which i am trying to write but its not creating the backuplocation.
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/ksh
  2. # Copyright (c) 2000 Telcordia Technologies, Inc.,
  3. # All Rights Reserved.
  4. #
  5. # Telcordia Confidential - Restricted Access
  6. #
  7. # This document contains proprietary information that shall be
  8. # distributed or routed only within Telcordia Technologies, Inc.,
  9. # and its authorized clients, except with written permission of
  10. # Telcordia
  11. #
  12. # @(#)mt_clean 107.2 01/18/08
  13. # created on 01/18/08 at 08:58:17 Namish Tiwari
  14.  
  15. #Get the INTAS release number
  16. RELEASE=${PATH##*/OCU_}
  17. RELEASE=${RELEASE%%/bin:*}
  18.  
  19. DEBUGGING=''
  20. WhenToRun="01:00"
  21.  
  22. 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*'
  23. FilesToDelete='*.log* *_log*'
  24. FileLocation='$INTAS_INSTALL_DIR/envsw/logs'
  25.  
  26. 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'
  27. Progname=$(basename $0)
  28.  
  29. Usage="Usage:$Progname"'[-a <delete or backup] [-t <time>] [-l|-L <backup location>]'
  30. IsCronJob=0
  31. if [[ "$1" = "cron" ]]; then
  32. . ~/.kshrc
  33. IsCronJob=1
  34. if [[ -z "$INTAS_INSTALL_DIR" ]]; then
  35. echo "You must be logged in as the application owner to delete the middle tier logs"
  36. return 1
  37. fi
  38. fi
  39.  
  40. BackupLocation="$INTAS_INSTALL_DIR/mt_backup"
  41. TEMPDIR=/tmp
  42. if [[ -d $INTAS_INSTALL_DIR/envsw/tmp ]]; then
  43. TEMPDIR=$INTAS_INSTALL_DIR/envsw/tmp
  44. fi
  45.  
  46. mt_clean_errfile="$INTAS_INSTALL_DIR/envsw/logs/mt_clean.$(date +%a)"
  47.  
  48. cfile=$(/bin/ls -1t $(find $INTAS_INSTALL_DIR -follow -name 'CommonScriptSetup.ksh') | head -1)
  49.  
  50. if [[ -x $cfile ]]; then
  51. export PATH=$PATH:$(dirname $cfile)
  52. . $cfile
  53. else
  54. echo "Unable to find common include file needed for execution"
  55. if [[ $IsCronJob -eq 1 ]]; then
  56. SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
  57. fi
  58. return 1
  59. fi
  60.  
  61. while getopts a:t:l:L:d optvar
  62. do
  63. case $optvar in
  64.  
  65. a)option="$OPTARG"
  66. echo "Delete or Backup option"
  67. if [[ $option = "delete" ]]; then
  68. for files in $FileLocation
  69. do
  70. touch $files $files.$(date +%a)
  71. cd $INTAS_INSTALL_DIR/envsw/logs
  72. rm -f $FilesToDelete
  73. echo " Middletier is cleaned up"
  74. done
  75. fi
  76. if [[ $option = "backup" ]]; then
  77. BackupLocation="$OPTARG"
  78. if [[ ! -d $BackupLocation ]]; then
  79. echo "Non-existant directory specified"
  80. if [[ $IsCronJob -eq 1 ]]; then
  81. SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
  82. fi
  83. return $E_INT_MISSING_DIR
  84. fi
  85. if [[ $BackupLocation != *backup ]]; then
  86. echo "Appending backup subdirectories"
  87. BackupLocation=$BackupLocation/backup
  88. mkdir -p $BackupLocation >/dev/null 2>&1
  89. if [[ ! -d $BackupLocation ]]; then
  90. echo "Unable to make backup directory: $BackupLocation"
  91. if [[ $IsCronJob -eq 1 ]]; then
  92. SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
  93. fi
  94. return $E_INT_MISSING_DIR
  95. fi
  96. else
  97. Parent=$(expr $BackupLocation : '\(.*\)/.*' \| $BackupLocation)
  98. if [[ ! -d $Parent ]]; then
  99. echo "Non-existant directory specified"
  100. if [[ $IsCronJob -eq 1 ]]; then
  101. SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
  102. fi
  103. return $E_INT_MISSING_DIR
  104. fi
  105. if [[ $IsCronJob -eq 1 ]]; then
  106. BackupLocation=$BackupLocation$(date +%w)
  107. if [[ ! -d $BackupLocation ]]; then
  108. mkdir -p $BackupLocation >/dev/null 2>&1
  109. if [[ ! -d $BackupLocation ]]; then
  110. echo "Unable to make backup directory: $BackupLocation"
  111. if [[ $IsCronJob -eq 1 ]]; then
  112. SendIntasNotifyMail "Middletier error message" $mt_clean_errfile
  113. fi
  114. return $E_INT_MISSING_DIR
  115. fi
  116. fi
  117. fi
  118. fi
  119. l_flag=Y
  120. Llcron=l
  121. # cd /intasmut2/envsw/logs
  122. #mkdir -p $BackupLocation >/dev/null 2>&1
  123. #cp $FilesToDelete $BackupLocation
  124. fi
  125. ;;
  126. t) WhenToRun="$OPTARG"
  127. if [[ $WhenToRun = "never" ]]; then
  128. WhenToRun='24:00'
  129. fi
  130. if [[ $WhenToRun = "now" ]]; then
  131. IsCronJob=1
  132. WhenToRun='24:00'
  133. fi
  134. if /bin/echo $WhenToRun | egrep '^[0-9]{1,2}:[0-5][0-9]$' >/dev/null; then
  135. : Time is correct format
  136. else
  137. echo "Incorrect time format, time must be in HH:MM format"
  138. return $E_INT_INVALID_ARGS
  139. fi
  140. ;;
  141. d) DEBUGGING='/bin/echo'
  142. ;;
  143.  
  144. *) echo $Usgae
  145. return 1
  146. ;;
  147. esac
  148. done
  149. (( optcount = OPTIND - 1 ))
  150. shift $optcount
  151.  
  152. if [[ -z "$1" ]]; then
  153. echo $Usage
  154. echo
  155. echo 'This command will schedule an INTAS cleanup at a certain time of the day'
  156. echo 'The time is given with the -t option and its default is 1am'
  157. echo "A time of 'now' means to execute the cleanup operation now"
  158. echo "A time of 'never' means that the cleanup operation should not be scheduled"
  159. echo "and any existing cleanup or the specified environments/centers should be cancelled"
  160. echo 'A backup location can be specified with the -l option. This is the directory'
  161. echo 'where files are moved that are older than 1 day. The default is '"$INTAS_INSTALL_DIR/backup."
  162. echo 'Instead of -l, you can use -L to specify a 7-day backup directory rotation.'
  163. echo
  164.  
  165. echo "Typical usage: $Progname -a <delete or backup> location 2:00 "
  166. echo "This will schedule backups at 2am each night, purge files more than 14 days old"
  167. return $E_INT_INVALID_ARGS
  168. fi
  169.  
  170. #Unzip the file under backup directory
  171. #/usr/contrib/bin/gunzip -r $BackupLocation
  172.  
  173. if [[ "$L_flag" = "Y" && "$l_flag" = "Y" ]]; then
  174. echo 'You can not specify both -l and -L options.'
  175. if [[ $IsCronJob -eq 1 ]]; then
  176. SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
  177. fi
  178. return 1;
  179. fi
  180.  
  181. if [[ ! -d $BackupLocation && -w $BackupLocation ]]; then
  182. echo 'The backup location does not exist or is not writable'
  183. if [[ $IsCronJob -eq 1 ]]; then
  184. SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
  185. fi
  186. return 1
  187. fi
  188.  
  189. cd /
  190. if whence $Progname >/dev/null; then
  191. : We found this program in the path
  192. else
  193. echo "Unable to locate $Progname in the PATH, check the .kshrc for correctness"
  194. if [[ $IsCronJob -eq 1 ]]; then
  195. SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
  196. fi
  197. return $E_INT_NO_FILE
  198. fi
  199.  
  200. arg1=$1
  201. arg2=$2
  202.  
  203. cd $INTAS_INSTALL_DIR/envsw
  204. if [[ $? != 0 ]]; then
  205. echo "Unable to cd to $INTAS_INSTALL_DIR/envsw, exiting..."
  206. if [[ $IsCronJob -eq 1 ]]; then
  207. SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
  208. fi
  209. return $E_INT_MISSING_DIR
  210. fi
  211.  
  212. dirlist=""
  213. for dir in $DirCleanupList
  214. do
  215. if [[ -d $dir ]]; then
  216. dirlist="$dirlist $dir"
  217. fi
  218. done
  219. DirCleanupList=$dirlist
  220.  
  221. if [[ -z "$DirCleanupList" ]]; then
  222. echo "No directories to clean up"
  223. if [[ $IsCronJob -eq 1 ]]; then
  224. SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
  225. fi
  226. return 1
  227. fi
  228.  
  229. echo "Start middletierclean at $(date)"
  230.  
  231. echo "Cleaning the following dirs:"
  232. echo "$DirCleanupList" | perl -ne 'print join("\n", split(" ")) . "\n"; '
  233.  
  234. if [[ $IsCronJob -ne 1 ]]; then
  235. # We are not a cron job, install a line into the crontab
  236. if (crontab -l 2>&1 1>/dev/null | grep 'not authorized'); then
  237. echo "Unable to use cron, contact your system administrator"
  238. echo "Check that $(whoami) has an entry in cron.allow"
  239. return 1
  240. fi
  241. cronfile="$TEMPDIR/cur_crontab$$"
  242. crontab -l 2>&1 | grep -v '^crontab' >$cronfile
  243. if grep "$Progname cron .* $arg1 $arg2" $cronfile >/dev/null; then
  244. tmpvar=$(grep -v "$Progname cron.*$arg1 $arg2" $cronfile)
  245. echo "$tmpvar" >$cronfile
  246. fi
  247. if [[ $WhenToRun != 24:00 ]]; then
  248. Llcron=l
  249. minute=$(/bin/echo $WhenToRun | cut -d: -f2)
  250. hour=$(/bin/echo $WhenToRun | cut -d: -f1)
  251. if [[ -z "$DEBUGGING" ]]; then
  252. echo "$minute $hour" '* * *' ". ~/.kshrc; $(whence $Progname) cron -e $FileLocation -$Llcron $BackupLocation $arg1 $arg2" >>$cronfile
  253. else
  254. echo "$minute $hour" '* * *' ". ~/.kshrc; $(whence $Progname) cron -d -e $FileLocation -$Llcron $BackupLocation $arg1 $arg2" >>$cronfile
  255. fi
  256. fi
  257. crontab $cronfile >/dev/null 2>&1
  258. if crontab -l | diff - $cronfile >/dev/null; then
  259. if [[ $WhenToRun != 24:00 ]]; then
  260. echo crontab has been installed correctly
  261. else
  262. echo The crontab entry for \"$arg1 $arg2\" has been removed
  263. fi
  264. else
  265. echo "Error installing crontab file"
  266. fi
  267. /bin/rm -f $cronfile
  268.  
  269. echo "End middletierclean at $(date)"
  270. return $E_NO_ERROR
  271.  

I need help in the script kindly help me out experts.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC