-
-
Save vip20/57c0f1932af7b43a254c892591c7f0e5 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# OPLOG_LIMIT: only include oplog entries before the provided Timestamp | |
# The timestamp is a unix timestamp | |
# If your desaster happened for example on 2017-18-10 12:20 and you want to restore until 12:19 | |
# your timestamp is 'date -d "2017-10-18 12:19:00" +%s' | |
FULL_DUMP_DIRECTORY=$1 | |
OPLOGS_DIRECTORY=$2 | |
OPLOG_LIMIT=$3 | |
MONGODB_USER=$4 | |
MONGODB_PWD=$5 | |
if [ "$FULL_DUMP_DIRECTORY" == "" ]; then | |
echo "usage: restoreOpLogs.sh [NAME_OF_DIRECTORY_OF_FULL_DUMP] [DIRECTORY_TOP_OPLOGS]" | |
exit 1 | |
fi | |
if [ "$OPLOGS_DIRECTORY" == "" ]; then | |
echo "usage: restoreOpLogs.sh [NAME_OF_DIRECTORY_OF_FULL_DUMP] [DIRECTORY_TOP_OPLOGS]" | |
exit 1 | |
fi | |
if [ "$OPLOG_LIMIT" == "" ]; then | |
echo "usage: restoreOpLogs.sh [NAME_OF_DIRECTORY_OF_FULL_DUMP] [DIRECTORY_TOP_OPLOGS] [OPLOG_LIMIT]" | |
exit 1 | |
fi | |
FULL_DUMP_TIMESTAMP=`echo $FULL_DUMP_DIRECTORY | cut -d "_" -f 2 | cut -d "/" -f 1` | |
LAST_OPLOG="" | |
ALREADY_APPLIED_OPLOG=0 | |
mkdir -p /tmp/emptyDirForOpRestore | |
for OPLOG in `ls $OPLOGS_DIRECTORY/*.bson`; do | |
OPLOG_TIMESTAMP=`echo $OPLOG | rev | cut -d "/" -f 1 | rev | cut -d "_" -f 1` | |
if [ $OPLOG_TIMESTAMP -gt $FULL_DUMP_TIMESTAMP ]; then | |
if [ $ALREADY_APPLIED_OPLOG -eq 0 ]; then | |
ALREADY_APPLIED_OPLOG=1 | |
echo "applying oplog $LAST_OPLOG" | |
mongorestore -u $MONGODB_USER -p $MONGODB_PWD --authenticationDatabase=admin --oplogFile $LAST_OPLOG --oplogReplay --dir /tmp/emptyDirForOpRestore --oplogLimit=$OPLOG_LIMIT | |
echo "applying oplog $OPLOG" | |
mongorestore -u $MONGODB_USER -p $MONGODB_PWD --authenticationDatabase=admin --oplogFile $OPLOG --oplogReplay --dir /tmp/emptyDirForOpRestore --oplogLimit=$OPLOG_LIMIT | |
else | |
echo "applying oplog $OPLOG" | |
mongorestore -u $MONGODB_USER -p $MONGODB_PWD --authenticationDatabase=admin --oplogFile $OPLOG --oplogReplay --dir /tmp/emptyDirForOpRestore --oplogLimit=$OPLOG_LIMIT | |
fi | |
else | |
LAST_OPLOG=$OPLOG | |
fi | |
done | |
if [ $ALREADY_APPLIED_OPLOG -eq 0 ]; then | |
if [ "$LAST_OPLOG" != "" ]; then | |
echo "applying oplog $LAST_OPLOG" | |
mongorestore -u $MONGODB_USER -p $MONGODB_PWD --authenticationDatabase=admin --oplogFile $LAST_OPLOG --oplogReplay --dir $OPLOGS_DIRECTORY --oplogLimit=$OPLOG_LIMIT | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment