2006-01-26 00:37:29 -05:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
2006-03-21 15:25:41 -05:00
|
|
|
# Copyright 2006 The Apache Software Foundation
|
|
|
|
#
|
|
|
|
# Licensed 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
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
unset sizeonly stats data_dir user verbose compress startStatus
|
|
|
|
. ${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="\
|
2006-03-21 15:25:41 -05:00
|
|
|
usage: $prog [-M master] [-P portnum] [-D mdir] [-S sdir] [-n snapshot] [-d dir] [-u username] [-svz]
|
|
|
|
-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)
|
|
|
|
-z enable compression of data
|
|
|
|
"
|
|
|
|
|
|
|
|
# parse args
|
2006-03-21 15:25:41 -05:00
|
|
|
while getopts M:P:D:S:n:d:u:svz 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
|
|
|
|
;;
|
|
|
|
z)
|
|
|
|
compress="z"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "$USAGE"
|
|
|
|
exit 1
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
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-21 15:25:41 -05:00
|
|
|
if [[ -z ${rsyncd_port} ]]
|
|
|
|
then
|
|
|
|
echo "rsyncd port number of master server missing in $confFile or command line."
|
|
|
|
echo "$USAGE"
|
|
|
|
exit 1
|
|
|
|
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 "$@"
|
|
|
|
|
2006-03-24 09:59:43 -05:00
|
|
|
# use default value for data_dir if not specified
|
2006-03-21 15:25:41 -05:00
|
|
|
# relative path starts at ${solr_root}
|
|
|
|
if [[ -z ${data_dir} ]]
|
|
|
|
then
|
|
|
|
data_dir=${solr_root}/data
|
|
|
|
elif [[ "`echo ${data_dir}|cut -c1`" != "/" ]]
|
|
|
|
then
|
|
|
|
data_dir=${solr_root}/${data_dir}
|
2006-01-26 00:37:29 -05:00
|
|
|
fi
|
|
|
|
|
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`
|
|
|
|
}
|
|
|
|
|
2006-01-26 00:37:29 -05:00
|
|
|
start=`date +"%s"`
|
|
|
|
|
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
|
|
|
|
exit 1
|
|
|
|
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
|
2006-03-21 15:25:41 -05:00
|
|
|
snap_name=`ssh -o StrictHostKeyChecking=no ${master_host} "ls -d ${master_data_dir}/snapshot.* 2>/dev/null"|tail -1`
|
|
|
|
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
|
2006-03-21 15:25:41 -05:00
|
|
|
cp -lr ${data_dir}/index ${data_dir}/${name}-wip
|
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
|
|
|
|
rsyncStart=`date`
|
|
|
|
startTimestamp=`date -d "$rsyncStart" +'%Y%m%d-%H%M%S'`
|
|
|
|
rsyncStartSec=`date -d "$rsyncStart" +'%s'`
|
|
|
|
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=$?
|
|
|
|
rsyncEnd=`date`
|
|
|
|
endTimestamp=`date -d "$rsyncEnd" +'%Y%m%d-%H%M%S'`
|
|
|
|
rsyncEndSec=`date -d "$rsyncEnd" +'%s'`
|
|
|
|
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
|