def __init__( self, url: Union[str, URL], app: AppT, table: CollectionT, *, key_index_size: Optional[int] = None, options: Optional[Mapping[str, Any]] = None, read_only: Optional[bool] = False, **kwargs: Any, ) -> None: if rocksdb is None: error = ImproperlyConfigured( "RocksDB bindings not installed? pip install python-rocksdb" ) try: import rocksdb as _rocksdb # noqa: F401 except Exception as exc: # pragma: no cover raise error from exc else: # pragma: no cover raise error super().__init__(url, app, table, **kwargs) if not self.url.path: self.url /= self.table_name self.options = options or {} self.read_only = self.options.pop("read_only", read_only) self.rocksdb_options = RocksDBOptions(**self.options) if key_index_size is None: key_index_size = app.conf.table_key_index_size self.key_index_size = key_index_size self._dbs = {} self._key_index = LRUCache(limit=self.key_index_size) self.db_lock = asyncio.Lock() self.rebalance_ack = False self._backup_path = os.path.join(self.path, f"{str(self.basename)}-backups") try: self._backup_engine = None if not os.path.isdir(self._backup_path): os.makedirs(self._backup_path, exist_ok=True) testfile = tempfile.TemporaryFile(dir=self._backup_path) testfile.close() except PermissionError: self.log.warning( f'Unable to make directory for path "{self._backup_path}",' f"disabling backups." ) except OSError: self.log.warning( f'Unable to create files in "{self._backup_path}",' f"disabling backups" ) else: self._backup_engine = rocksdb.BackupEngine(self._backup_path)
def main(argv): inputfile = '' try: opts, args = getopt.getopt(argv,"hd:o:",["database="]) except getopt.GetoptError: print 'rocksdb_backup.py -d database_name [e]' sys.exit(2) for opt, arg in opts: if opt == '-h': print 'rocksdb_backup.py -d database_name ' sys.exit() elif opt in ("-d", "--database"): inputfile = arg print 'Database to be backed up', inputfile db = rocksdb.DB(inputfile, rocksdb.Options(create_if_missing=False)) backup = rocksdb.BackupEngine("./backups.bk") backup.create_backup(db, flush_before_backup=True) print 'Database ', inputfile, ' has been successfully backed up ', os.getcwd(), '/backups.bk' else: assert False, "Invalid Options"
import rocksdb db = rocksdb.DB('/db/db', rocksdb.Options()) backup = rocksdb.BackupEngine("/pyrocksdb_backups") backup.create_backup(db, flush_before_backup=True)
def restore_latest_backup(self, path): backup = rocksdb.BackupEngine(path) self.close() r = backup.restore_latest_backup(self.data_dir, self.data_dir) self.rdb = self.open() return r
def purge_old_backups(self, path, num_backups_to_keep): backup = rocksdb.BackupEngine(path) self.close() r = backup.purge_old_backups(num_backups_to_keep) self.rdb = self.open() return r
def restore_backup(self, path, backup_id): backup = rocksdb.BackupEngine(path) return backup.restore_backup(backup_id, self.data_dir, self.data_dir)
def get_backup_info(self, path): backup = rocksdb.BackupEngine(path) return backup.get_backup_info()
def delete_backup(self, path, backup_id): backup = rocksdb.BackupEngine(path) return backup.delete_backup(backup_id)
def stop_backup(self, path): backup = rocksdb.BackupEngine(path) return backup.stop_backup()
def create_backup(self, path): backup = rocksdb.BackupEngine(path) return backup.create_backup(self.rdb, flush_before_backup=True)