Skip to content

Instantly share code, notes, and snippets.

@nicklegr
Created August 1, 2023 04:39
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 nicklegr/73d9d0824a8919a9e095e72db2837c9a to your computer and use it in GitHub Desktop.
Save nicklegr/73d9d0824a8919a9e095e72db2837c9a to your computer and use it in GitHub Desktop.
MongoDBをJSON形式でエクスポート
#!/bin/bash
SERVICE_NAME=mongodb
if [ ! $1 ]; then
echo " Example of use: $0 database_name [dir_to_store]"
exit 1
fi
db=$1
out_dir=$2
if [ ! $out_dir ]; then
out_dir="./"
else
mkdir -p $out_dir
fi
tmp_file="fadlfhsdofheinwvw.js"
echo "print('_ ' + db.getCollectionNames())" > $tmp_file
cols=`mongo --host $SERVICE_NAME $db $tmp_file | grep '_' | awk '{print $2}' | tr ',' ' '`
for c in $cols
do
mongoexport --host $SERVICE_NAME -d $db -c $c -o "$out_dir/${db}_${c}.json"
done
rm $tmp_file
# ./backup に export_mongo.sh を配置してから実行
$ docker compose run --rm -it -v `pwd`/backup:/dst mongodb sh
# cd /dst
# ./export_mongo.sh <database_name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment