def mongodb_action(self,deployment): ''' handle all database action insert data delete backup and restore :param deployment: standalone replicaset or sharding :return:OK -1 ''' my_student =student_main.student() while True: os.system("clear") print " a - Add new student " print " d - Delete all students " print " b - Backup db " print " r - Restore db " print " i - Insert all student" print " q - Return to main menu \n" user_choice = raw_input("Enter choice: ") if user_choice == 'q': break elif user_choice == 'a': new_student = { } new_student['student_id']= randint(10000,99999) new_student['first_name'] = raw_input("Enter first name: ") new_student['last_name'] = raw_input("Enter last name: ") new_student['course'] = {} my_student.add_student(new_student) elif user_choice == 'b': if deployment =="standalone": db_log.debug("lock Database") subprocess.call(shlex.split('{} {} {}'.format("mongo", "--eval", "db.fsyncLock()"))) print "*********************lock Database*********************\n" print "******************************************************" time.sleep(5) self.mongo_dump_and_restore(self.STANDALONE_DUMP_PATH, self.LOCALHOST, self.STANDALONE_PORT_27017, self.STANDALONE_DUMP_FILE_PATH) db_log.debug("Unlock Database") print "*********************Unlock Database*********************\n" print "******************************************************" subprocess.call(shlex.split('{} {} {}'.format("mongo", "--eval", "db.fsyncUnlock()"))) time.sleep(5) elif deployment =="replicaset": self.mongo_dump_and_restore(self.REPLICASET_DUMP_PATH, self.LOCALHOST, self.REPLICASET_SEONDARY_PORT_27018, self.REPLICASET_DUMP_FILE_PATH) elif deployment =="sharding": #stop balancer self.stop_blancer() #for each shard backup his secondary db_log.debug( "dump shard 1 port {}".format(self.SHARD_1_SEONDARY_PORT_37018)) self.mongo_dump_and_restore(self.REPLICASET_DUMP_PATH, self.LOCALHOST, self.SHARD_1_SEONDARY_PORT_37018, self.SHARD_1_DUMP_PATH) time.sleep(2) db_log.debug("dump shard 2 port {}".format(self.SHARD_2_SEONDARY_PORT_47018)) self.mongo_dump_and_restore(self.REPLICASET_DUMP_PATH, self.LOCALHOST, self.SHARD_2_SEONDARY_PORT_47018, self.SHARD_2_DUMP_PATH) time.sleep(2) db_log.debug( "dump shard 3 port {}".format(self.SHARD_3_SEONDARY_PORT_57018)) self.mongo_dump_and_restore(self.REPLICASET_DUMP_PATH, self.LOCALHOST, self.SHARD_3_SEONDARY_PORT_57018, self.SHARD_3_DUMP_PATH) time.sleep(2) #backup config server db_log.debug( "dump config server port {}".format(self.SHARD_CONFIG_SERVER_PORT_57040)) self.mongo_dump_and_restore(self.REPLICASET_DUMP_PATH, self.LOCALHOST, self.SHARD_CONFIG_SERVER_PORT_57040, self.CONFIG_SERVER_DUMP_PATH) self.start_blancer() else: db_log.debug( "not valid deployment") exit(1) elif user_choice == 'r': if deployment =="standalone": self.mongo_dump_and_restore(self.STANDALONE_RESTORE_PATH, self.LOCALHOST, self.STANDALONE_PORT_27017, self.STANDALONE_DUMP_FILE_PATH) elif deployment =="replicaset": db_log.debug( "replica set restore parameters {} {} {} {}".format(self.REPLICASET_RESTORE_PATH, self.LOCALHOST, self.REPLICASET_PRIMARY_PORT_27017, self.REPLICASET_DUMP_FILE_PATH)) self.mongo_dump_and_restore(self.REPLICASET_RESTORE_PATH, self.LOCALHOST, self.REPLICASET_PRIMARY_PORT_27017, self.REPLICASET_DUMP_FILE_PATH) elif deployment =="sharding": #for each shard restore primary print self.mongo_dump_and_restore(self.REPLICASET_RESTORE_PATH, self.LOCALHOST, self.SHARD_1_PRIMARY_PORT_37017, self.SHARD_1_DUMP_PATH) self.mongo_dump_and_restore(self.REPLICASET_RESTORE_PATH, self.LOCALHOST, self.SHARD_2_PRIMARY_PORT_47017, self.SHARD_2_DUMP_PATH) self.mongo_dump_and_restore(self.REPLICASET_RESTORE_PATH, self.LOCALHOST, self.SHARD_3_PRIMARY_PORT_57017, self.SHARD_3_DUMP_PATH) self.mongo_dump_and_restore(self.REPLICASET_RESTORE_PATH, self.LOCALHOST, self.SHARD_CONFIG_SERVER_PORT_57040, self.CONFIG_SERVER_DUMP_PATH) else: db_log.error(" Error not valid deployment") exit(1) elif user_choice == 'd': my_student.delete_student_collection() elif user_choice == 'i': my_student.insert_data_to_student_collection() else: db_log.debug( "you did not choose from the above -{} error {} \n".format(user_choice,student_main.ERR_WRONG)) time.sleep(5) return 1
#!/usr/bin/python import json import bottle from bottle import route, run, view,template import sys sys.path.append("/home/uripab/mycode/mongoDB-Backup/db_code/") import student_main my_student =student_main.student() student_column =["student_id","first_name","last_name","course"] @route('/') @route('/Standalone') @view('student') def standalone(): data=my_student.get_student() return dict(title='students',db_type='Standalone',data=data,student_column=student_column) @route('/Replica-set') @view('replica-set') def replicaset(): primary_port = "27017" secendary_port = "27018" primary_data=my_student.get_student_from_replicaset(primary_port) secendary_data=my_student.get_student_from_replicaset(secendary_port) return dict(title='Replica-set',db_type='Replica-set',data=primary_data,secendary_data =secendary_data, student_column=student_column,primary_port=primary_port,secendary_port=secendary_port) @route('/Sharding')
#!/usr/bin/python import json import bottle from bottle import route, run, view, template import sys sys.path.append("/home/uripab/mycode/mongoDB-Backup/db_code/") import student_main my_student = student_main.student() student_column = ["student_id", "first_name", "last_name", "course"] @route('/') @route('/Standalone') @view('student') def standalone(): data = my_student.get_student() return dict(title='students', db_type='Standalone', data=data, student_column=student_column) @route('/Replica-set') @view('replica-set') def replicaset(): primary_port = "27017" secendary_port = "27018" primary_data = my_student.get_student_from_replicaset(primary_port) secendary_data = my_student.get_student_from_replicaset(secendary_port) return dict(title='Replica-set',
def mongodb_action(self, deployment): ''' handle all database action insert data delete backup and restore :param deployment: standalone replicaset or sharding :return:OK -1 ''' my_student = student_main.student() while True: os.system("clear") print " a - Add new student " print " d - Delete all students " print " b - Backup db " print " r - Restore db " print " i - Insert all student" print " q - Return to main menu \n" user_choice = raw_input("Enter choice: ") if user_choice == 'q': break elif user_choice == 'a': new_student = {} new_student['student_id'] = randint(10000, 99999) new_student['first_name'] = raw_input("Enter first name: ") new_student['last_name'] = raw_input("Enter last name: ") new_student['course'] = {} my_student.add_student(new_student) elif user_choice == 'b': if deployment == "standalone": db_log.debug("lock Database") subprocess.call( shlex.split('{} {} {}'.format("mongo", "--eval", "db.fsyncLock()"))) print "*********************lock Database*********************\n" print "******************************************************" time.sleep(5) self.mongo_dump_and_restore(self.STANDALONE_DUMP_PATH, self.LOCALHOST, self.STANDALONE_PORT_27017, self.STANDALONE_DUMP_FILE_PATH) db_log.debug("Unlock Database") print "*********************Unlock Database*********************\n" print "******************************************************" subprocess.call( shlex.split('{} {} {}'.format("mongo", "--eval", "db.fsyncUnlock()"))) time.sleep(5) elif deployment == "replicaset": self.mongo_dump_and_restore( self.REPLICASET_DUMP_PATH, self.LOCALHOST, self.REPLICASET_SEONDARY_PORT_27018, self.REPLICASET_DUMP_FILE_PATH) elif deployment == "sharding": #stop balancer self.stop_blancer() #for each shard backup his secondary db_log.debug("dump shard 1 port {}".format( self.SHARD_1_SEONDARY_PORT_37018)) self.mongo_dump_and_restore( self.REPLICASET_DUMP_PATH, self.LOCALHOST, self.SHARD_1_SEONDARY_PORT_37018, self.SHARD_1_DUMP_PATH) time.sleep(2) db_log.debug("dump shard 2 port {}".format( self.SHARD_2_SEONDARY_PORT_47018)) self.mongo_dump_and_restore( self.REPLICASET_DUMP_PATH, self.LOCALHOST, self.SHARD_2_SEONDARY_PORT_47018, self.SHARD_2_DUMP_PATH) time.sleep(2) db_log.debug("dump shard 3 port {}".format( self.SHARD_3_SEONDARY_PORT_57018)) self.mongo_dump_and_restore( self.REPLICASET_DUMP_PATH, self.LOCALHOST, self.SHARD_3_SEONDARY_PORT_57018, self.SHARD_3_DUMP_PATH) time.sleep(2) #backup config server db_log.debug("dump config server port {}".format( self.SHARD_CONFIG_SERVER_PORT_57040)) self.mongo_dump_and_restore( self.REPLICASET_DUMP_PATH, self.LOCALHOST, self.SHARD_CONFIG_SERVER_PORT_57040, self.CONFIG_SERVER_DUMP_PATH) self.start_blancer() else: db_log.debug("not valid deployment") exit(1) elif user_choice == 'r': if deployment == "standalone": self.mongo_dump_and_restore(self.STANDALONE_RESTORE_PATH, self.LOCALHOST, self.STANDALONE_PORT_27017, self.STANDALONE_DUMP_FILE_PATH) elif deployment == "replicaset": db_log.debug( "replica set restore parameters {} {} {} {}".format( self.REPLICASET_RESTORE_PATH, self.LOCALHOST, self.REPLICASET_PRIMARY_PORT_27017, self.REPLICASET_DUMP_FILE_PATH)) self.mongo_dump_and_restore( self.REPLICASET_RESTORE_PATH, self.LOCALHOST, self.REPLICASET_PRIMARY_PORT_27017, self.REPLICASET_DUMP_FILE_PATH) elif deployment == "sharding": #for each shard restore primary print self.mongo_dump_and_restore( self.REPLICASET_RESTORE_PATH, self.LOCALHOST, self.SHARD_1_PRIMARY_PORT_37017, self.SHARD_1_DUMP_PATH) self.mongo_dump_and_restore( self.REPLICASET_RESTORE_PATH, self.LOCALHOST, self.SHARD_2_PRIMARY_PORT_47017, self.SHARD_2_DUMP_PATH) self.mongo_dump_and_restore( self.REPLICASET_RESTORE_PATH, self.LOCALHOST, self.SHARD_3_PRIMARY_PORT_57017, self.SHARD_3_DUMP_PATH) self.mongo_dump_and_restore( self.REPLICASET_RESTORE_PATH, self.LOCALHOST, self.SHARD_CONFIG_SERVER_PORT_57040, self.CONFIG_SERVER_DUMP_PATH) else: db_log.error(" Error not valid deployment") exit(1) elif user_choice == 'd': my_student.delete_student_collection() elif user_choice == 'i': my_student.insert_data_to_student_collection() else: db_log.debug( "you did not choose from the above -{} error {} \n".format( user_choice, student_main.ERR_WRONG)) time.sleep(5) return 1