Mongo Restore

#!/bin/sh

HOST_IP=`/sbin/ifconfig | sed -n 's/.*inet addr:\([0-9.]\+\)\s.*/\1/p' | head -n1`
echo "HOST_IP: " $HOST_IP

HOST_NAME=`hostname`
echo "HOST_NAME: " $HOST_NAME

IS_PRIMARY=`mongo --quiet $HOST_IP --eval "print(db.isMaster().ismaster);"`
echo "IS_PRIMARY: " $IS_PRIMARY

PRIMARY_HOST=`mongo --quiet $HOST_IP --eval " var primary = db.isMaster().primary;
if(primary && primary.indexOf(':') > 0 ) primary = primary.substring(0, primary.indexOf(':'));
print(primary);"`
echo "PRIMARY_HOST: " $PRIMARY_HOST

if [ "${IS_PRIMARY}" == "true" ]
then
    echo "$PRIMARY_HOST is primary mongo node. Restoring data...."
    BACKUP_FOLDER=$1

    if [ "x$BACKUP_FOLDER" = "x" ]; then
        echo "Missing database dump folder: mongo-db-restore.sh <BKP_FOLDER>"
        exit
    fi

    if [ ! -d $BACKUP_FOLDER ]; then
        echo "Database dump folder does not exist: $BACKUP_FOLDER"
        exit
    fi

    if [ ! -d $BACKUP_FOLDER/dump ]; then
        echo "Database dump folder does not have sub-folder 'dump'. May not be the correct folder: $BACKUP_FOLDER"
        exit
    fi
    cd $BACKUP_FOLDER
    echo "PWD:" `pwd`
    RESTORE_RESULT=`mongorestore -h $HOST_IP --drop`
    echo "Restore complete."

else
    echo "$HOST_NAME is not primary mongo server ($PRIMARY_HOST is primary). Restore should be run only on the primary."
fi

猜你喜欢

转载自www.cnblogs.com/tben/p/8985065.html