def dump_and_reinit_bbdb(self, data_descriptor):
        '''Dumps the current black box database in self.bb_config_params.default.db_export_dir,
        drops the database, and then reinitialises a new black box database
        with the black box metadata.

        Keyword arguments:
        data_descriptor: str -- descriptive name of the directory in which
                                the data is be exported

        '''
        dump_dir = '{0}/{1}_{2}'.format(
            self.bb_config_params.default.db_export_dir, data_descriptor,
            datetime.datetime.now().isoformat())

        # we dump the black box database
        print('Dumping database {0} to {1}'.format(self.data_logger.db_name,
                                                   dump_dir))
        DBUtils.dump_db(db_name=self.data_logger.db_name,
                        data_dir=dump_dir,
                        delete_db=True)

        # we reinitialise the black box database
        # by writing the metadata
        print('Reinitialising database {0}'.format(self.data_logger.db_name))
        self.data_logger.write_metadata(self.bb_config_params)
        print('Database {0} reinitialised'.format(self.data_logger.db_name))
 def test_dump_db_with_delete(self):
     dump_dir = '/tmp/bb_tools_test_dump_' + str(time.time()).replace('.', '_')
     os.makedirs(dump_dir)
     success = DBUtils.dump_db(
         db_name=self.test_db_name,
         data_dir=dump_dir)
     self.assertTrue(success)
     files = os.listdir(os.path.join(dump_dir, self.test_db_name))
     self.assertSetEqual(set(files) - {'system.indexes.bson'},
                         set(os.listdir(self.test_db_dir)))
     self.assertNotIn(self.test_db_name, self.client.list_database_names())
#!/usr/bin/env python3
import ast
import argparse

from black_box_tools.db_utils import DBUtils

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Dump database (and optionally delete it afterwards)')
    parser.add_argument('-db', help='name of the mongo db', default='logs')
    parser.add_argument('-path', help='directory of the dump', default='.')
    parser.add_argument('-delete', help='name of the mongo db', default='True')

    args = parser.parse_args()
    db_name = args.db
    dump_path = args.path
    delete_db = ast.literal_eval(args.delete)

    print('Dumping database {0} to {1}'.format(db_name, dump_path))
    DBUtils.dump_db(db_name=db_name, data_dir=dump_path, delete_db=delete_db)
    print('{0} successfully dumped to {1}'.format(db_name, dump_path))