From e3aef69f380c893c2ae6a8ce64aade3ae8b5f2b4 Mon Sep 17 00:00:00 2001 From: Rohit Joshi Date: Sun, 24 Feb 2019 20:53:59 -0500 Subject: [PATCH] issue#135 Expose API to restore database from backups --- src/backup.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/backup.rs b/src/backup.rs index 56901a0..f53e918 100644 --- a/src/backup.rs +++ b/src/backup.rs @@ -78,6 +78,60 @@ impl BackupEngine { Ok(()) } } + /// Restore from the latest backup + /// db_dir: input db directory + /// wal_dir: input wal directory + /// opts: restore options + /// example: + /// use rocksdb::backup::{BackupEngine, BackupEngineOptions}; + /// let backup_opts = BackupEngineOptions::default(); + /// let backup_engine = BackupEngine::open(&backup_opts, &backup_path).unwrap(); + /// let mut restore_option = rocksdb::backup::RestoreOptions::default(); + /// restore_option.set_keep_log_files(true); /// true to keep log files + /// if let Err(e) = backup_engine.restore_from_latest_backup(&db_path, &wal_dir, &restore_option) { + /// error!("Failed to restore from the backup. Error:{:?}", e); + /// return Err(e.to_string()); + /// } + pub fn restore_from_latest_backup>( + &mut self, + db_dir: P, + wal_dir: P, + opts: &RestoreOptions, + ) -> Result<(), Error> { + let db_dir = db_dir.as_ref(); + let c_db_dir = match CString::new(db_dir.to_string_lossy().as_bytes()) { + Ok(c) => c, + Err(_) => { + return Err(Error::new( + "Failed to convert db_dir to CString \ + when restoring from latest backup" + .to_owned(), + )); + } + }; + + let wal_dir = wal_dir.as_ref(); + let c_wal_dir = match CString::new(wal_dir.to_string_lossy().as_bytes()) { + Ok(c) => c, + Err(_) => { + return Err(Error::new( + "Failed to convert wal_dir to CString \ + when restoring from latest backup" + .to_owned(), + )); + } + }; + + unsafe { + ffi_try!(ffi::rocksdb_backup_engine_restore_db_from_latest_backup( + self.inner, + c_db_dir.as_ptr(), + c_wal_dir.as_ptr(), + opts.inner, + )); + } + Ok(()) + } } impl BackupEngineOptions {