2006-01-26 00:37:29 -05:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
2006-11-08 13:25:52 -05:00
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
2006-03-21 15:25:41 -05:00
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2006-01-26 00:37:29 -05:00
|
|
|
#
|
2006-03-02 23:33:24 -05:00
|
|
|
# Shell script to copy snapshots of a Solr Lucene collection from the master
|
2006-01-26 00:37:29 -05:00
|
|
|
|
2006-03-21 15:25:41 -05:00
|
|
|
orig_dir=$(pwd)
|
|
|
|
cd ${0%/*}/..
|
|
|
|
solr_root=$(pwd)
|
|
|
|
cd ${orig_dir}
|
2006-01-26 00:37:29 -05:00
|
|
|
|
2006-03-21 15:25:41 -05:00
|
|
|
unset master_host rsyncd_port master_data_dir master_status_dir snap_name
|
2006-07-06 15:14:40 -04:00
|
|
|
unset sizeonly stats data_dir user verbose debug compress startStatus
|
2006-03-21 15:25:41 -05:00
|
|
|
. ${solr_root}/bin/scripts-util
|
2006-01-26 00:37:29 -05:00
|
|
|
|
|
|
|
# set up variables
|
|
|
|
prog=${0##*/}
|
2006-03-21 15:25:41 -05:00
|
|
|
log=${solr_root}/logs/${prog}.log
|
2006-01-26 00:37:29 -05:00
|
|
|
|
|
|
|
# define usage string
|
|
|
|
USAGE="\
|
2010-08-27 10:10:11 -04:00
|
|
|
usage: $prog [-M master] [-P portnum] [-D mdir] [-S sdir] [-n snapshot] [-d dir] [-u username] [-svVz]
|
2006-03-21 15:25:41 -05:00
|
|
|
-M master specify hostname of master server from where to pull index
|
|
|
|
snapshot
|
|
|
|
-P port specify rsyncd port number of master server from where to
|
|
|
|
pull index snapshot
|
|
|
|
-D specify directory holding index data on master server
|
|
|
|
-S specify directory holding snapshot status on master server
|
2006-01-26 00:37:29 -05:00
|
|
|
-n snapshot pull a specific snapshot by name
|
2006-03-21 15:25:41 -05:00
|
|
|
-d specify directory holding index data on local machine
|
|
|
|
-u specify user to sudo to before running script
|
2006-01-26 00:37:29 -05:00
|
|
|
-s use the --size-only option with rsync
|
|
|
|
-v increase verbosity (-vv show file transfer stats also)
|
2006-07-06 15:14:40 -04:00
|
|
|
-V output debugging info
|
2006-01-26 00:37:29 -05:00
|
|
|
-z enable compression of data
|
|
|
|
"
|
|
|
|
|
|
|
|
# parse args
|
2006-07-06 15:14:40 -04:00
|
|
|
while getopts M:P:D:S:n:d:u:svVz OPTION
|
2006-01-26 00:37:29 -05:00
|
|
|
do
|
|
|
|
case $OPTION in
|
2006-03-21 15:25:41 -05:00
|
|
|
M)
|
|
|
|
master_host="$OPTARG"
|
2006-01-26 00:37:29 -05:00
|
|
|
;;
|
2006-03-21 15:25:41 -05:00
|
|
|
P)
|
|
|
|
rsyncd_port="$OPTARG"
|
|
|
|
;;
|
|
|
|
D)
|
|
|
|
master_data_dir="$OPTARG"
|
|
|
|
;;
|
|
|
|
S)
|
|
|
|
master_status_dir="$OPTARG"
|
2006-01-26 00:37:29 -05:00
|
|
|
;;
|
|
|
|
n)
|
2006-03-21 15:25:41 -05:00
|
|
|
snap_name="$OPTARG"
|
|
|
|
;;
|
|
|
|
d)
|
|
|
|
data_dir="$OPTARG"
|
|
|
|
;;
|
|
|
|
u)
|
|
|
|
user="$OPTARG"
|
2006-01-26 00:37:29 -05:00
|
|
|
;;
|
|
|
|
s)
|
|
|
|
sizeonly="--size-only"
|
|
|
|
;;
|
|
|
|
v)
|
|
|
|
[[ -n $verbose ]] && stats="--stats" || verbose=v
|
|
|
|
;;
|
2006-07-06 15:14:40 -04:00
|
|
|
V)
|
|
|
|
debug="V"
|
|
|
|
;;
|
2006-01-26 00:37:29 -05:00
|
|
|
z)
|
|
|
|
compress="z"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "$USAGE"
|
|
|
|
exit 1
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2006-07-06 15:14:40 -04:00
|
|
|
[[ -n $debug ]] && set -x
|
|
|
|
|
2006-03-21 15:25:41 -05:00
|
|
|
if [[ -z ${master_host} ]]
|
|
|
|
then
|
|
|
|
echo "name of master server missing in $confFile or command line."
|
|
|
|
echo "$USAGE"
|
|
|
|
exit 1
|
|
|
|
fi
|
2006-01-26 00:37:29 -05:00
|
|
|
|
2006-03-24 14:56:18 -05:00
|
|
|
# try to determine rsyncd port number from $confFile if not specified on
|
|
|
|
# command line, default to solr_port+10000
|
2006-03-21 15:25:41 -05:00
|
|
|
if [[ -z ${rsyncd_port} ]]
|
|
|
|
then
|
2006-03-24 14:56:18 -05:00
|
|
|
if [[ "${solr_port}" ]]
|
|
|
|
then
|
|
|
|
rsyncd_port=`expr 10000 + ${solr_port}`
|
|
|
|
else
|
|
|
|
echo "rsyncd port number of master server missing in $confFile or command line."
|
|
|
|
echo "$USAGE"
|
|
|
|
exit 1
|
|
|
|
fi
|
2006-03-21 15:25:41 -05:00
|
|
|
fi
|
2006-01-26 00:37:29 -05:00
|
|
|
|
2006-03-21 15:25:41 -05:00
|
|
|
if [[ -z ${master_data_dir} ]]
|
2006-01-26 00:37:29 -05:00
|
|
|
then
|
2006-03-21 15:25:41 -05:00
|
|
|
echo "directory holding index data on master server missing in $confFile or command line."
|
2006-01-26 00:37:29 -05:00
|
|
|
echo "$USAGE"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2006-03-21 15:25:41 -05:00
|
|
|
if [[ -z ${master_status_dir} ]]
|
2006-01-26 00:37:29 -05:00
|
|
|
then
|
2006-03-21 15:25:41 -05:00
|
|
|
echo "directory holding snapshot status on master server missing in $confFile or command line."
|
2006-01-26 00:37:29 -05:00
|
|
|
echo "$USAGE"
|
|
|
|
exit 1
|
2006-03-21 15:25:41 -05:00
|
|
|
fi
|
2006-01-26 00:37:29 -05:00
|
|
|
|
2006-03-21 15:25:41 -05:00
|
|
|
fixUser "$@"
|
|
|
|
|
2010-08-27 10:10:11 -04:00
|
|
|
dataDir
|
2006-01-26 00:37:29 -05:00
|
|
|
|
2006-03-21 15:25:41 -05:00
|
|
|
# assume relative path to start at ${solr_root}
|
|
|
|
if [[ "`echo ${master_data_dir}|cut -c1`" != "/" ]]
|
|
|
|
then
|
|
|
|
master_data_dir=${solr_root}/${master_data_dir}
|
|
|
|
fi
|
|
|
|
if [[ "`echo ${master_status_dir}|cut -c1`" != "/" ]]
|
|
|
|
then
|
|
|
|
master_status_dir=${solr_root}/${master_status_dir}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# push stats/state to master if necessary
|
|
|
|
function pushStatus
|
|
|
|
{
|
|
|
|
scp -q -o StrictHostKeyChecking=no ${solr_root}/logs/snappuller.status ${master_host}:${master_status_dir}/snapshot.status.`uname -n`
|
|
|
|
}
|
|
|
|
|
2007-07-17 08:31:03 -04:00
|
|
|
setStartTime
|
2006-01-26 00:37:29 -05:00
|
|
|
|
2006-03-21 15:25:41 -05:00
|
|
|
logMessage started by $oldwhoami
|
|
|
|
logMessage command: $0 $@
|
|
|
|
|
|
|
|
if [[ ! -f ${solr_root}/logs/snappuller-enabled ]]
|
2006-01-26 00:37:29 -05:00
|
|
|
then
|
|
|
|
logMessage snappuller disabled
|
2008-04-07 12:50:08 -04:00
|
|
|
exit 2
|
2006-01-26 00:37:29 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
# make sure we can ssh to master
|
|
|
|
if
|
2006-03-21 15:25:41 -05:00
|
|
|
! ssh -o StrictHostKeyChecking=no ${master_host} id 1>/dev/null 2>&1
|
2006-01-26 00:37:29 -05:00
|
|
|
then
|
2006-03-21 15:25:41 -05:00
|
|
|
logMessage failed to ssh to master ${master_host}
|
2006-01-26 00:37:29 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# get directory name of latest snapshot if not specified on command line
|
2006-03-21 15:25:41 -05:00
|
|
|
if [[ -z ${snap_name} ]]
|
2006-01-26 00:37:29 -05:00
|
|
|
then
|
2008-11-20 09:14:07 -05:00
|
|
|
snap_name=`ssh -o StrictHostKeyChecking=no ${master_host} "perl -e 'chdir q|${master_data_dir}|; print ((sort grep {/^snapshot[.][1-9][0-9]{13}$/} <*>)[-1])'"`
|
2006-03-21 15:25:41 -05:00
|
|
|
fi
|
|
|
|
if [[ "${snap_name}" == "" ]]
|
|
|
|
then
|
|
|
|
logMessage no snapshot available on ${master_host} in ${master_data_dir}
|
|
|
|
logExit ended 0
|
|
|
|
else
|
|
|
|
name=`basename ${snap_name}`
|
2006-01-26 00:37:29 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
# clean up after INT/TERM
|
2006-03-21 15:25:41 -05:00
|
|
|
trap 'echo cleaning up, please wait ...;/bin/rm -rf ${data_dir}/${name} ${data_dir}/${name}-wip;echo ${startStatus} aborted:$(timeStamp)>${solr_root}/logs/snappuller.status;pushStatus;logExit aborted 13' INT TERM
|
2006-01-26 00:37:29 -05:00
|
|
|
|
2006-03-21 15:25:41 -05:00
|
|
|
if [[ -d ${data_dir}/${name} || -d ${data_dir}/${name}-wip ]]
|
2006-01-26 00:37:29 -05:00
|
|
|
then
|
2006-03-21 15:25:41 -05:00
|
|
|
logMessage no new snapshot available on ${master_host} in ${master_data_dir}
|
2006-01-26 00:37:29 -05:00
|
|
|
logExit ended 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# take a snapshot of current index so that only modified files will be rsync-ed
|
|
|
|
# put the snapshot in the 'work-in-progress" directory to prevent it from
|
|
|
|
# being installed while the copying is still in progress
|
2008-08-08 11:23:48 -04:00
|
|
|
if [[ "${OS}" == "SunOS" || "${OS}" == "Darwin" || "${OS}" == "FreeBSD" ]]
|
2007-07-12 08:35:01 -04:00
|
|
|
then
|
|
|
|
orig_dir=$(pwd)
|
|
|
|
mkdir ${data_dir}/${name}-wip
|
|
|
|
cd ${data_dir}/index
|
|
|
|
find . -print|cpio -pdlmu ${data_dir}/${name}-wip 1>/dev/null 2>&1
|
|
|
|
cd ${orig_dir}
|
|
|
|
else
|
|
|
|
cp -lr ${data_dir}/index ${data_dir}/${name}-wip
|
|
|
|
fi
|
2006-01-26 00:37:29 -05:00
|
|
|
# force rsync of segments and .del files since we are doing size-only
|
|
|
|
if [[ -n ${sizeonly} ]]
|
|
|
|
then
|
2006-03-21 15:25:41 -05:00
|
|
|
rm -f ${data_dir}/${name}-wip/segments
|
|
|
|
rm -f ${data_dir}/${name}-wip/*.del
|
2006-01-26 00:37:29 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
logMessage pulling snapshot ${name}
|
|
|
|
|
|
|
|
# make sure master has directory for hold slaves stats/state
|
2006-03-21 15:25:41 -05:00
|
|
|
ssh -o StrictHostKeyChecking=no ${master_host} mkdir -p ${master_status_dir}
|
2006-01-26 00:37:29 -05:00
|
|
|
|
|
|
|
# start new distribution stats
|
2007-07-17 08:51:00 -04:00
|
|
|
rsyncStart=`date +'%Y-%m-%d %H:%M:%S'`
|
2008-08-14 09:41:56 -04:00
|
|
|
if [[ "${OS}" == "Darwin" || "${OS}" == "FreeBSD" ]]
|
|
|
|
then
|
|
|
|
startTimestamp=`date -j -f '%Y-%m-%d %H:%M:%S' "$rsyncStart" +'%Y%m%d-%H%M%S'`
|
|
|
|
rsyncStartSec=`date -j -f '%Y-%m-%d %H:%M:%S' "$rsyncStart" +'%s'`
|
|
|
|
else
|
2006-01-26 00:37:29 -05:00
|
|
|
startTimestamp=`date -d "$rsyncStart" +'%Y%m%d-%H%M%S'`
|
|
|
|
rsyncStartSec=`date -d "$rsyncStart" +'%s'`
|
2008-08-14 09:41:56 -04:00
|
|
|
fi
|
2006-01-26 00:37:29 -05:00
|
|
|
startStatus="rsync of `basename ${name}` started:$startTimestamp"
|
2006-03-21 15:25:41 -05:00
|
|
|
echo ${startStatus} > ${solr_root}/logs/snappuller.status
|
|
|
|
pushStatus
|
2006-01-26 00:37:29 -05:00
|
|
|
|
|
|
|
# rsync over files that have changed
|
|
|
|
rsync -Wa${verbose}${compress} --delete ${sizeonly} \
|
2006-03-21 15:25:41 -05:00
|
|
|
${stats} rsync://${master_host}:${rsyncd_port}/solr/${name}/ ${data_dir}/${name}-wip
|
2006-01-26 00:37:29 -05:00
|
|
|
|
|
|
|
rc=$?
|
2007-07-17 08:51:00 -04:00
|
|
|
rsyncEnd=`date +'%Y-%m-%d %H:%M:%S'`
|
2008-08-14 09:41:56 -04:00
|
|
|
if [[ "${OS}" == "Darwin" || "${OS}" == "FreeBSD" ]]
|
|
|
|
then
|
|
|
|
endTimestamp=`date -j -f '%Y-%m-%d %H:%M:%S' "$rsyncEnd" +'%Y%m%d-%H%M%S'`
|
|
|
|
rsyncEndSec=`date -j -f '%Y-%m-%d %H:%M:%S' "$rsyncEnd" +'%s'`
|
|
|
|
else
|
2006-01-26 00:37:29 -05:00
|
|
|
endTimestamp=`date -d "$rsyncEnd" +'%Y%m%d-%H%M%S'`
|
|
|
|
rsyncEndSec=`date -d "$rsyncEnd" +'%s'`
|
2008-08-14 09:41:56 -04:00
|
|
|
fi
|
2006-01-26 00:37:29 -05:00
|
|
|
elapsed=`expr $rsyncEndSec - $rsyncStartSec`
|
|
|
|
if [[ $rc != 0 ]]
|
|
|
|
then
|
|
|
|
logMessage rsync failed
|
2006-03-21 15:25:41 -05:00
|
|
|
/bin/rm -rf ${data_dir}/${name}-wip
|
|
|
|
echo ${startStatus} failed:$endTimestamp > ${solr_root}/logs/snappuller.status
|
|
|
|
pushStatus
|
2006-01-26 00:37:29 -05:00
|
|
|
logExit failed 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# move into place atomically
|
2006-03-21 15:25:41 -05:00
|
|
|
mv ${data_dir}/${name}-wip ${data_dir}/${name}
|
2006-01-26 00:37:29 -05:00
|
|
|
|
|
|
|
# finish new distribution stats`
|
2006-03-21 15:25:41 -05:00
|
|
|
echo ${startStatus} ended:$endTimestamp rsync-elapsed:${elapsed} > ${solr_root}/logs/snappuller.status
|
|
|
|
pushStatus
|
2006-01-26 00:37:29 -05:00
|
|
|
logExit ended 0
|