Created
June 25, 2015 05:16
-
-
Save tokudu/e3424175e82488a077ca to your computer and use it in GitHub Desktop.
Simple shell script to restore a mongodb databse from an S3 backup using mongorestore
This file contains 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 | |
TIMESTAMP=`date +%F-%H%M` | |
DUMP_LOCAL=/tmp/dump-${TIMESTAMP} | |
# exit if something fails | |
set -e | |
if [ $# -lt 2 ] | |
then | |
echo "Usage: `basename $0` database path_or_url_to_dump" | |
exit 1 | |
fi | |
# get instance address | |
DATABASE=$1 | |
# get the dump location | |
DUMP_LOCATION=$2 | |
mkdir -p $DUMP_LOCAL | |
# add the s3 prefix | |
if [ $DUMP_LOCATION != s3* ] | |
then | |
DUMP_LOCATION=s3://${DUMP_LOCATION} | |
fi | |
echo "Downloading from S3..." | |
s3cmd get -r ${DUMP_LOCATION%/}/ $DUMP_LOCAL | |
echo "Done." | |
pushd $DUMP_LOCAL > /dev/null | |
echo "Combining..." | |
cat * > dump.tar.gz | |
find . -type f -not -name 'dump.tar.gz' | xargs rm | |
echo "Done." | |
echo "Extracting..." | |
tar xvzf dump.tar.gz --strip-components 1 | |
rm -rf dump.tar.gz | |
echo "Done." | |
# work around for https://jira.mongodb.org/browse/SERVER-6947 | |
find $DATABASE/. -name '*.json' -exec sed -i -e 's/, \"[a-z]*\" : { \"\$undefined\" : [a-z]* }//g' "{}" \; | |
perl -pi -e 'chomp if eof' $DATABASE/*.json | |
echo "Restoring" | |
mongorestore -d $DATABASE --drop $DATABASE | |
echo "Restoring" | |
popd > /dev/null | |
rm -rf $DUMP_LOCAL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://gist.github.com/tokudu/1d2ecb7356aca631a984 for the backup script.