Friday, 25 January 2013

Sample UNIX Shell script to Archive and Delete Data files

Every Data-warehousing or OLTP systems procure data from flat files.
Storing, archiving and Purging these files is necessary to maintain free space on your file-system.

Below is a sample script which will help you in these activities:


$> vi ArchiveScript.ksh

Then in the vi-editor enter the below mentioned script:

#! /bin/ksh

#Set Local variables

Date1=`date +%Y%m%d`
echo "$Date1 is Current Date"

#User-defined Function to Display error messages
ErrorCapture()
{
if [ $1 -ne 0 ]
then
echo "$2"
exit $1
else
exit 0
fi
}


#Delete users temporary files

if [ -s /tmp/users.tmp1 ]
then
echo " Delete Temporary files"
rm /tmp/users.tmp1
RC=$?
ErrorCapture $RC "Temporary file removal Failed "
else
echo "Temp file not found"
fi

# Archive users data file

if [  -s /fs/fs01/data/users ]
then
echo "Rename file with datestamp"
if [ -d /fs/fs01/data/SYWmbrVisitArch ]
then
mv /fs/fs01/data/users /fs/fs01/data/SYWmbrVisitArch/users_$Date1
else
echo "Creating directory /fs/fs01/data/SYWmbrVisitArch"
mkdir /fs/fs01/data/SYWmbrVisitArch
mv /fs/fs01/data/users /fs/fs01/data/SYWmbrVisitArch/users_$Date1
fi
RC=$?
ErrorCapture $RC "Renaming User Failed"
else
echo "File users not found in /fs/fs01/data"
fi

#Purge files older than 7 days

find /fs/fs01/data/SYWmbrVisitArch -type f -mtime +7 -exec rm {} \;
RC=$?
ErrorCapture $RC "Purge of datafile users older than 7 days Failed"

exit 0


Step 3 - Save by pressing 'Esc' + ":" (colon) + "wq!"


Let us know if this was helpful. Subscribe using the button on the right !!

No comments:

Post a Comment

Please share your thoughts and let us know the topics you want covered