|
| 1 | +// Copyright (c) 2013, Facebook, Inc. All rights reserved. |
| 2 | +// This source code is licensed under the BSD-style license found in the |
| 3 | +// LICENSE file in the root directory of this source tree. An additional grant |
| 4 | +// of patent rights can be found in the PATENTS file in the same directory. |
| 5 | +// |
| 6 | +// Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
| 7 | +// Use of this source code is governed by a BSD-style license that can be |
| 8 | +// found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 9 | + |
| 10 | +#pragma once |
| 11 | +#include "utilities/stackable_db.h" |
| 12 | +#include "rocksdb/env.h" |
| 13 | +#include "rocksdb/status.h" |
| 14 | + |
| 15 | +#include <string> |
| 16 | +#include <map> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +namespace rocksdb { |
| 20 | + |
| 21 | +struct BackupableDBOptions { |
| 22 | + // Where to keep the backup files. Has to be different than dbname_ |
| 23 | + // Best to set this to dbname_ + "/backups" |
| 24 | + // Required |
| 25 | + std::string backup_dir; |
| 26 | + |
| 27 | + // Backup Env object. It will be used for backup file I/O. If it's |
| 28 | + // nullptr, backups will be written out using DBs Env. If it's |
| 29 | + // non-nullptr, backup's I/O will be performed using this object. |
| 30 | + // If you want to have backups on HDFS, use HDFS Env here! |
| 31 | + // Default: nullptr |
| 32 | + Env* backup_env; |
| 33 | + |
| 34 | + // Backup info and error messages will be written to info_log |
| 35 | + // if non-nullptr. |
| 36 | + // Default: nullptr |
| 37 | + Logger* info_log; |
| 38 | + |
| 39 | + // If sync == true, we can guarantee you'll get consistent backup even |
| 40 | + // on a machine crash/reboot. Backup process is slower with sync enabled. |
| 41 | + // If sync == false, we don't guarantee anything on machine reboot. However, |
| 42 | + // chances are some of the backups are consistent. |
| 43 | + // Default: true |
| 44 | + bool sync; |
| 45 | + |
| 46 | + // If true, it will delete whatever backups there are already |
| 47 | + // Default: false |
| 48 | + bool destroy_old_data; |
| 49 | + |
| 50 | + explicit BackupableDBOptions(const std::string& _backup_dir, |
| 51 | + Env* _backup_env = nullptr, |
| 52 | + Logger* _info_log = nullptr, |
| 53 | + bool _sync = true, |
| 54 | + bool _destroy_old_data = false) : |
| 55 | + backup_dir(_backup_dir), |
| 56 | + backup_env(_backup_env), |
| 57 | + info_log(_info_log), |
| 58 | + sync(_sync), |
| 59 | + destroy_old_data(_destroy_old_data) { } |
| 60 | +}; |
| 61 | + |
| 62 | +class BackupEngine; |
| 63 | + |
| 64 | +typedef uint32_t BackupID; |
| 65 | + |
| 66 | +struct BackupInfo { |
| 67 | + BackupID backup_id; |
| 68 | + int64_t timestamp; |
| 69 | + uint64_t size; |
| 70 | + |
| 71 | + BackupInfo() {} |
| 72 | + BackupInfo(BackupID _backup_id, int64_t _timestamp, uint64_t _size) |
| 73 | + : backup_id(_backup_id), timestamp(_timestamp), size(_size) {} |
| 74 | +}; |
| 75 | + |
| 76 | +// Stack your DB with BackupableDB to be able to backup the DB |
| 77 | +class BackupableDB : public StackableDB { |
| 78 | + public: |
| 79 | + // BackupableDBOptions have to be the same as the ones used in a previous |
| 80 | + // incarnation of the DB |
| 81 | + BackupableDB(DB* db, const BackupableDBOptions& options); |
| 82 | + virtual ~BackupableDB(); |
| 83 | + |
| 84 | + // Captures the state of the database in the latest backup |
| 85 | + // NOT a thread safe call |
| 86 | + Status CreateNewBackup(bool flush_before_backup = false); |
| 87 | + // Returns info about backups in backup_info |
| 88 | + void GetBackupInfo(std::vector<BackupInfo>* backup_info); |
| 89 | + // deletes old backups, keeping latest num_backups_to_keep alive |
| 90 | + Status PurgeOldBackups(uint32_t num_backups_to_keep); |
| 91 | + // deletes a specific backup |
| 92 | + Status DeleteBackup(BackupID backup_id); |
| 93 | + |
| 94 | + private: |
| 95 | + BackupEngine* backup_engine_; |
| 96 | +}; |
| 97 | + |
| 98 | +// Use this class to access information about backups and restore from them |
| 99 | +class RestoreBackupableDB { |
| 100 | + public: |
| 101 | + RestoreBackupableDB(Env* db_env, const BackupableDBOptions& options); |
| 102 | + ~RestoreBackupableDB(); |
| 103 | + |
| 104 | + // Returns info about backups in backup_info |
| 105 | + void GetBackupInfo(std::vector<BackupInfo>* backup_info); |
| 106 | + |
| 107 | + // restore from backup with backup_id |
| 108 | + // IMPORTANT -- if you restore from some backup that is not the latest, |
| 109 | + // you HAVE to delete all the newer backups immediately, before creating |
| 110 | + // new backup on the restored database. Otherwise, your new backups |
| 111 | + // will be corrupted. |
| 112 | + // TODO should we enforce this somehow? |
| 113 | + Status RestoreDBFromBackup(BackupID backup_id, const std::string& db_dir, |
| 114 | + const std::string& wal_dir); |
| 115 | + |
| 116 | + // restore from the latest backup |
| 117 | + Status RestoreDBFromLatestBackup(const std::string& db_dir, |
| 118 | + const std::string& wal_dir); |
| 119 | + // deletes old backups, keeping latest num_backups_to_keep alive |
| 120 | + Status PurgeOldBackups(uint32_t num_backups_to_keep); |
| 121 | + // deletes a specific backup |
| 122 | + Status DeleteBackup(BackupID backup_id); |
| 123 | + |
| 124 | + private: |
| 125 | + BackupEngine* backup_engine_; |
| 126 | +}; |
| 127 | + |
| 128 | +} // rocksdb namespace |
0 commit comments