Skip to content

Instantly share code, notes, and snippets.

@sepastian
Last active June 18, 2020 09:36
Show Gist options
  • Save sepastian/5a2185a16b45a0b17e1bdd2332d6c528 to your computer and use it in GitHub Desktop.
Save sepastian/5a2185a16b45a0b17e1bdd2332d6c528 to your computer and use it in GitHub Desktop.
Dump all Mongo collections to JSON files
#!/bin/bash
set -euo pipefail
docker_container=sipts-mongo
db=si-pts
target=$(mktemp -d "${db}_mongoexport_XXX")
echo "Dumping into ${target}."
docker exec "$docker_container" mongo --quiet \
mongodb://localhost/"${db}" \
--eval "db.getCollectionNames().join('\n');" | \
while read c
do
f="${target}/${c}.json"
echo "${c} -> ${f}"
docker exec "$docker_container" \
mongoexport --db "$db" \
--collection="$c" \
--out= > \
"${f}"
done
echo "Dumped collections into ${target}."
#!/bin/bash
set -euo pipefail
docker_container=sipts-mongo
db=si-pts
target=$(mktemp -d "${db}_mongoexport_XXX")
echo "Dumping into ${target}."
docker exec "$docker_container" mongo --quiet \
mongodb://localhost/"${db}" \
--eval "db.getCollectionNames().join('\n');" | \
while read c
do
f="${target}/${c}.json"
echo "${c} -> ${f}"
docker exec "$docker_container" \
mongoexport --db "$db" \
--collection="$c" \
--out= | tee "${f}" | \
xz --compress -T0 --stdout > "${f}.xz"
done
echo "Dumped collections into ${target}."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment