Anda di halaman 1dari 3

visnit01@ldoh0010isphn01:/etl/dev/scripts> cat adaa_archive_purge.sh #!

/usr/bin/ksh set -x ##################################### # This script will purge files from archive directory # The following are the parameters # 1. Archive directory # 2. Processing directory( Will zip all files in this directory) # 3. Num of Days ( This is the number of days from current date that you want to store files).Files older than the resulting # date will be deleted. This is for purge) # 4. Job type( This will be appended to the log file name for differentiation # 5. Pattern ( File pattern to search for) # This script will first tar the files and then zip the tar file. If the zip fai ls , then the tar file # will be moved to archive. ###################################### # Created By:Nidhi Choudhary # Date : 11/22/2010 # Revision :1.0 #################################### # Initialize the command line arguments archive_dir=$1 processing_dir=$2 num_of_days=2 job_type=$4 pattern=$5 # Create Log file DATETIME=$(date "+%Y%m%d") LOGFILENAME="adaa_archive_purge_files_"$job_type".log."$DATETIME LOGFILE="$archive_dir/$LOGFILENAME" TIMESTAMP=$(date "+%Y%m%d%H%M") echo "************************************************************" echo "$TIMESTAMP: Starting shell script ... $SCRIPTNAME" >>$LOGFILE echo "************************************************************" if [ $# -ne 5 ] then echo " Script usage is sh adaa_archive_purge_files.sh archive_dir num_of_days Jo b_type pattern" >> $LOGFILE fi # Zip the files in the processing folder. These have already been processed zip_file_name="arc_"$job_type"."$TIMESTAMP".tar" # Go to the processing directory cd $processing_dir echo "In processing directory" >>$LOGFILE pwd >>$LOGFILE # Tar the files in the processing directory echo "Tar the files tar -cvf" >>$LOGFILE tar -cvf $zip_file_name $pattern if [ $? -ne 0 ]

then echo "Tar was unsuccessful">>$LOGFILE exit 1 fi echo "Tar successful">>$LOGFILE # Zip the Tar file created in previous step echo "Zipping the Tar file">>$LOGFILE gzip $zip_file_name if [ $? -ne 0 ] then echo "Unable to Zip files Move the tar file without zipping" >>$LOGFILE mv $zip_file_name $archive_dir else echo " Zipping files in processing directory was successfull" >>$LOGFILE mv $zip_file_name".gz" $archive_dir if [ $? -ne 0 ] then echo "Moving Zip file to archive dir was unsuccessful Please check directory permissions " >>$LOGFILE exit 1 else echo "Moving Zip file to archive dir was successful" >>$LOGFILE # Once the zip file is successfully created and moved to archive directory the f iles in processing directory need to be # deleted echo " Deleting files from Processing Directory:$processing_dir" >>$LOGFILE rm -f $processing_dir$pattern if [ $? -ne 0 ] then echo " Files couldnt be deleted from processing directory Please check per missions " >>$LOGFILE exit 1 fi fi fi if [ $? -ne 0 ] then echo "Unable to Move files to archive Please check " >>$LOGFILE exit 1 fi # Tar file moved successfully to archive directory. Now delete the files in the processing folder. # rm -f $processing_dir$pattern ########################## # Purge Section # This section will purge files older than given number of days ######################### # Go to archive dir

cd $archive_dir pwd >>$LOGFILE # Delete the files find $archive_dir -type f -mtime +$num_of_days -exec rm -f {} \; -print ################################ # Tar file moved successfully to archive directory. Now delete the files in the processing folder. ################################## rm -f $processing_dir$pattern if [ $? -ne 0 ] then echo "Files in the processing directory could not be delete Please check permis sions" >>$LOGFILE exit 1 else echo "Files in the processing directory have been deleted. Process complete" >> $LOGFILE exit 0 fi visnit01@ldoh0010isphn01:/etl/dev/scripts>

Anda mungkin juga menyukai