22
22
23
23
while getopts u:p: OPT; do
24
24
case " ${OPT} " in
25
- u) AUTH_OPTS =" -u ${OPTARG} "
25
+ u) MONGO_AUTH_OPTS =" -u ${OPTARG} "
26
26
;;
27
- p) AUTH_OPTS +=" -p ${OPTARG} "
27
+ p) MONGO_AUTH_OPTS +=" -p ${OPTARG} "
28
28
;;
29
29
esac
30
30
done
@@ -36,17 +36,17 @@ shift 2
36
36
37
37
case " ${ACTION} " in
38
38
# Action:
39
- # import
39
+ # import-collection
40
40
# Description:
41
41
# Import data to a collection from a JSON file.
42
42
# Usage:
43
- # ... import <json_file_name> [collection_name]
44
- # collection name : collection name in the database ; default is
45
- # the JSON file name without the extension
43
+ # ... import-collection <json_file_name> [collection_name]
44
+ # [collection_name] : name of collection to import ; default is
45
+ # JSON file name
46
46
# Note:
47
47
# If needing to import multiple MongoDB documents in a single JSON array,
48
48
# append option "--jsonArray" to the end of the mongoimport command.
49
- ' import' )
49
+ ' import-collection ' )
50
50
[ " $# " -eq 0 ] && \
51
51
echo " mongodbutils: no directory specified" 1>&2 && \
52
52
exit 1
@@ -57,19 +57,19 @@ case "${ACTION}" in
57
57
if [ " $# " -ge 2 ]; then
58
58
opts+=" --collection ${2} "
59
59
fi
60
- mongoimport --db " ${DB_NAME} " ${AUTH_OPTS } --file " ${1} " ${opts}
60
+ mongoimport --db " ${DB_NAME} " ${MONGO_AUTH_OPTS } --file " ${1} " ${opts}
61
61
;;
62
62
63
63
# Action:
64
- # export
64
+ # export-collection
65
65
# Description:
66
66
# Export data from a collection to a JSON file.
67
67
# Usage:
68
- # ... export <collection_name> [directory]
69
- # collection name : collection name in the database
70
- # directory: directory to output JSON file to; default is
71
- # current
72
- ' export' )
68
+ # ... export-collection <collection_name> [directory]
69
+ # <collection_name> : name of the collection to export
70
+ # [ directory] : directory to output JSON file to; default
71
+ # is current
72
+ ' export-collection ' )
73
73
if [ " $# " -ge 1 ]; then
74
74
collName=" ${1} "
75
75
opts+=" --collection ${collName} "
@@ -80,26 +80,40 @@ case "${ACTION}" in
80
80
opts+=" --out ${collName} .json"
81
81
fi
82
82
fi
83
- mongoexport --db " ${DB_NAME} " ${AUTH_OPTS } ${opts} --jsonArray
83
+ mongoexport --db " ${DB_NAME} " ${MONGO_AUTH_OPTS } ${opts} --jsonArray
84
84
;;
85
85
86
86
# Action:
87
- # drop
87
+ # drop-collection
88
+ # Description:
89
+ # Drop a collection
90
+ # Usage:
91
+ # ... drop-collection <collection_name>
92
+ # <collection_name>: the name of the collection to drop
93
+ ' drop-collection' )
94
+ [ " $# " -eq 0 ] && \
95
+ echo " mongodbutils: no collection specified" 1>&2 && \
96
+ exit 1
97
+ mongo " ${DB_NAME} " --eval " db.${1} .drop()"
98
+ ;;
99
+
100
+ # Action:
101
+ # drop-database
88
102
# Description:
89
103
# Drop the database.
90
104
# Usage:
91
- # ... drop
92
- ' drop' )
105
+ # ... drop-database
106
+ ' drop-database ' )
93
107
mongo " ${DB_NAME} " --eval ' db.dropDatabase()'
94
108
;;
95
109
96
110
# Action:
97
- # drop_all
111
+ # drop-all-databases
98
112
# Description:
99
113
# Drop all databases.
100
114
# Usage:
101
- # ... drop_all
102
- ' drop_all ' )
115
+ # ... drop-all-databases
116
+ ' drop-all-databases ' )
103
117
/usr/bin/mongo << -EOF
104
118
db.getMongo().getDBNames().forEach(function(database) {
105
119
if (database != 'local' && database != 'admin') {
@@ -108,23 +122,7 @@ case "${ACTION}" in
108
122
print('dropped database ' + db.getName());
109
123
}
110
124
})
111
- EOF
112
- ;;
113
-
114
- # Action:
115
- # restore
116
- # Description:
117
- # Restore data from one or more BSON files.
118
- # Usage:
119
- # ... restore <bson_file_or_directory_containing_bson_files>
120
- ' restore' )
121
- [ -z " ${1} " ] && \
122
- echo " mongodbutils: no directory specified" 1>&2 && \
123
- exit 1
124
- [ ! -d " ${1} " ] && \
125
- echo " mongodbutils: directory \" ${1} \" does not exist" 1>&2 && \
126
- exit 1
127
- mongorestore ${AUTH_OPTS} " ${1} "
125
+ EOF
128
126
;;
129
127
130
128
# Action:
@@ -133,19 +131,34 @@ case "${ACTION}" in
133
131
# Dump the database to BSON files.
134
132
# Usage:
135
133
# ... dump [collection_name] [directory]
136
- # collection name : collection name in the database; default is
137
- # all
138
- # directory: directory to output BSON files to; default is
139
- # current
134
+ # [collection_name] : name of collection to dump; if no name is
135
+ # specified, all collections will be dumped
136
+ # [ directory] : directory to output BSON files to; default
137
+ # is current
140
138
'dump')
141
139
if [ "$# " -ge 1 ]; then
142
140
opts+="--collection ${1} "
143
-
144
141
if [ "$# " -ge 2 ]; then
145
142
opts=" --out ${2} "
146
143
fi
147
144
fi
148
- mongodump --db " ${DB_NAME} " ${AUTH_OPTS} ${opts}
145
+ mongodump --db "${DB_NAME} " ${MONGO_AUTH_OPTS} ${opts}
146
+ ;;
147
+
148
+ # Action:
149
+ # restore
150
+ # Description:
151
+ # Restore data from one or more BSON files.
152
+ # Usage:
153
+ # ... restore <bson_file_or_directory_containing_bson_files>
154
+ 'restore')
155
+ [ -z "${1} " ] && \
156
+ echo "mongodbutils: no directory specified" 1>&2 && \
157
+ exit 1
158
+ [ ! -d "${1} " ] && \
159
+ echo "mongodbutils: directory \"${1} \" does not exist" 1>&2 && \
160
+ exit 1
161
+ mongorestore ${MONGO_AUTH_OPTS} "${1} "
149
162
;;
150
163
151
164
*)
0 commit comments