Yesterday I wrote a script which can clean file locations by using a catalogfile. In this catalog file are locations defined with retention min size, etc.
I wrote this script for an environment where we have to clean over 60 location daily, so you can understand we wanted to automate it. Below you’ll find the script and an example of the catalog.
rm_file.sh:
#!/bin/sh
#
# [21-07-2009] rudi.heinen - Initial Creation
#
# Cleans and removes files which are defined in the catalogfile
# Global variables
base="`echo $(dirname $(which $0)) | rev | cut -d '/' -f1- | rev`"
#Help
function help {
echo "Usage `basename $0`"
echo ""
echo "Cleans and removes files."
echo " -c Clean folders defined in the catalog."
echo " -o Logfile when using the catalog"
echo " -f Clean file."
echo " -l Lines to save in file."
echo ""
echo "Example catalog file:"
echo " ;;;;"
echo " ex: /user_home/log;0;500;log;1"
echo " For comments begin line with #"
echo " When retention 0 then file is cleaned instead of removed."
echo ""
echo "`basename $0 -c` relies on a catalogfile!!"
echo " ex: `basename $0` -c (-o )"
echo "Or you can clean / remove a file, if lines are not defined, file is removed."
echo " ex: `basename $0` -f (-l )"
exit 1
}
#Clean / remove files
function cleanfile {
# $1 = File # $2 = Lines
if [ -z $2 ]; then
# Check with fuser if file is open
if [[ `/sbin/fuser -u $1` == "" ]]; then
rm $1
else
> $1
fi
else
tail -n$2 $1 1>/tmp/cleanfile.$$ 2>/dev/null
if [ $? == 0 ]; then
cat /tmp/cleanfile.$$ > $1
else
echo "Error cleaning file $1."
fi
test -f /tmp/cleanfile.$$ && rm /tmp/cleanfile.$$
fi
}
#Getfile from catalogfile function
function cleancatalog {
# $1 = catalogfile # $2 = logfile
if [ -z $2 ]; then
cleanlog="/dev/null"
else
test -f $2 && > $2
fi
if [[ -z $1 || -e $1 ]] ; then
for line in `cat $1 | grep -v "#"`; do
x=0
for l in `echo ${line} | tr ';', ' '`;do
cat[$x]=$l
((x=x+1))
done
files=`test -e ${cat[0]} && find ${cat[0]} -name "*${cat[3]}*" -size +${cat[2]}k -mtime +${cat[1]} -maxdepth ${cat[4]}`
for file in ${files}; do
if [[ "${path}" != "${file}" ]];then
if [[ ${cat[1]} == "0" ]];then
echo "- `ls -ltrh ${file}` will be cleaned" >> $2
cleanfile ${file} 8000
else
echo "* ${file} will be removed" >> $2
cleanfile ${file}
fi
fi
done
done
else
echo "Catalog file does not exists!"
echo -e "Create one.n"
help
fi
}
#Main script
while getopts "c:f:l:ho:" options; do
case $options in
h) help;;
c) catalog=$OPTARG;;
f) dfile=$OPTARG;;
l) rline=$OPTARG;;
o) log=$OPTARG;;
*) help;;
esac
done
if [ -z ${catalog} ]; then
if [ -z ${dfile} ]; then
help
else
cleanfile ${dfile} ${rline}
fi
else
cleancatalog ${catalog} ${log}
fi
Catalog:
#Catalog file for rm_file.sh #;;;; #Datapump locations /backup/data_pump;7;0;.dmp.gz;1 /backup/data_pump;7;0;.log;1 #Portal /oracle/portal/opmn/logs;7;1000;log;1 /oracle/portal/Apache/Apache/logs;7;0;log;1 /oracle/portal/j2ee/oc4j/logs;7;1000;log;2