Last active
August 14, 2021 15:37
-
-
Save henrik242/3bdbe8d505e0642c7a29223a66dbad83 to your computer and use it in GitHub Desktop.
SolrCloud backup/restore without shared disk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# https://lucene.apache.org/solr/guide/7_5/making-and-restoring-backups.html explains that | |
# SolrCloud Backup/Restore requires a shared file system mounted at the same path on all nodes. | |
# | |
# This script uses the old Solr Backup/Restore procedure, which is useful if you don't have | |
# access to a shared disk. | |
# | |
# Requires that the target collection is already set up, with identical number of shards/replicas. | |
set -o errexit -o pipefail -o nounset | |
COLLECTION=my_collection_name | |
FROMHOST=old.solr.host:12655 | |
TOHOST=new.solr.host:12599 | |
MYUSER=myuser | |
SSHOPTS="-o StrictHostKeyChecking=no" | |
TMPDIR=/tmp/backup_$COLLECTION | |
DRY=dry | |
#DRY= | |
# Should produce a list of "replica host port" | |
function getstat() { | |
curl -s "http://$1/solr/admin/collections?action=CLUSTERSTATUS&wt=json" \ | |
| grep -e $COLLECTION -e \"base_url\" \ | |
| grep -A1 ${COLLECTION}_shard \ | |
| cut -d\" -f4 \ | |
| perl -0777 -pe 's|(\d)\n|\1 |g; s|http://||g; s|:(\d+)/solr| \1|g' \ | |
| grep $COLLECTION > $2 | |
} | |
function dry() { | |
echo WOULD HAVE RUN: $@ | |
} | |
# oldreplica oldhost oldport newreplica newhost newport | |
function backup() { | |
$DRY ssh -n $SSHOPTS $MYUSER@$2 mkdir -p $TMPDIR/$1 | |
$DRY curl -s "http://$2:$3/solr/$1/replication?command=backup&numberToKeep=1&location=$TMPDIR/$1" | |
} | |
# oldreplica oldhost oldport newreplica newhost newport | |
function restore() { | |
$DRY ssh -n $SSHOPTS $MYUSER@$5 mkdir -p $TMPDIR | |
$DRY ssh -n $SSHOPTS $MYUSER@$2 "cd $TMPDIR && rsync -e 'ssh $SSHOPTS' -av $1 $MYUSER@$5:$TMPDIR/" | |
$DRY curl -s "http://$5:$6/solr/$4/replication?command=restore&location=$TMPDIR/$1" | |
} | |
getstat $FROMHOST /tmp/old | |
getstat $TOHOST /tmp/new | |
paste -d" " /tmp/old /tmp/new > /tmp/pasted | |
# Format of /tmp/pasted: | |
# oldreplica oldhost oldport newreplica newhost newport | |
printf '\nSTATUS:\n' | |
cat /tmp/pasted | |
printf '\nPress any key to backup\n' | |
read foo | |
printf '\nBACKUP\n' | |
cat /tmp/pasted | while read i; do | |
backup $i | |
done | |
printf "\nDone. You should really run http://${FROMHOST}/solr/INSERT_REPLICA_NAME/replication?command=details on all backup hosts to check backup states before restoring\n" | |
printf '\nPress any key to restore\n' | |
read foo | |
printf '\nRESTORING\n' | |
cat /tmp/pasted | while read i; do | |
restore $i | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment