Skip to content

Instantly share code, notes, and snippets.

@danmaispace
Created August 28, 2014 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danmaispace/f42d9ef2747ef520e157 to your computer and use it in GitHub Desktop.
Save danmaispace/f42d9ef2747ef520e157 to your computer and use it in GitHub Desktop.
export mongo all connections with last 100 documents
#!/bin/bash
# 2 parameters (database name, dir to store files).
# http://docs.mongodb.org/manual/reference/program/mongoexport/
# example: ./mongoexport_limit.sh database /data/dump/ 100 username password
if [ ! $1 ]; then
echo " Example of use: $0 database_name [dir_to_store]"
exit 1
fi
db=$1
out_dir=$2
limit=$3
username=$4
password=$5
if [ ! $out_dir ]; then
out_dir="./"
else
mkdir -p $out_dir
fi
tmp_file="fadlfhsdofheinwvw.js"
echo "print('_ ' + db.getCollectionNames())" > $tmp_file
cols=`/home/jesse/sources/mongodb-linux-x86_64-2.6.4/bin/mongo --host localhost $db --username $username --password $password $tmp_file | grep '_' | awk '{print $2}' | tr ',' ' '`
for c in $cols
do
echo "/home/jesse/sources/mongodb-linux-x86_64-2.6.4/bin/mongoexport --host localhost -d $db --username $username --password $password -c $c --sort '{_id: -1}' --limit $limit -o $out_dir/exp_${db}_${c}.json"
/home/jesse/sources/mongodb-linux-x86_64-2.6.4/bin/mongoexport --host localhost -d $db --username $username --password $password -c $c --sort '{_id: -1}' --limit $limit -o "$out_dir/exp_${db}_${c}.json"
done
rm $tmp_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment