Last active
October 3, 2023 01:55
-
-
Save EyalAr/67791f51fce51ed55594 to your computer and use it in GitHub Desktop.
Shell script to easily export mongo collections into JSON files
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
#!/usr/bin/env bash | |
#https://gist.github.com/EyalAr/67791f51fce51ed55594 | |
COLLECTIONS=() | |
HOST="127.0.0.1" | |
PORT="27017" | |
DB= | |
if [ -n "$1" -a "$1" = "--help" ] | |
then | |
echo "Usage: $0 -h hostname -p port - d database -c collection" | |
echo | |
echo " hostname Address of the mongo server. Defaults to 127.0.0.1" | |
echo " port Port of the mongo server. Defaults to 27017" | |
echo " database Name of the database." | |
echo " collection Name of collection to backup." | |
echo | |
echo "Option -c may be specified multiple times for multiple collections." | |
echo | |
echo "JSON files will be created in the current directory," | |
echo "One for each collection." | |
echo | |
echo "Example: $0 -h 192.168.1.154 -d dev -c products -c prices -c companies" | |
exit | |
fi | |
while getopts "h:p:d:c:" flag | |
do | |
case $flag in | |
h) HOST="$OPTARG" ;; | |
p) PORT="$OPTARG" ;; | |
d) DB="$OPTARG" ;; | |
c) COLLECTIONS+=("$OPTARG") ;; | |
esac | |
done | |
if [ -z "$HOST" ] | |
then | |
echo "Please specify a host (-h). Type '$0 --help' for help." | |
exit | |
fi | |
if [ -z "$PORT" ] | |
then | |
echo "Please specify a port (-p). Type '$0 --help' for help." | |
exit | |
fi | |
if [ -z "$DB" ] | |
then | |
echo "Please specify a database (-d). Type '$0 --help' for help." | |
exit | |
fi | |
if [ -z "$COLLECTIONS" ] | |
then | |
echo "Please specify at least one collection to export (-c)." | |
echo "Type '$0 --help' for help." | |
exit | |
fi | |
for c in ${COLLECTIONS[@]} | |
do | |
echo "Exporting $HOST:$PORT/$DB/$c to $c.json..." | |
mongoexport --host $HOST --port $PORT --db $DB --collection $c > $c.json | |
done | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment