Script to archive log files:Urgent Help required


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to archive log files:Urgent Help required
# 1  
Old 07-30-2008
Script to archive log files:Urgent Help required

I have no prior knowledge of Unix shell scripting,but my requriment demands to wrie a script that do fallowing things.
1.Delete older than one year log files
2.Moves files in to the directories as YYYYMM wise.
3.If files in $LOGDIR older than n=2 months tar and move them to $ARCHIVEDIR
4.Removing n=4 months older files from Archive directory

I write a script in ksh .But am getting lot of errors.Can any one help me out.My code is as fallows:

code:log_cln.sh

#!/bin/ksh
#*******************************************************************************
#** Program : log_clean.sh
#**
#** Job Name :
#**
#** Original Author :
#**
#** Description : Script used to archive and clean up files
#**
#** Revision History: Please do not stray from the example provided.
#**
#**
#*****************************************************************************
#test hook
. ~/.setup_env
. $FPATH/common_funcs.sh
#Define varibles used
L_SCRIPTNAME=`basename $0`
curr_year=`date +%Y`
curr_month=`date +%m`
curr_yearmm=`date +%Y%m`
#file_year
#file_mm
#file_yearmm ****variables to hold the file year,mm,yearmm values********
#last_n_yearmm

#
while getopts "s:t:i:d:f" option
do
case $option in
s) start_step=$OPTARG;;
t) data_tablespace=$OPTARG;;
i) index_tablespace=$OPTARG;;
d) debug=1;;
f) date_string=$OPTARG;;
esac
done
shift $(($OPTIND - 1))
#-----------------------------------------------------------------
# Set the default values for all options. This will only set the
# variables which were NOT previously set in the getopts section.
#-----------------------------------------------------------------
debug=${debug:=0}
#-----------------------------------------------------------------
#Check for debug mode [-d]
#-----------------------------------------------------------------
if [ $debug -eq 1 ]; then
set -x
fi
#-----------------------------------------------------------------
# Set $ parameters here.
#-----------------------------------------------------------------

#-------------------------------------------------------------------
#function which takes file's year, month name and retrieve yearmonth
value
#----------------------------------------------------------------------
function get_file_yearmm{ # inputs : file year and month name
#output :files yyyymm
year=$1
mon_name=$2
case $year in
?????)file_year=year;; #for year like 2007 ,wc is 5
*)file_year=curr_year ;; #for current year itz showing something like 11:45 ,wc 6.
esac

