Created
August 1, 2023 04:39
-
-
Save nicklegr/73d9d0824a8919a9e095e72db2837c9a to your computer and use it in GitHub Desktop.
MongoDBをJSON形式でエクスポート
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 | |
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 |
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
# ./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