#!/bin/bash # # $Id: snapcleaner.template,v 1.7 2005/06/09 15:34:06 billa Exp $ # $Source: /cvs/main/searching/solar-tools/snapcleaner.template,v $ # $Name: r20050725_standardized_server_enabled $ # # Shell script to clean up snapshots of a SOLAR Lucene collection. export PATH=/sbin:/usr/sbin:/bin:/usr/bin:$PATH # sudo to app user if necessary if [[ $(whoami) != app ]] then sudo -u app $0 "$@" exit $? fi oldwhoami=$(who -m | cut -d' ' -f1 | sed -e's/^.*!//') if [[ "${oldwhoami}" == "" ]] then oldwhoami=`ps h -Hfp $(pgrep -g0 ${0##*/}) | tail -1|cut -f1 -d" "` fi # set up variables prog=${0##*/} log=logs/${prog}.log # define usage string USAGE="\ usage: $prog -d | -n [ -v ] -d cleanup snapshots more than days old -n keep the most most recent number of snapshots and cleanup up the remaining ones that are not being pulled -v increase verbosity " unset days num verbose # parse args originalargs="$@" while getopts d:n:v OPTION do case $OPTION in d) days="$OPTARG" ;; n) num="$OPTARG" ;; v) verbose="v" ;; *) echo "$USAGE" exit 1 esac done shift $(( OPTIND - 1 )) if [[ -z ${days} && -z ${num} ]] then echo "$USAGE" exit 1 fi function timeStamp { date +'%Y%m%d-%H%M%S' } function logMessage { echo $(timeStamp) $@>>$log if [[ -n ${verbose} ]] then echo $@ fi } function logExit { end=`date +"%s"` diff=`expr $end - $start` echo "$(timeStamp) $1 (elapsed time: $diff sec)">>$log exit $2 } function remove { syncing=`ps -fwwwu app|grep rsync|grep -v grep|cut -f6 -d"/"|grep $1` if [[ -n $syncing ]] then logMessage $1 not removed - rsync in progress else logMessage removing snapshot $1 /bin/rm -rf $1 fi } cd ${0%/*}/../.. logMessage started by $oldwhoami logMessage command: $0 $originalargs start=`date +"%s"` # trap control-c trap 'echo "caught INT/TERM, exiting now but partial cleanup may have already occured";logExit aborted 13' INT TERM if [[ -n ${days} ]] then logMessage cleaning up snapshots more than ${days} days old for i in `find . -name "snapshot.*" -maxdepth 1 -mtime +${days} -print` do remove `basename $i` done elif [[ -n ${num} ]] then logMessage cleaning up all snapshots except for the most recent ${num} ones unset snapshots count snapshots=`ls -cd snapshot.* 2>/dev/null` if [[ $? == 0 ]] then count=`echo $snapshots|wc -w` startpos=`expr $num + 1` if [[ $count -gt $num ]] then for i in `echo $snapshots|cut -f${startpos}- -d" "` do remove $i done fi fi fi logExit ended 0