Wednesday, 13 July 2016

Auto Housekeep aged out files in Unix

How housekeep the aged out files in Unix platform?

For example, if you would like to keep the files within 2 days , to remove the aged out file per hour.

1. Prepare Shell Script.
filename: auto_housekeep_file.sh
housekeep files location: /data_01


v_date=`date +%d%m%y-%H-%M-%S`
v_host=host101
echo $v_date;
echo $v_host;
echo "executed: auto_housekeep_file.sh ";

log_file=/data_01/$v_date.$v_host.auto_housekeep_file.log
echo "Host: host101" >> $log_file
echo "Start Date/Time: " $(date) >> $log_file
echo "disk info" >> $log_file
echo "++++++++++++++++++++++++++++++++++++++++++++++++" >> $log_file
echo " data mount point - before remove the aged file " >> $log_file
echo "++++++++++++++++++++++++++++++++++++++++++++++++" >> $log_file
echo " " >> $log_file
df -k /data_01 >> $log_file
echo " " >> $log_file

echo "++++++++++++++++++++++++++++++++++++++++++++++++" >> $log_file
echo "List of all aged out files:" >> $log_file
echo "++++++++++++++++++++++++++++++++++++++++++++++++" >> $log_file
echo " " >> $log_file
find /data01_/*.* -type f -mtime +1 >> $log_file
echo " " >> $log_file
echo "Remove more than 2 days archive log backup now......" >> $log_file
echo " " >> $log_file
find /data_01/*.* -type f -mtime +1  -exec rm {} +>> $log_file
echo "++++++++++++++++++++++++++++++++++++++++++++++++" >> $log_file
echo "After removed, listed all file:                 " >> $log_file
echo "++++++++++++++++++++++++++++++++++++++++++++++++" >> $log_file
echo " " >> $log_file
find /data_01/* -type f >> $log_file
echo " " >> $log_file
echo " " >> $log_file
echo "End Date/Time: " $(date) >> $log_file
echo "disk info" >> $log_file
echo "++++++++++++++++++++++++++++++++++++++++++++++++" >> $log_file
echo " data mount point - after remove the aged file  " >> $log_file
echo "++++++++++++++++++++++++++++++++++++++++++++++++" >> $log_file
echo " " >> $log_file
df -k /data_01 >> $log_file
echo " " >> $log_file
echo "End of List " >> $log_file

2. set cron job
For example: Everyday between 08:00 - 22:00
cmd> crontab -e

# auto housekeep files
# per hour between 0800 - 2200 to housekeep the aged out files
00 8-22 * * * /data_01/auto_housekeep_file.sh

Enjoy! =)


No comments:

Post a Comment