Linux Training

Linux training for private, public & voluntary sector.

0800 024 8425

City LinUX sample scripts - rclean


#!/bin/bash
# $Id: rclean,v 1.154 2017/01/02 19:54:55 fulford Exp fulford $
# $Source: /src/merlin/usr/local/etc/RCS/rclean,v $
# $Revision: 1.154 $
# Author C W Fulford.
# Copyright (c) 2012-2013 C W Fulford.
# Licensed for public use under the LGPL.
# For assistance contact fulford@fulford.net 0709 229 5385
############################################################################
cmd=`basename $0`
syntax="$cmd [-a #max_age] [-c <config_file>] [-d] [-l] [-t] [-v] <client_hostid>" 

while [ $# -gt 0 ];do
	case $1 in
		-a) orage=$2;shift 2;; #overides config file
		-c) config=$2;shift 2;;
		-d) debug=:;set -x;shift ;;
		-l) log=:;shift 1 ;;
		-t) testmode=:; archive=/var/ta/test`date +%d`.tar.gz;shift ;;
		-v) verbose=: ;shift 1;;
		 *) if [ $# -eq 1 ];then
			hostid=$1;shift; 
		    else
			echo "syntax: $syntax" >&2
			exit 1;
		    fi;;
	esac
done
arc=error_log`date +%d`
config=${config:-"/usr/local/etc/rclean.cf"}
debug=${debug:-""}
log=${log:-""}
tag=$cmd
tconf=/tmp/$cmd.$$
testmode=${testmode:-""}
tmp=/tmp/clean$$
verbose=${verbose:-""}

[ -r $config ] ||{ 
	echo "$cmd: configuration file $config not found">&2
	exit 1
}
grep -e "^$hostid:" $config >/dev/null 2>&1 || {
	echo "$cmd: host id \"$hostid\" not found in $config."
	exit 2
}
sed -ne '/^'$hostid':/,/^[\t ]*$/{
	/^[ \t]*[^=]*=.*$/p
}' $config >$tconf
eval `sed -ne '/^'$hostid':/,/^[\t ]*$/{
		/^[ \t]*[^=]*=.*$/p
	}' $tconf`

age=${age:-100} # max age in days before deletion
[ "$orage" ] && age=$orage # command line trumps default and config

awk  <$tconf '/target:/ {split(t,":",$1);print("cmd=find $2 -ctime=$3 -ls")}' 

echo "
#!/bin/bash
[ $debug ] && set -x
dcount=/tmp/dcount\$\$
zcount=/tmp/zcount\$\$
echo "0" >\$dcount
echo "0" >\$zcount

find $wd -type d -name \*\.co\.uk -print |
while read d ;do
	[ -d \$d/statistics/logs ] && {
       		cd \$d/statistics/logs ;
		[ $verbose ] && echo "wd=\$d"
		[ ! $testmode ] &&{
			cp error_log $arc ;
			if [ -f $arc ];then
	 			bash -c '>error_log'
				[ $verbose ] && echo "error_log truncated"
			fi
			[ -f $arc.gz ] && rm $arc.gz ;
			if gzip $arc ;then
				[ $verbose ] && echo "$arc.gz created"
				cfz=\$(<\$zcount)
				let cfz=\"\$cfz + 1\"
				echo \$cfz >\$zcount
			fi
			cd  \$wd
		}
	}
	find \$d/httpdocs/cron/backups \$d/httpdocs/cron/downloads \( -name EN_\*_META_ORG.zip -o -name httpdocs_\*.tar.gz -o -name mysql_\*.sql.gz \) -a -ctime +$age -print 2>/dev/null|
	while read f ;do
		[ ! $testmode ] &&{
			if rm \$f ;then
				[ $verbose ] && echo "\$f deleted"
				cfd=\$(<\$dcount)
				let cfd=\"\$cfd + 1\"
				echo \$cfd >\$dcount
			fi
		}
	done
done
find /usr/local/psa/PMM/logs -name pmmcli.log\* -ctime +$age\
       		-print 2>/dev/null|
while read f ;do
	if rm \$f ;then
		#cfd=\$((cfd+1))
		cfd=\$(<\$dcount)
		let cfd=\"\$cfd + 1\"
		echo \$cfd >\$dcount
	fi
done

find /var/log -name rkhunter\.log\.[0-9\-:]*$ -ctime $age\
		-print 2>/dev/null|
while read f ;do
	if rm \$f ;then
		cfd=\$((cfd+1))
		let cfd=\"\$cfd + 1\"
		echo \$cfd >\$dcount
	fi
done

# use local0.info to match rsyslog configuration
if [ $log ];then 
	cfd=\$(<\$dcount)
	cfz=\$(<\$zcount)
	logger -p local0.info -t $tag \"$hostid \$cfd files removed. \$cfz zipped.\" 
fi
[ $verbose ] && echo "\$0 removing self"
rm \$0 \$dcount \$zcount
" >$tmp

[ $verbose ] || options="-q"
scp $options $tmp $hostid:$tmp
[ $verbose ] && params="$params -v"
ssh $hostid "sudo sh $tmp $params"

######################################################################
# This program is free software: you can redistribute it and or      #
# modify it under the terms of the Lesser GNU General Public License #
# as published by the Free Software Foundation, either version 3 of  #
# the License, or (at your option) any later version.                #
#                                                                    #
# This program is distributed in the hope that it will be useful,    #
# but WITHOUT ANY WARRANTY; without even the implied warranty of     #    
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
# GNU General Public License for more details.                       #
#                                                                    # 
# A copy of the Lesser GNU General Public License and the GNU        #
# General Public License is available at                             #
# <http://www.gnu.org/licenses/>.                                    #
######################################################################

The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.