88from signal import signal , SIGINT , SIGTERM
99from time import time
1010
11- from Common import DB , Lock
12- from ShardingHandler import ShardingHandler
13- from Mongodumper import Mongodumper
14- from Oplog import OplogTailer , OplogResolver
1511from Archiver import Archiver
12+ from Common import DB , Lock
13+ from Methods import Dumper
1614from Notify import NotifyNSCA
15+ from Oplog import OplogTailer , OplogResolver
16+ from Replset import Replset , ReplsetSharded
17+ from Sharding import Sharding
1718from Upload import UploadS3
1819
1920
@@ -34,7 +35,7 @@ def __init__(self, options):
3435 self .backup_location = None
3536 self .dump_gzip = False
3637 self .balancer_wait_secs = 300
37- self .balancer_sleep = 10
38+ self .balancer_sleep = 5
3839 self .archiver_threads = 1
3940 self .resolver_threads = 1
4041 self .notify_nsca = None
@@ -58,6 +59,7 @@ def __init__(self, options):
5859 self .upload_s3_chunk_size_mb = None
5960 self .archiver = None
6061 self .sharding = None
62+ self .replset = None
6163 self .mongodumper = None
6264 self .oplogtailer = None
6365 self .oplog_resolver = None
@@ -69,6 +71,7 @@ def __init__(self, options):
6971 self .start_time = time ()
7072 self .oplog_threads = []
7173 self .oplog_summary = {}
74+ self .secondaries = {}
7275 self .mongodumper_summary = {}
7376
7477 # Setup options are properies and connection to node
@@ -108,9 +111,9 @@ def __init__(self, options):
108111
109112 # Get a DB connection
110113 try :
111- connection = DB (self .host , self .port , self .user , self .password , self .authdb ). connection ( )
112- self .is_mongos = connection . is_mongos
113- connection .close ()
114+ self . db = DB (self .host , self .port , self .user , self .password , self .authdb )
115+ self .connection = self . db . connection ()
116+ self . is_sharded = self . connection .is_mongos
114117 except Exception , e :
115118 raise e
116119
@@ -140,7 +143,7 @@ def cleanup_and_exit(self, code, frame):
140143 if current_process ().name == "MainProcess" :
141144 logging .info ("Starting cleanup and exit procedure! Killing running threads" )
142145
143- submodules = ['sharding' , 'mongodumper' , 'oplogtailer' , 'archiver' , 'uploader_s3' ]
146+ submodules = ['replset' , ' sharding' , 'mongodumper' , 'oplogtailer' , 'archiver' , 'uploader_s3' ]
144147 for submodule_name in submodules :
145148 submodule = getattr (self , submodule_name )
146149 if submodule :
@@ -152,6 +155,9 @@ def cleanup_and_exit(self, code, frame):
152155 self .backup_name
153156 ))
154157
158+ if self .db :
159+ self .db .close ()
160+
155161 if self ._lock :
156162 self ._lock .release ()
157163
@@ -172,22 +178,37 @@ def run(self):
172178 logging .fatal ("Could not acquire lock! Is another %s process running? Exiting" % self .program_name )
173179 sys .exit (1 )
174180
175- if not self .is_mongos :
181+ if not self .is_sharded :
176182 logging .info ("Running backup of %s:%s in replset mode" % (self .host , self .port ))
177183
178184 self .archiver_threads = 1
179185
186+ # get shard secondary
180187 try :
181- self .mongodumper = Mongodumper (
182- self .host ,
183- self .port ,
188+ self .replset = Replset (
189+ self .db ,
184190 self .user ,
185191 self .password ,
186192 self .authdb ,
193+ self .max_repl_lag_secs
194+ )
195+ secondary = self .replset .find_secondary ()
196+ replset_name = secondary ['replSet' ]
197+
198+ self .secondaries [replset_name ] = secondary
199+ self .replset .close ()
200+ except Exception , e :
201+ self .exception ("Problem getting shard secondaries! Error: %s" % e )
202+
203+ try :
204+ self .mongodumper = Dumper (
205+ self .secondaries ,
187206 self .backup_root_directory ,
188207 self .backup_binary ,
189208 self .dump_gzip ,
190- self .max_repl_lag_secs ,
209+ self .user ,
210+ self .password ,
211+ self .authdb ,
191212 None ,
192213 self .verbose
193214 )
@@ -200,32 +221,48 @@ def run(self):
200221
201222 # connect to balancer and stop it
202223 try :
203- self .sharding = ShardingHandler (
204- self .host ,
205- self .port ,
224+ self .sharding = Sharding (
225+ self .db ,
206226 self .user ,
207227 self .password ,
208228 self .authdb ,
209229 self .balancer_wait_secs ,
210230 self .balancer_sleep
211231 )
212232 self .sharding .get_start_state ()
233+ except Exception , e :
234+ self .exception ("Problem connecting to the balancer! Error: %s" % e )
235+
236+ # get shard secondaries
237+ try :
238+ self .replset = ReplsetSharded (
239+ self .sharding ,
240+ self .db ,
241+ self .user ,
242+ self .password ,
243+ self .authdb ,
244+ self .max_repl_lag_secs
245+ )
246+ self .secondaries = self .replset .find_secondaries ()
247+ except Exception , e :
248+ self .exception ("Problem getting shard secondaries! Error: %s" % e )
249+
250+ # Stop the balancer:
251+ try :
213252 self .sharding .stop_balancer ()
214253 except Exception , e :
215- self .exception ("Problem connecting-to and/or stopping balancer! Error: %s" % e )
254+ self .exception ("Problem stopping the balancer! Error: %s" % e )
216255
217256 # start the oplog tailer threads
218257 if self .no_oplog_tailer :
219258 logging .warning ("Oplog tailing disabled! Skipping" )
220259 else :
221260 try :
222261 self .oplogtailer = OplogTailer (
262+ self .secondaries ,
223263 self .backup_name ,
224264 self .backup_root_directory ,
225- self .host ,
226- self .port ,
227265 self .dump_gzip ,
228- self .max_repl_lag_secs ,
229266 self .user ,
230267 self .password ,
231268 self .authdb
@@ -236,16 +273,14 @@ def run(self):
236273
237274 # start the mongodumper threads
238275 try :
239- self .mongodumper = Mongodumper (
240- self .host ,
241- self .port ,
242- self .user ,
243- self .password ,
244- self .authdb ,
276+ self .mongodumper = Dumper (
277+ self .secondaries ,
245278 self .backup_root_directory ,
246279 self .backup_binary ,
247280 self .dump_gzip ,
248- self .max_repl_lag_secs ,
281+ self .user ,
282+ self .password ,
283+ self .authdb ,
249284 self .sharding .get_configserver (),
250285 self .verbose
251286 )
@@ -312,6 +347,9 @@ def run(self):
312347 except Exception , e :
313348 self .exception ("Problem running NSCA notifier! Error: %s" % e )
314349
350+ if self .db :
351+ self .db .close ()
352+
315353 self ._lock .release ()
316354
317355 logging .info ("Backup completed in %s sec" % self .backup_duration )
0 commit comments