public void backup() { resetDbLock.readLock().lock(); if (logger.isTraceEnabled()) logger.trace("~> RocksDbDataSource.backup(): " + name); Path path = backupPath(); path.toFile().mkdirs(); try (BackupableDBOptions backupOptions = new BackupableDBOptions(path.toString()); BackupEngine backups = BackupEngine.open(Env.getDefault(), backupOptions)) { backups.createNewBackup(db, true); if (logger.isTraceEnabled()) logger.trace("<~ RocksDbDataSource.backup(): " + name + " done"); } catch (RocksDBException e) { logger.error("Failed to backup database '{}'", name, e); hintOnTooManyOpenFiles(e); throw new RuntimeException(e); } finally { resetDbLock.readLock().unlock(); } }
try (BackupableDBOptions backupOptions = new BackupableDBOptions(backupPath().toString()); RestoreOptions restoreOptions = new RestoreOptions(false); BackupEngine backups = BackupEngine.open(Env.getDefault(), backupOptions)) { if (!backups.getBackupInfo().isEmpty()) { backups.restoreDbFromLatestBackup(getPath().toString(), getPath().toString(), restoreOptions);
.setShareTableFiles(true) .setMaxBackgroundOperations(parallelism); BackupEngine backupEngine = BackupEngine.open(Env.getDefault(), backupOptions); List<BackupInfo> backupInfo = backupEngine.getBackupInfo(); if(backupInfo.size() > 0) { delete(); backupEngine.restoreDbFromLatestBackup(dbUri.getPath(), dbUri.getPath(), restoreOptions); backupEngine.purgeOldBackups(backupsToKeep); backupEngine.close(); backupOptions.close(); restoreOptions.close();
/** * Backups the DB to a local path. * @param backupPath - The local backup path */ protected void backup(String backupPath) throws RocksDBException { File file = new File(backupPath); if(!file.exists()) file.mkdir(); BackupableDBOptions backupOptions = new BackupableDBOptions(backupPath) .setShareTableFiles(true) .setMaxBackgroundOperations(parallelism); BackupEngine backupEngine = BackupEngine.open(Env.getDefault(), backupOptions); backupEngine.createNewBackup(rocksDB, true); backupEngine.purgeOldBackups(backupsToKeep); backupEngine.close(); backupOptions.close(); }
/** * Gets information about the available * backups * * @return A list of information about each available backup */ public List<BackupInfo> getBackupInfo() { assert (isOwningHandle()); return getBackupInfo(nativeHandle_); }
/** * Opens a new Backup Engine * * @param env The environment that the backup engine should operate within * @param options Any options for the backup engine * * @return A new BackupEngine instance * @throws RocksDBException thrown if the backup engine could not be opened */ public static BackupEngine open(final Env env, final BackupableDBOptions options) throws RocksDBException { return new BackupEngine(open(env.nativeHandle_, options.nativeHandle_)); }
/** * Restore the database from the latest backup * * @param dbDir The directory to restore the backup to, i.e. where your * database is * @param walDir The location of the log files for your database, often the * same as dbDir * @param restoreOptions Options for controlling the restore * * @throws RocksDBException thrown if the database could not be restored */ public void restoreDbFromLatestBackup( final String dbDir, final String walDir, final RestoreOptions restoreOptions) throws RocksDBException { assert (isOwningHandle()); restoreDbFromLatestBackup(nativeHandle_, dbDir, walDir, restoreOptions.nativeHandle_); }
/** * Captures the state of the database in the latest backup * * @param db The database to backup * @param flushBeforeBackup When true, the Backup Engine will first issue a * memtable flush and only then copy the DB files to * the backup directory. Doing so will prevent log * files from being copied to the backup directory * (since flush will delete them). * When false, the Backup Engine will not issue a * flush before starting the backup. In that case, * the backup will also include log files * corresponding to live memtables. The backup will * always be consistent with the current state of the * database regardless of the flushBeforeBackup * parameter. * * Note - This method is not thread safe * * @throws RocksDBException thrown if a new backup could not be created */ public void createNewBackup( final RocksDB db, final boolean flushBeforeBackup) throws RocksDBException { assert (isOwningHandle()); createNewBackup(nativeHandle_, db.nativeHandle_, flushBeforeBackup); }
/** * Deletes old backups, keeping just the latest numBackupsToKeep * * @param numBackupsToKeep The latest n backups to keep * * @throws RocksDBException thrown if the old backups could not be deleted */ public void purgeOldBackups( final int numBackupsToKeep) throws RocksDBException { assert (isOwningHandle()); purgeOldBackups(nativeHandle_, numBackupsToKeep); }
/** * Captures the state of the database in the latest backup * * Just a convenience for {@link #createNewBackup(RocksDB, boolean)} with * the flushBeforeBackup parameter set to false * * @param db The database to backup * * Note - This method is not thread safe * * @throws RocksDBException thrown if a new backup could not be created */ public void createNewBackup(final RocksDB db) throws RocksDBException { createNewBackup(db, false); }
/** * Deletes a backup * * @param backupId The id of the backup to delete * * @throws RocksDBException thrown if the backup could not be deleted */ public void deleteBackup(final int backupId) throws RocksDBException { assert (isOwningHandle()); deleteBackup(nativeHandle_, backupId); }
/** * Captures the state of the database in the latest backup along with * application specific metadata. * * @param db The database to backup * @param metadata Application metadata * @param flushBeforeBackup When true, the Backup Engine will first issue a * memtable flush and only then copy the DB files to * the backup directory. Doing so will prevent log * files from being copied to the backup directory * (since flush will delete them). * When false, the Backup Engine will not issue a * flush before starting the backup. In that case, * the backup will also include log files * corresponding to live memtables. The backup will * always be consistent with the current state of the * database regardless of the flushBeforeBackup * parameter. * * Note - This method is not thread safe * * @throws RocksDBException thrown if a new backup could not be created */ public void createNewBackupWithMetadata(final RocksDB db, final String metadata, final boolean flushBeforeBackup) throws RocksDBException { assert (isOwningHandle()); createNewBackupWithMetadata(nativeHandle_, db.nativeHandle_, metadata, flushBeforeBackup); }