33import logging
44import sys
55from distutils .version import LooseVersion
6- import urllib
6+
77from multiprocessing import Process
88from select import select
99from shutil import rmtree
@@ -144,40 +144,6 @@ def wait(self):
144144 finally :
145145 self ._process .communicate ()
146146
147- def auth_db_insert (self , mongodump_flags ,auth_details = dict ()):
148- # --username/--password/--authdb
149- if self .is_version_gte ("4.2.0" ):
150- if self .authdb and self .authdb != "admin" :
151- logging .debug ("Using database %s for authentication" % self .authdb )
152- auth_details ['authdb' ]= self .authdb
153- else :
154- auth_details ['authdb' ]= "admin"
155- if self .user and self .password :
156- # >= 3.0.2 supports password input via stdin to mask from ps
157- auth_details ['user' ]= urllib .quote_plus (self .user )
158- auth_details ['password' ]= urllib .quote_plus (self .password )
159- return auth_details
160- elif self .is_version_gte ("3.0.2" ):
161- if self .authdb and self .authdb != "admin" :
162- mongodump_flags .append ("--authenticationDatabase=%s" % self .authdb )
163- if self .user and self .password :
164- mongodump_flags .extend ([
165- "--username=%s" % self .user ,
166- "--password=\" \" "
167- ])
168- self .do_stdin_passwd = True
169- return mongodump_flags
170- else :
171- if self .authdb and self .authdb != "admin" :
172- mongodump_flags .append ("--authenticationDatabase=%s" % self .authdb )
173- if self .user and self .password :
174- logging .warning ("Mongodump is too old to set password securely! Upgrade to mongodump >= 3.0.2 to resolve this" )
175- mongodump_flags .extend ([
176- "--username=%s" % self .user ,
177- "--password=%s" % self .password
178- ])
179- return mongodump_flags
180-
181147 def mongodump_cmd (self ):
182148 mongodump_uri = self .uri .get ()
183149 mongodump_cmd = [self .binary ]
@@ -189,21 +155,12 @@ def mongodump_cmd(self):
189155 logging .fatal ("Mongodump must be >= 3.6.0 to use mongodb+srv:// URIs" )
190156 sys .exit (1 )
191157 mongodump_flags .append ("--host=%s" % self .uri .url )
192- ## TODO we can support compress with srv too ,but as of 22feb we donot require the feature can be a point for improvement
193- elif self .is_version_gte ("4.2.0" ):
194- mongo_url_port = ":" .join ([mongodump_uri .host ,str (mongodump_uri .port )])
195- auth_details = self .auth_db_insert (mongodump_flags )
196- parsed_uri = 'mongodb://%s:%s@%s/?authSource=%s&compressors=snappy,zlib,zstd&readPreference=secondary' % (urllib .quote_plus (auth_details ['user' ]), urllib .quote_plus (auth_details ['password' ]), mongo_url_port , auth_details ['authdb' ])
197- mongodump_flags .extend ([
198- "--uri=%s" % parsed_uri ,
199- ])
200158 else :
201159 mongodump_flags .extend ([
202160 "--host=%s" % mongodump_uri .host ,
203161 "--port=%s" % str (mongodump_uri .port )
204162 ])
205163
206-
207164 if self .oplog_enabled_parse ():
208165 mongodump_flags .extend ([
209166 "--oplog"
@@ -212,11 +169,11 @@ def mongodump_cmd(self):
212169 mongodump_flags .extend ([
213170 "--out=%s/dump" % self .backup_dir
214171 ])
215- # if self.is_version_gte("4.2.0"):
216- # logging.info("MongoDump Version higher that 4.2.0 found extendingmongodump with snappy compressor flag")
217- # mongodump_flags.extend([
218- # "--compressor =%s" % "snappy"
219- # ])
172+ if self .is_version_gte ("4.2.0" ):
173+ logging .info ("MongoDump Version higher that 4.2.0 found extendingmongodump with snppy compressor flag" )
174+ mongodump_flags .extend ([
175+ "--compressors =%s" % "snappy"
176+ ])
220177
221178 # --numParallelCollections
222179 if self .threads > 0 :
@@ -235,11 +192,24 @@ def mongodump_cmd(self):
235192 logging .fatal ("Mongodump must be >= 3.2.0 to set read preference!" )
236193 sys .exit (1 )
237194
238- if not self .is_version_gte ("4.2.0" ):
239- # this will add auth to non srv url and which are above 4.2.0
240- mongodump_flags = self .auth_db_insert (mongodump_flags )
241-
242-
195+ # --username/--password/--authdb
196+ if self .authdb and self .authdb != "admin" :
197+ logging .debug ("Using database %s for authentication" % self .authdb )
198+ mongodump_flags .append ("--authenticationDatabase=%s" % self .authdb )
199+ if self .user and self .password :
200+ # >= 3.0.2 supports password input via stdin to mask from ps
201+ if self .is_version_gte ("3.0.2" ):
202+ mongodump_flags .extend ([
203+ "--username=%s" % self .user ,
204+ "--password=\" \" "
205+ ])
206+ self .do_stdin_passwd = True
207+ else :
208+ logging .warning ("Mongodump is too old to set password securely! Upgrade to mongodump >= 3.0.2 to resolve this" )
209+ mongodump_flags .extend ([
210+ "--username=%s" % self .user ,
211+ "--password=%s" % self .password
212+ ])
243213
244214 # --ssl
245215 if self .do_ssl ():
@@ -260,7 +230,7 @@ def mongodump_cmd(self):
260230 mongodump_cmd .extend (mongodump_flags )
261231 logging .info ("-----mongodump_cmd: %s" % mongodump_cmd )
262232 return mongodump_cmd
263-
233+
264234 def run (self ):
265235 logging .info ("Starting mongodump backup of %s" % self .uri )
266236
0 commit comments