0b0582a3aa10227767e359e693c4b43fec272388nd#!/bin/bash
0b0582a3aa10227767e359e693c4b43fec272388nd#
031b91a62d25106ae69d4693475c79618dd5e884fielding# Licensed to the Apache Software Foundation (ASF) under one or more
031b91a62d25106ae69d4693475c79618dd5e884fielding# contributor license agreements. See the NOTICE file distributed with
031b91a62d25106ae69d4693475c79618dd5e884fielding# this work for additional information regarding copyright ownership.
031b91a62d25106ae69d4693475c79618dd5e884fielding# The ASF licenses this file to You under the Apache License, Version 2.0
031b91a62d25106ae69d4693475c79618dd5e884fielding# (the "License"); you may not use this file except in compliance with
031b91a62d25106ae69d4693475c79618dd5e884fielding# the License. You may obtain a copy of the License at
0b0582a3aa10227767e359e693c4b43fec272388nd#
0b0582a3aa10227767e359e693c4b43fec272388nd# http://www.apache.org/licenses/LICENSE-2.0
0b0582a3aa10227767e359e693c4b43fec272388nd#
0b0582a3aa10227767e359e693c4b43fec272388nd# Unless required by applicable law or agreed to in writing, software
0b0582a3aa10227767e359e693c4b43fec272388nd# distributed under the License is distributed on an "AS IS" BASIS,
0b0582a3aa10227767e359e693c4b43fec272388nd# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0b0582a3aa10227767e359e693c4b43fec272388nd# See the License for the specific language governing permissions and
0b0582a3aa10227767e359e693c4b43fec272388nd# limitations under the License.
0b0582a3aa10227767e359e693c4b43fec272388nd#
0b0582a3aa10227767e359e693c4b43fec272388nd# Shell script to clean up snapshots of a Solr Lucene collection.
0b0582a3aa10227767e359e693c4b43fec272388nd
0b0582a3aa10227767e359e693c4b43fec272388ndorig_dir=$(pwd)
0b0582a3aa10227767e359e693c4b43fec272388ndcd ${0%/*}/..
0b0582a3aa10227767e359e693c4b43fec272388ndsolr_root=$(pwd)
0b0582a3aa10227767e359e693c4b43fec272388ndcd ${orig_dir}
41789a044f209e8f989133e3be14d2cfcb9ceb39rjung
c828ca1d4ed93f618316d64e03ce3fcea5eb285eigalicunset days num data_dir user verbose debug
3174ed450858787dfec8bf9701e282d25e87188brbowen. ${solr_root}/bin/scripts-util
3174ed450858787dfec8bf9701e282d25e87188brbowen
a546d8ff17af2eb98179377f39a02ac81459f0bend# set up variables
a546d8ff17af2eb98179377f39a02ac81459f0bendprog=${0##*/}
a546d8ff17af2eb98179377f39a02ac81459f0bendlog=${solr_root}/logs/${prog}.log
a546d8ff17af2eb98179377f39a02ac81459f0bend
a546d8ff17af2eb98179377f39a02ac81459f0bend# define usage string
a546d8ff17af2eb98179377f39a02ac81459f0bendUSAGE="\
a546d8ff17af2eb98179377f39a02ac81459f0bendusage: $prog -D <days> | -N <num> [-d dir] [-u username] [-v] [-V]
a546d8ff17af2eb98179377f39a02ac81459f0bend -D <days> cleanup snapshots more than <days> days old
a546d8ff17af2eb98179377f39a02ac81459f0bend -N <num> keep the most recent <num> number of snapshots and
a546d8ff17af2eb98179377f39a02ac81459f0bend cleanup up the remaining ones that are not being pulled
a546d8ff17af2eb98179377f39a02ac81459f0bend -d specify directory holding index data
a546d8ff17af2eb98179377f39a02ac81459f0bend -u specify user to sudo to before running script
a546d8ff17af2eb98179377f39a02ac81459f0bend -v increase verbosity
a546d8ff17af2eb98179377f39a02ac81459f0bend -V output debugging info
a546d8ff17af2eb98179377f39a02ac81459f0bend"
a546d8ff17af2eb98179377f39a02ac81459f0bend
a546d8ff17af2eb98179377f39a02ac81459f0bend# parse args
a546d8ff17af2eb98179377f39a02ac81459f0bendwhile getopts D:N:d:u:vV OPTION
a546d8ff17af2eb98179377f39a02ac81459f0benddo
a546d8ff17af2eb98179377f39a02ac81459f0bend case $OPTION in
a546d8ff17af2eb98179377f39a02ac81459f0bend D)
a546d8ff17af2eb98179377f39a02ac81459f0bend days="$OPTARG"
a546d8ff17af2eb98179377f39a02ac81459f0bend ;;
0b0582a3aa10227767e359e693c4b43fec272388nd N)
0b0582a3aa10227767e359e693c4b43fec272388nd num="$OPTARG"
0b0582a3aa10227767e359e693c4b43fec272388nd ;;
0b0582a3aa10227767e359e693c4b43fec272388nd d)
0b0582a3aa10227767e359e693c4b43fec272388nd data_dir="$OPTARG"
0b0582a3aa10227767e359e693c4b43fec272388nd ;;
0b0582a3aa10227767e359e693c4b43fec272388nd u)
0b0582a3aa10227767e359e693c4b43fec272388nd user="$OPTARG"
0b0582a3aa10227767e359e693c4b43fec272388nd ;;
ada7369deaf47fb16f6079f0c9af273f33050ff4nd v)
ada7369deaf47fb16f6079f0c9af273f33050ff4nd verbose="v"
ada7369deaf47fb16f6079f0c9af273f33050ff4nd ;;
ada7369deaf47fb16f6079f0c9af273f33050ff4nd V)
ada7369deaf47fb16f6079f0c9af273f33050ff4nd debug="V"
ada7369deaf47fb16f6079f0c9af273f33050ff4nd ;;
0b0582a3aa10227767e359e693c4b43fec272388nd *)
0b0582a3aa10227767e359e693c4b43fec272388nd echo "$USAGE"
ada7369deaf47fb16f6079f0c9af273f33050ff4nd exit 1
e5576107d28b5d7f76c4515e39f197e6b8bcba9and esac
e5576107d28b5d7f76c4515e39f197e6b8bcba9anddone
0b0582a3aa10227767e359e693c4b43fec272388nd
0b0582a3aa10227767e359e693c4b43fec272388nd[[ -n $debug ]] && set -x
0b0582a3aa10227767e359e693c4b43fec272388nd
0b0582a3aa10227767e359e693c4b43fec272388ndif [[ -z ${days} && -z ${num} ]]
0b0582a3aa10227767e359e693c4b43fec272388ndthen
0b0582a3aa10227767e359e693c4b43fec272388nd echo "$USAGE"
0b0582a3aa10227767e359e693c4b43fec272388nd exit 1
0b0582a3aa10227767e359e693c4b43fec272388ndfi
0b0582a3aa10227767e359e693c4b43fec272388nd
0b0582a3aa10227767e359e693c4b43fec272388ndfixUser "$@"
0b0582a3aa10227767e359e693c4b43fec272388nd
0b0582a3aa10227767e359e693c4b43fec272388nddataDir
0b0582a3aa10227767e359e693c4b43fec272388nd
ada7369deaf47fb16f6079f0c9af273f33050ff4ndfunction remove
ada7369deaf47fb16f6079f0c9af273f33050ff4nd{
ada7369deaf47fb16f6079f0c9af273f33050ff4nd if [[ "${OS}" == "Darwin" || "${OS}" == "FreeBSD" ]]
ada7369deaf47fb16f6079f0c9af273f33050ff4nd then
ada7369deaf47fb16f6079f0c9af273f33050ff4nd syncing=`ps -www -U ${user} |grep -w rsync|grep -v grep|grep -w $1`
ada7369deaf47fb16f6079f0c9af273f33050ff4nd else
0b0582a3aa10227767e359e693c4b43fec272388nd syncing=`ps -fwwwu ${user}|grep -w rsync|grep -v grep|grep -w $1`
0b0582a3aa10227767e359e693c4b43fec272388nd fi
ada7369deaf47fb16f6079f0c9af273f33050ff4nd
e5576107d28b5d7f76c4515e39f197e6b8bcba9and if [[ -n $syncing ]]
e5576107d28b5d7f76c4515e39f197e6b8bcba9and then
0b0582a3aa10227767e359e693c4b43fec272388nd logMessage $1 not removed - rsync in progress
0b0582a3aa10227767e359e693c4b43fec272388nd else
0b0582a3aa10227767e359e693c4b43fec272388nd logMessage removing snapshot $1
41789a044f209e8f989133e3be14d2cfcb9ceb39rjung /bin/rm -rf $1
0b0582a3aa10227767e359e693c4b43fec272388nd fi
0b0582a3aa10227767e359e693c4b43fec272388nd}
0b0582a3aa10227767e359e693c4b43fec272388nd
0b0582a3aa10227767e359e693c4b43fec272388ndsetStartTime
0b0582a3aa10227767e359e693c4b43fec272388nd
0b0582a3aa10227767e359e693c4b43fec272388ndlogMessage started by $oldwhoami
0b0582a3aa10227767e359e693c4b43fec272388ndlogMessage command: $0 $@
0b0582a3aa10227767e359e693c4b43fec272388nd
0b0582a3aa10227767e359e693c4b43fec272388nd# trap control-c
0b0582a3aa10227767e359e693c4b43fec272388ndtrap 'echo "caught INT/TERM, exiting now but partial cleanup may have already occured";logExit aborted 13' INT TERM
0b0582a3aa10227767e359e693c4b43fec272388nd
0b0582a3aa10227767e359e693c4b43fec272388ndif [[ -n ${days} ]]
0b0582a3aa10227767e359e693c4b43fec272388ndthen
0b0582a3aa10227767e359e693c4b43fec272388nd #is maxdepth supported?
ada7369deaf47fb16f6079f0c9af273f33050ff4nd find ${data_dir} -maxdepth 0 -name foobar >/dev/null 2>&1
ada7369deaf47fb16f6079f0c9af273f33050ff4nd if [ $? = 0 ]; then
ada7369deaf47fb16f6079f0c9af273f33050ff4nd maxdepth="-maxdepth 1"
ada7369deaf47fb16f6079f0c9af273f33050ff4nd else
ada7369deaf47fb16f6079f0c9af273f33050ff4nd unset maxdepth
ada7369deaf47fb16f6079f0c9af273f33050ff4nd fi
0b0582a3aa10227767e359e693c4b43fec272388nd
0b0582a3aa10227767e359e693c4b43fec272388nd logMessage cleaning up snapshots more than ${days} days old
ada7369deaf47fb16f6079f0c9af273f33050ff4nd for i in `find ${data_dir} ${maxdepth} -name 'snapshot.*' -mtime +${days} -print`
e5576107d28b5d7f76c4515e39f197e6b8bcba9and do
e5576107d28b5d7f76c4515e39f197e6b8bcba9and remove $i
0b0582a3aa10227767e359e693c4b43fec272388nd done
0b0582a3aa10227767e359e693c4b43fec272388ndelif [[ -n ${num} ]]
0b0582a3aa10227767e359e693c4b43fec272388ndthen
0b0582a3aa10227767e359e693c4b43fec272388nd logMessage cleaning up all snapshots except for the most recent ${num} ones
61d237f7e3a54089a7514227d663fac296d7d8f0bnicholes unset snapshots count
61d237f7e3a54089a7514227d663fac296d7d8f0bnicholes snapshots=`find ${data_dir} -type d -name 'snapshot.*' 2>/dev/null| sort -r`
61d237f7e3a54089a7514227d663fac296d7d8f0bnicholes if [[ $? == 0 ]]
61d237f7e3a54089a7514227d663fac296d7d8f0bnicholes then
61d237f7e3a54089a7514227d663fac296d7d8f0bnicholes count=`echo $snapshots|wc -w`
61d237f7e3a54089a7514227d663fac296d7d8f0bnicholes startpos=`expr $num + 1`
61d237f7e3a54089a7514227d663fac296d7d8f0bnicholes if [[ $count -gt $num ]]
61d237f7e3a54089a7514227d663fac296d7d8f0bnicholes then
ada7369deaf47fb16f6079f0c9af273f33050ff4nd for i in `echo $snapshots|cut -f${startpos}- -d" "`
ada7369deaf47fb16f6079f0c9af273f33050ff4nd do
ada7369deaf47fb16f6079f0c9af273f33050ff4nd remove $i
ada7369deaf47fb16f6079f0c9af273f33050ff4nd done
ada7369deaf47fb16f6079f0c9af273f33050ff4nd fi
ada7369deaf47fb16f6079f0c9af273f33050ff4nd fi
61d237f7e3a54089a7514227d663fac296d7d8f0bnicholesfi
61d237f7e3a54089a7514227d663fac296d7d8f0bnicholes
ada7369deaf47fb16f6079f0c9af273f33050ff4ndlogExit ended 0
e5576107d28b5d7f76c4515e39f197e6b8bcba9and
e5576107d28b5d7f76c4515e39f197e6b8bcba9and
7fb3a4909879d0bf4031292dd34e64a9b50f8d4dnd