Scenario:
We have a job which produces/creates a file F_Output_File.txt in output directory.
Archive Directory :/kwdev/OutputFile/Archive
#--*<<<<<<<<<<<<<<<<<<<<<START OF SCRIPT>>>>>>>>>>>>>>>>>>>>>>>>>>
#!bin/sh
#------------------------
#--* Variable Declaration
#------------------------
V_Output_Dir="/kwdev/OutputFile"
V_Archive_Dir="/kwdev/OutputFile/Archive"
V_Number_Of_Days="20"
V_Today_Date=`date "+%d-%m-%Y"`
V_File_Name="F_Output_File.txt"
V_Log_File="F_Archive_Maintenance.log"
#******************************************************************
#--* As we want to keep the file in output directory, we will copy
#--* it in archive directory. Below code will copy the output file
#--* in archive directory by appending file name with Today's date.
#******************************************************************
$ cp ${V_Output_Dir}/${V_File_Name} ${V_Archive_Dir}/${V_File_Name}"_"${V_Today_Date}
#******************************************************************
#We might have used mv command as well in place of cp. In case of #mv file will not be available in output directory, it will #only #available in Archive. In case of cp it would be available in #both #output as well as in Archive. It depends on requirement whether you #want to use mv or cp.
#------------------------------------------------------------------
#--* mv ${V_Output_Dir}/${V_File_Name} ${V_Archive_Dir}/${V_File_Name}"_"${V_Today_Date}
#******************************************************************
if [ $? -eq 0 ]
then
#------------------------------------------------------------------
#--* $? will check whether cp command executed successfully or not.
#--* $? = 0, Success
#--* $? != 0, Failure
#--* Note: $? checks the status of previous executed command(cp).
#------------------------------------------------------------------
echo "File Copied In Archive Directory" > ${V_Log_File}
else
echo "Error..!!File Has Not Copied In Archive Directory" > ${V_Log_File}
fi
#------------------------------------------------------------------
#--* Below command will use for maintenance of Archive Directory:
#--* Here we are finding all the objects of file type which has not #--* Accessed since last 20 days and then removing those files from # archive directory.
#------------------------------------------------------------------
find ${V_Archive_Dir} -type f -atime +${V_Number_Of_Days} -exec rm -f {} \;
if [ $? -eq 0 ]
then
#------------------------------------------------------------------
#--* $? will check whether find command successfully or not.
#--* $? = 0, Success
#--* $? != 0, Failure
#--* Note: $? Checks the status of previous executed command (find)
#------------------------------------------------------------------
echo "Files older than 20 Days have been removed from Archive" >> ${V_Log_File}
else
echo "Error..!!Something Went Wrong While Performing Maintenance." >> ${V_Log_File}
fi
#--*<<<<<<<<<<<<<<<<<<END OF SCRIPT>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Let see whether we have file available in output directory or not:
$ ls -ltr /kwdev/OutputFile
-rw-r--r-- 1 baba None 0 Jan 17 12:36 F_Output_File.txt
We have output file available in output directory, let see we have file available in archive directory or not?
$ ls -ltr /kwdev/OutputFile/Archive
total 0
before the execution of the script, nothing is available in archive directory (see the above command). Lets execute the shell script:
$ sh Archive_Maintenance.sh
Lets check whether file is copied to archive directory or not.
$ ls -ltr /kwdev/OutputFile/Archive
-rw-r--r-- 1 baba None 0 Jan 17 13:36 F_Output_File.txt_18-01-2016
$ cat F_Archive_Maintenance.log
Output: Will display the content of the log file.
File Copied In Archive Directory
Files older than 20 Days has been removed from Archive
Conclusion:
Keep Reading, Keep Learning, Keep Sharing....!!!
We have a job which produces/creates a file F_Output_File.txt in output directory.
- Archive(Copy) : You want to keep a COPY of this file in same directory and another in archiving directory:
- Archive(Move) : After some processing, you want to MOVE the file in archiving directory.
- Maintenance: You want to remove all the files which are not accessed since last 20 days in archive directory.
Archive Directory :/kwdev/OutputFile/Archive
#--*<<<<<<<<<<<<<<<<<<<<<START OF SCRIPT>>>>>>>>>>>>>>>>>>>>>>>>>>
#!bin/sh
#------------------------
#--* Variable Declaration
#------------------------
V_Output_Dir="/kwdev/OutputFile"
V_Archive_Dir="/kwdev/OutputFile/Archive"
V_Number_Of_Days="20"
V_Today_Date=`date "+%d-%m-%Y"`
V_File_Name="F_Output_File.txt"
V_Log_File="F_Archive_Maintenance.log"
#******************************************************************
#--* As we want to keep the file in output directory, we will copy
#--* it in archive directory. Below code will copy the output file
#--* in archive directory by appending file name with Today's date.
#******************************************************************
$ cp ${V_Output_Dir}/${V_File_Name} ${V_Archive_Dir}/${V_File_Name}"_"${V_Today_Date}
#******************************************************************
#We might have used mv command as well in place of cp. In case of #mv file will not be available in output directory, it will #only #available in Archive. In case of cp it would be available in #both #output as well as in Archive. It depends on requirement whether you #want to use mv or cp.
#------------------------------------------------------------------
#--* mv ${V_Output_Dir}/${V_File_Name} ${V_Archive_Dir}/${V_File_Name}"_"${V_Today_Date}
#******************************************************************
if [ $? -eq 0 ]
then
#------------------------------------------------------------------
#--* $? will check whether cp command executed successfully or not.
#--* $? = 0, Success
#--* $? != 0, Failure
#--* Note: $? checks the status of previous executed command(cp).
#------------------------------------------------------------------
echo "File Copied In Archive Directory" > ${V_Log_File}
else
echo "Error..!!File Has Not Copied In Archive Directory" > ${V_Log_File}
fi
#------------------------------------------------------------------
#--* Below command will use for maintenance of Archive Directory:
#--* Here we are finding all the objects of file type which has not #--* Accessed since last 20 days and then removing those files from # archive directory.
#------------------------------------------------------------------
find ${V_Archive_Dir} -type f -atime +${V_Number_Of_Days} -exec rm -f {} \;
if [ $? -eq 0 ]
then
#------------------------------------------------------------------
#--* $? will check whether find command successfully or not.
#--* $? = 0, Success
#--* $? != 0, Failure
#--* Note: $? Checks the status of previous executed command (find)
#------------------------------------------------------------------
echo "Files older than 20 Days have been removed from Archive" >> ${V_Log_File}
else
echo "Error..!!Something Went Wrong While Performing Maintenance." >> ${V_Log_File}
fi
#--*<<<<<<<<<<<<<<<<<<END OF SCRIPT>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Let see whether we have file available in output directory or not:
$ ls -ltr /kwdev/OutputFile
-rw-r--r-- 1 baba None 0 Jan 17 12:36 F_Output_File.txt
We have output file available in output directory, let see we have file available in archive directory or not?
$ ls -ltr /kwdev/OutputFile/Archive
total 0
before the execution of the script, nothing is available in archive directory (see the above command). Lets execute the shell script:
$ sh Archive_Maintenance.sh
Lets check whether file is copied to archive directory or not.
$ ls -ltr /kwdev/OutputFile/Archive
-rw-r--r-- 1 baba None 0 Jan 17 13:36 F_Output_File.txt_18-01-2016
As we can see file has been copied in the archive directory with concatenation of date in file name. Find will not remove anything from the archive directory in this case as we have only one file, F_Output_File.txt_18-01-2016, that is not 20 days old.
$ cat F_Archive_Maintenance.log
Output: Will display the content of the log file.
File Copied In Archive Directory
Files older than 20 Days has been removed from Archive
Conclusion:
Archiving & Maintenance is something which is required very often and archiving files can be used in future analysis. Above code will help in implement Archiving & Maintenance functionality. If you are facing any issue in understanding of find, please go through with the following posts Advance Uses Of FIND Command or Find label.
Keep Reading, Keep Learning, Keep Sharing....!!!
No comments:
Post a Comment