@ -183,6 +183,8 @@ class BackupStatistics {
uint32_t number_fail_backup ;
uint32_t number_fail_backup ;
} ;
} ;
// A backup engine for accessing information about backups and restoring from
// them.
class BackupEngineReadOnly {
class BackupEngineReadOnly {
public :
public :
virtual ~ BackupEngineReadOnly ( ) { }
virtual ~ BackupEngineReadOnly ( ) { }
@ -190,9 +192,12 @@ class BackupEngineReadOnly {
static Status Open ( Env * db_env , const BackupableDBOptions & options ,
static Status Open ( Env * db_env , const BackupableDBOptions & options ,
BackupEngineReadOnly * * backup_engine_ptr ) ;
BackupEngineReadOnly * * backup_engine_ptr ) ;
// Returns info about backups in backup_info
// You can GetBackupInfo safely, even with other BackupEngine performing
// You can GetBackupInfo safely, even with other BackupEngine performing
// backups on the same directory
// backups on the same directory
virtual void GetBackupInfo ( std : : vector < BackupInfo > * backup_info ) = 0 ;
virtual void GetBackupInfo ( std : : vector < BackupInfo > * backup_info ) = 0 ;
// Returns info about corrupt backups in corrupt_backups
virtual void GetCorruptedBackups (
virtual void GetCorruptedBackups (
std : : vector < BackupID > * corrupt_backup_ids ) = 0 ;
std : : vector < BackupID > * corrupt_backup_ids ) = 0 ;
@ -200,9 +205,12 @@ class BackupEngineReadOnly {
// running that might call DeleteBackup() or PurgeOldBackups(). It is caller's
// running that might call DeleteBackup() or PurgeOldBackups(). It is caller's
// responsibility to synchronize the operation, i.e. don't delete the backup
// responsibility to synchronize the operation, i.e. don't delete the backup
// when you're restoring from it
// when you're restoring from it
// See also the corresponding doc in BackupEngine
virtual Status RestoreDBFromBackup (
virtual Status RestoreDBFromBackup (
BackupID backup_id , const std : : string & db_dir , const std : : string & wal_dir ,
BackupID backup_id , const std : : string & db_dir , const std : : string & wal_dir ,
const RestoreOptions & restore_options = RestoreOptions ( ) ) = 0 ;
const RestoreOptions & restore_options = RestoreOptions ( ) ) = 0 ;
// See the corresponding doc in BackupEngine
virtual Status RestoreDBFromLatestBackup (
virtual Status RestoreDBFromLatestBackup (
const std : : string & db_dir , const std : : string & wal_dir ,
const std : : string & db_dir , const std : : string & wal_dir ,
const RestoreOptions & restore_options = RestoreOptions ( ) ) = 0 ;
const RestoreOptions & restore_options = RestoreOptions ( ) ) = 0 ;
@ -213,70 +221,37 @@ class BackupEngineReadOnly {
virtual Status VerifyBackup ( BackupID backup_id ) = 0 ;
virtual Status VerifyBackup ( BackupID backup_id ) = 0 ;
} ;
} ;
// Please see the documentation in BackupableDB and RestoreBackupableDB
// A backup engine for creating new backups.
class BackupEngine {
class BackupEngine {
public :
public :
virtual ~ BackupEngine ( ) { }
virtual ~ BackupEngine ( ) { }
// BackupableDBOptions have to be the same as the ones used in previous
// BackupEngines for the same backup directory.
static Status Open ( Env * db_env ,
static Status Open ( Env * db_env ,
const BackupableDBOptions & options ,
const BackupableDBOptions & options ,
BackupEngine * * backup_engine_ptr ) ;
BackupEngine * * backup_engine_ptr ) ;
/// same as CreateNewBackup, but stores extra application metadata
// same as CreateNewBackup, but stores extra application metadata
virtual Status CreateNewBackupWithMetadata (
virtual Status CreateNewBackupWithMetadata (
DB * db , const std : : string & app_metadata , bool flush_before_backup = false ,
DB * db , const std : : string & app_metadata , bool flush_before_backup = false ,
std : : function < void ( ) > progress_callback = [ ] ( ) { } ) = 0 ;
std : : function < void ( ) > progress_callback = [ ] ( ) { } ) = 0 ;
// Captures the state of the database in the latest backup
// NOT a thread safe call
virtual Status CreateNewBackup ( DB * db , bool flush_before_backup = false ,
virtual Status CreateNewBackup ( DB * db , bool flush_before_backup = false ,
std : : function < void ( ) > progress_callback =
std : : function < void ( ) > progress_callback =
[ ] ( ) { } ) {
[ ] ( ) { } ) {
return CreateNewBackupWithMetadata ( db , " " , flush_before_backup ,
return CreateNewBackupWithMetadata ( db , " " , flush_before_backup ,
progress_callback ) ;
progress_callback ) ;
}
}
virtual Status PurgeOldBackups ( uint32_t num_backups_to_keep ) = 0 ;
virtual Status DeleteBackup ( BackupID backup_id ) = 0 ;
virtual void StopBackup ( ) = 0 ;
virtual void GetBackupInfo ( std : : vector < BackupInfo > * backup_info ) = 0 ;
virtual void GetCorruptedBackups (
std : : vector < BackupID > * corrupt_backup_ids ) = 0 ;
virtual Status RestoreDBFromBackup (
BackupID backup_id , const std : : string & db_dir , const std : : string & wal_dir ,
const RestoreOptions & restore_options = RestoreOptions ( ) ) = 0 ;
virtual Status RestoreDBFromLatestBackup (
const std : : string & db_dir , const std : : string & wal_dir ,
const RestoreOptions & restore_options = RestoreOptions ( ) ) = 0 ;
// checks that each file exists and that the size of the file matches our
// expectations. it does not check file checksum.
// Returns Status::OK() if all checks are good
virtual Status VerifyBackup ( BackupID backup_id ) = 0 ;
virtual Status GarbageCollect ( ) = 0 ;
} ;
// Stack your DB with BackupableDB to be able to backup the DB
class BackupableDB : public StackableDB {
public :
// BackupableDBOptions have to be the same as the ones used in a previous
// incarnation of the DB
//
// BackupableDB ownes the pointer `DB* db` now. You should not delete it or
// use it after the invocation of BackupableDB
BackupableDB ( DB * db , const BackupableDBOptions & options ) ;
virtual ~ BackupableDB ( ) ;
// Captures the state of the database in the latest backup
// NOT a thread safe call
Status CreateNewBackup ( bool flush_before_backup = false ) ;
// Returns info about backups in backup_info
void GetBackupInfo ( std : : vector < BackupInfo > * backup_info ) ;
// Returns info about corrupt backups in corrupt_backups
void GetCorruptedBackups ( std : : vector < BackupID > * corrupt_backup_ids ) ;
// deletes old backups, keeping latest num_backups_to_keep alive
// deletes old backups, keeping latest num_backups_to_keep alive
Status PurgeOldBackups ( uint32_t num_backups_to_keep ) ;
virtual Status PurgeOldBackups ( uint32_t num_backups_to_keep ) = 0 ;
// deletes a specific backup
// deletes a specific backup
Status DeleteBackup ( BackupID backup_id ) ;
virtual Status DeleteBackup ( BackupID backup_id ) = 0 ;
// Call this from another thread if you want to stop the backup
// Call this from another thread if you want to stop the backup
// that is currently happening. It will return immediatelly, will
// that is currently happening. It will return immediatelly, will
// not wait for the backup to stop.
// not wait for the backup to stop.
@ -284,62 +259,44 @@ class BackupableDB : public StackableDB {
// return Status::Incomplete(). It will not clean up after itself, but
// return Status::Incomplete(). It will not clean up after itself, but
// the state will remain consistent. The state will be cleaned up
// the state will remain consistent. The state will be cleaned up
// next time you create BackupableDB or RestoreBackupableDB.
// next time you create BackupableDB or RestoreBackupableDB.
void StopBackup ( ) ;
virtual void StopBackup ( ) = 0 ;
// Will delete all the files we don't need anymore
// It will do the full scan of the files/ directory and delete all the
// files that are not referenced.
Status GarbageCollect ( ) ;
private :
BackupEngine * backup_engine_ ;
Status status_ ;
} ;
// Use this class to access information about backups and restore from them
class RestoreBackupableDB {
public :
RestoreBackupableDB ( Env * db_env , const BackupableDBOptions & options ) ;
~ RestoreBackupableDB ( ) ;
// Returns info about backups in backup_info
// Returns info about backups in backup_info
void GetBackupInfo ( std : : vector < BackupInfo > * backup_info ) ;
virtual void GetBackupInfo ( std : : vector < BackupInfo > * backup_info ) = 0 ;
// Returns info about corrupt backups in corrupt_backups
// Returns info about corrupt backups in corrupt_backups
void GetCorruptedBackups ( std : : vector < BackupID > * corrupt_backup_ids ) ;
virtual void GetCorruptedBackups (
std : : vector < BackupID > * corrupt_backup_ids ) = 0 ;
// restore from backup with backup_id
// restore from backup with backup_id
// IMPORTANT -- if options_.share_table_files == true and you restore DB
// IMPORTANT -- if options_.share_table_files == true,
// from some backup that is not the latest, and you start creating new
// options_.share_files_with_checksum == false, you restore DB from some
// backups from the new DB, they will probably fail
// backup that is not the latest, and you start creating new backups from the
// new DB, they will probably fail.
//
//
// Example: Let's say you have backups 1, 2, 3, 4, 5 and you restore 3.
// Example: Let's say you have backups 1, 2, 3, 4, 5 and you restore 3.
// If you add new data to the DB and try creating a new backup now, the
// If you add new data to the DB and try creating a new backup now, the
// database will diverge from backups 4 and 5 and the new backup will fail.
// database will diverge from backups 4 and 5 and the new backup will fail.
// If you want to create new backup, you will first have to delete backups 4
// If you want to create new backup, you will first have to delete backups 4
// and 5.
// and 5.
Status RestoreDBFromBackup ( BackupID backup_id , const std : : string & db_dir ,
virtual Status RestoreDBFromBackup (
const std : : string & wal_dir ,
BackupID backup_id , const std : : string & db_dir , const std : : string & wal_dir ,
const RestoreOptions & restore_options =
const RestoreOptions & restore_options = RestoreOptions ( ) ) = 0 ;
RestoreOptions ( ) ) ;
// restore from the latest backup
// restore from the latest backup
Status RestoreDBFromLatestBackup ( const std : : string & db_dir ,
virtual Status RestoreDBFromLatestBackup (
const std : : string & wal_dir ,
const std : : string & db_dir , const std : : string & wal_dir ,
const RestoreOptions & restore_options =
const RestoreOptions & restore_options = RestoreOptions ( ) ) = 0 ;
RestoreOptions ( ) ) ;
// deletes old backups, keeping latest num_backups_to_keep alive
// checks that each file exists and that the size of the file matches our
Status PurgeOldBackups ( uint32_t num_backups_to_keep ) ;
// expectations. it does not check file checksum.
// deletes a specific backup
// Returns Status::OK() if all checks are good
Status Delete Backup( BackupID backup_id ) ;
virtual Status Verify Backup( BackupID backup_id ) = 0 ;
// Will delete all the files we don't need anymore
// Will delete all the files we don't need anymore
// It will do the full scan of the files/ directory and delete all the
// It will do the full scan of the files/ directory and delete all the
// files that are not referenced.
// files that are not referenced.
Status GarbageCollect ( ) ;
virtual Status GarbageCollect ( ) = 0 ;
private :
BackupEngine * backup_engine_ ;
Status status_ ;
} ;
} ;
} // namespace rocksdb
} // namespace rocksdb