case "$mon_name" in
"Jan")file_mm=01;;
"Feb")file_mm=02;;
"Mar")file_mm=03;;
"Apr")file_mm=04;;
"May")file_mm=05;;
"Jun")file_mm=06;;
"Jul")file_mm=07;;
"Aug")file_mm=08;;
"Sep")file_mm=09;;
"Oct")file_mm=10;;
"Nov")file_mm=11;;
"Dec")file_mm=12;;
*) echo " Invalid month" ;;
esac
file_yearmm=${file_year}$file_mm
}
#-------------------------------------------------------------------------------
#function which gives the last nth month yyyymm value from current date
#-------------------------------------------------------------------------------
function get_last_n_yearmm{
yyyy=curr_year
mm=curr_month
n=$1
while [ n -gt 0 ]
do
if [ mm -eq 01 ];then
yyyy=`expr $yyyy - 1`
mm=12
last_n_yearmm=${yyyy}$mm
else
mm=`printf %02d $(expr $mm - 1)`
last_n_yearmm=${yyyy}$mm
fi
n=`expr $n - 1`
done
}
#----------------------------------------------------------------
Function to check the return status and set the appropriate
# message
#-----------------------------------------------------------------
function check_status
{
if [ $? -ne 0 ]; then
err_msg="$L_SCRIPTNAME Errored at Step: $step_number"
echo "$err_msg"
subject_msg="Job Error - $L_SCRIPTNAME"
send_mail "$err_msg" "$subject_msg" "$ERR_LIST"
exit $step_number
fi
}
#-----------------------------------------------------------------
#Begin Main Program
#-----------------------------------------------------------------
check_variables LOGDIR ARCHIVEDIR
#-----------------------------------------------------------------
step_number=1
#Description: Delete older than one year log files
#
#-----------------------------------------------------------------
#
if [ $start_step -le $step_number ] ; then
echo "*** Step Number $step_number.................."
echo "****Deletiing the files older than 1 year....."
cd $LOGDIR
for $file in $LOGDIR
do
file_year=$(ls -l $file | awk '{ print $8 }' )
file_year=$(get_file_yearmm $file_year)
if [ file_year -le $(expr $curr_year -1 ];then
rm -rf $file
check_status
fi
done
fi

#------------------------------------------------------------------
#-----------------------------------------------------------------
step_number=2
#Description: Moves files in to the directories as YYYYMM wise
#-----------------------------------------------------------------
if [ $start_step -le $step_number ] ; then
echo "*** Step Number $step_number"
echo " Moves files in to the directories as YYYYMM wise"
for $file in $LOGDIR
do
file_year=$(ls -l $file | awk '{ print $8 }')
file_mm=$(ls -l $file | awk '{ print $6 }')
file_yearmm=$(get_file_yearmm $file_year $file_mm)
mv $file ./${file_yearmm}/$file
check_status
done
fi
#---------------------------------------------------------------------
#-----------------------------------------------------------------
step_number=3
#Description: If files in $LOGDIR older than n=2 months tar and move
them to $ARCHIVEDIR
#-----------------------------------------------------------------
if [ $start_step -le $step_number ] ; then
echo "*** Step Number $step_number"
echo " tar and moving files to archive directory"
n=2
last_n_yearmm=$(get_last_n_yearmm $n)
cd $LOGDIR
for $file in $LOGDIR
do
file_yearmm=$(ls -l $file |grep "^d"| awk '{ print $9 }')
if [ file_yearmm -le last_n_yearmm ];then
tar -cvf `find . "*.*" -type d `>${file_yearmm}.tar 2>/dev/null
check_status
fi
done
mv $LOGDIR/"*.tar" ${ARCHIVEDIR}
check_status
fi
#------------------------------------------------------------------------
step_number=4
#Description: untar all the files
#
#-------------------------------------------------------------------------
if [ $start_step -le $step_number ] ; then
echo "*** Step Number $step_number"
cd $ARCHIVEDIR
for $file in $ARCHIVEDIR
do
tar -xvf ${file_yearmm}.tar
check_status
done
fi
#--------------------------------------------------------------------------
step_number=5
#Description: Removing n=4 months older files from Archive directory
#--------------------------------------------------------------------------
if [ $start_step -le $step_number ] ; then
echo "*** Step Number $step_number"
echo "Removing n=4 months older files from Archive directory"
n=4
last_n_yearmm=$(get_last_n_yearmm $n)
for $file in $ARCHIVEDIR
do
file_yearmm=$(ls -l $file | awk '{ print $9 }')
if [ file_yearmm -le last_n_yearmm ];then
rm -rf $file
check_status
fi
done
fi
#---------------------------------------------------------------------------
step_number=6
#---------------------------------------
#Description:6 tar all
#---------------------------------------
if [ $start_step -le $step_number ] ; then
echo "*** Step Number $step_number"
cd $ARCHIVEDIR
for $file in $ARCHIVEDIR
do
file_yearmm=$(ls -l $file |grep "^d"| awk '{ print $9 }')

tar -cvf ${file_yearmm} ${file_year}.tar
check_status
done
fi
exit 0
#-----------------------------------------------------------------------------
# 2  
Old 07-30-2008
Post the errors.
# 3  
Old 07-30-2008
Hi era thanks for quick reply.am getting the error.
log_cln.sh[63]: Syntax error at line 66 : `year=$1' is not expected.
# 4  
Old 07-30-2008
You need a space before the opening bracket in function get_file_yearmm{

Is that it? You said a lot of errors. If there are more, please post all of them (after fixing what you can; you seem to have the same error with get_last_n_yearmm).
# 5  
Old 07-30-2008
Thanx era i fixed that error.
when use function syntax like this :
----------------------------------
function get_file_yearmm{
#code
}

-----------------------------------
Errors am getting related to the variables:
variable_name:unexpected

but using the syntax i over come those:
get_file_yearmm()
{
#code
}
--------------
Now up to function calls, code is working fine.I debug some errors.Now am getting error about $file like this :
log_cln.sh[140]: $file: is not an identifier
Am attatching my modified code.

Thanks for replying.
# 6  
Old 07-30-2008
The error message indicates line 140, but it seems to correspond with line 142 (and also on line 159) in your script: the syntax of the for statement is wrong. You should remove the dollar sign in front of the variable name; the correct syntax is

Code:
for file in $LOGDIR

Please remove the "urgent help" tag from this thread; tags are meant to help users of this site.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to Archive Files from Subdirectories

Hello, I have a CentOS server that contains a 'storage' directory. Within that directory, there could be any number of subfolders (all with unique names that match usernames). Under each username folder, there are two additional folders: db and files /STORAGE/user1/db/... (3 Replies)
Discussion started by: JasonH
3 Replies

2. Shell Programming and Scripting

Log archive script

I need a log archival script which will delete files older than 3 days in a given JBOSS log directory which has files as follows server.log.2011-08-25 server.log.2011-08-26 server.log.2011-08-27 server.log.2011-08-28 server.log I only want to save server.log and 3 days before server.log... (1 Reply)
Discussion started by: gubbu
1 Replies

3. Shell Programming and Scripting

Script invoked using "sh" is not executing. Urgent help required

Hi , I am new to shell scripting. I am using Linux for doing scripting. Below is my script, which takes 2 parameters as input. test.sh has the below: #!/bin/bash . $HOME/.profile gpg --yes --no-use-agent -r "$(eval echo \$$2_Var)" -e $1 1st parameter is command line... (7 Replies)
Discussion started by: rangarb
7 Replies

4. Shell Programming and Scripting

URGENT SCRIPT LOGIC required

Hello friends, It will be great if we found some way to check this our: we have some databases (teradata and oracle). our applications(in java on weblogic) are using connection pools of these databases. we have a pair of userID and password for every pool. If we have any logic to test... (2 Replies)
Discussion started by: NIMISH AGARWAL
2 Replies

5. Shell Programming and Scripting

Shell script interview question...help required urgent!!!

hi i have cancelled my previous post (2 Replies)
Discussion started by: choco4202002
2 Replies

6. HP-UX

Archive Script-Urgent

Hi All, How do I archive a script so that all the files are not sent when the script runs? Is the command ArchiveNx used. If yes then where is it added in the command line? E.g.: /home/projects/ftpToShrDrive {USERID} ${PASSWORD} ${REMOTEHOST} ${LOCALDIR} ${NAMEOFFILE} Here the ftpToShrDrive is... (1 Reply)
Discussion started by: indira
1 Replies

7. Shell Programming and Scripting

Archive Script-Urgent

Hi All, How do I archive a script so that all the files are not sent when the script runs? Is the command ArchiveNx used. If yes then where is it added in the command line? E.g.: /home/projects/ftpToShrDrive {USERID} ${PASSWORD} ${REMOTEHOST} ${LOCALDIR} ${NAMEOFFILE} Here the ftpToShrDrive is... (0 Replies)
Discussion started by: indira
0 Replies

8. Shell Programming and Scripting

Archive script old files

Hi All, Im trying to write a script to archive files based on the date the files were created. For example, if a group of files were created on 23rd August,I would have 230806.tar. I have a problem,I want the script to read a separately created file(readarchive.txt) to look for the path to... (1 Reply)
Discussion started by: kayarsenal
1 Replies

9. Shell Programming and Scripting

Urgent help required in deleting a line without opening a file usinga shell script

Hi, I need a help in deleting a line matching a particular pattern in a file using shell script without opening the file. The file is a .c/.cpp file. Is it possible? Thanks (6 Replies)
Discussion started by: naan
6 Replies

10. Shell Programming and Scripting

script to archive all the log files

is there a way to write a script and run with a cron job which archives all the *.log files into .tar.gz. :eek: (0 Replies)
Discussion started by: tintedwindow
0 Replies
Login or Register to Ask a Question