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
#-----------------------------------------------------------------------------
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)
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)
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)
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)
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)
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)
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)
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)