Skip to content

Instantly share code, notes, and snippets.

@tokudu
Created June 25, 2015 05:16
Show Gist options
  • Save tokudu/e3424175e82488a077ca to your computer and use it in GitHub Desktop.
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
#!/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
@tokudu
Copy link
Author

tokudu commented Jun 25, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment