def mongorestore(mongo_user, mongo_password, backup_directory_path, drop_database=False, silent=False): """ Warning: Setting drop_database to True will drop the ENTIRE CURRENTLY RUNNING DATABASE before restoring. Mongorestore requires a running mongod process, in addition the provided user must have restore permissions for the database. A mongolia superuser will have more than adequate permissions, but a regular user may not. """ if not path.exists(backup_directory_path): raise Exception("the provided tar directory %s does not exist." % (backup_directory_path)) if silent: mongorestore_command = ( "mongorestore --quiet -u %s -p %s %s" % (mongo_user, mongo_password, backup_directory_path)) else: mongorestore_command = ( "mongorestore -v -u %s -p %s %s" % (mongo_user, mongo_password, backup_directory_path)) if drop_database: mongorestore_command = mongorestore_command + " --drop" call(mongorestore_command, silent=silent)
def mongodump(mongo_user, mongo_password, mongo_dump_directory_path): """ Runs mongodump using the provided credentials on the running mongod process. WARNING: This function will delete the contents of the provided directory before it runs. """ if path.exists(mongo_dump_directory_path): # If a backup dump already exists, delete it rmtree(mongo_dump_directory_path) dump_command = ("mongodump -u %s -p %s -o %s" % (mongo_user, mongo_password, mongo_dump_directory_path)) call(dump_command)
def mongorestore(mongo_user, mongo_password, backup_directory_path, mongo_host="localhost", drop_database=False): """ Warning: Setting drop_database to True will drop the ENTIRE CURRENTLY RUNNING DATABASE before restoring. Mongorestore requires a running mongod process, in addition the provided user must have restore permissions for the database. A mongolia superuser will have more than adequate permissions, but a regular user may not. """ if not path.exists(backup_directory_path): raise Exception("the provided tar directory %s does not exist." % (backup_directory_path)) mongorestore_command = ("mongorestore -v -h %s -u %s -p %s %s" % (mongo_host, mongo_user, mongo_password, backup_directory_path)) if drop_database: mongorestore_command = mongorestore_command + " --drop" call(mongorestore_command)
def mongodump(mongo_user, mongo_password, mongo_dump_directory_path, mongo_host="localhost", mongo_database="", authSource='',useSSL=False, sslPEMPath=''): """ Runs mongodump using the provided credentials on the running mongod process. WARNING: This function will delete the contents of the provided directory before it runs. """ if path.exists(mongo_dump_directory_path): # If a backup dump already exists, delete it rmtree(mongo_dump_directory_path) command_base = 'mongodump -h __HOST__ -o __DESTINATION__' if mongo_user != '': command_base = command_base + ' -u __USERNAME__ -p __PASSWORD__' if mongo_database != '': command_base = command_base + ' -d __DATABASE__ ' if authSource != '': command_base = command_base + ' --authenticationDatabase __AUTHSOURCE__ ' if useSSL: if sslPEMPath != '': command_base = command_base + ' --ssl --sslCAFile __SSLPEMPATH__ ' else: print 'ERROR: sslPEMPath is empty. It will not use ssl.' command_base = command_base.replace('__HOST__', mongo_host) command_base = command_base.replace('__DESTINATION__', mongo_dump_directory_path) command_base = command_base.replace('__USERNAME__', mongo_user) command_base = command_base.replace('__PASSWORD__', mongo_password) command_base = command_base.replace('__DATABASE__', mongo_database) command_base = command_base.replace('__AUTHSOURCE__', authSource) command_base = command_base.replace('__SSLPEMPATH__', sslPEMPath) call(command_base)
def mongodump(mongo_user, mongo_password, mongo_dump_directory_path, database=None, silent=False): """ Runs mongodump using the provided credentials on the running mongod process. WARNING: This function will delete the contents of the provided directory before it runs. """ if path.exists(mongo_dump_directory_path): # If a backup dump already exists, delete it rmtree(mongo_dump_directory_path) if silent: dump_command = ( "mongodump --quiet -u %s -p %s -o %s" % (mongo_user, mongo_password, mongo_dump_directory_path)) else: dump_command = ( "mongodump -u %s -p %s -o %s" % (mongo_user, mongo_password, mongo_dump_directory_path)) if database: dump_command += (" --db %s" % database) call(dump_command, silent=silent)