fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
BackupManager.h
1/*
2 * MIT License
3 *
4 * Copyright (c) 2026 Adriano dos Santos Fernandes
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#ifndef FBCPP_BACKUP_MANAGER_H
26#define FBCPP_BACKUP_MANAGER_H
27
28#include "ServiceManager.h"
29#include <cstdint>
30#include <optional>
31#include <stdexcept>
32#include <string>
33#include <vector>
34
35
39namespace fbcpp
40{
44 class BackupOptions final
45 {
46 public:
47 struct BackupFileSpec final
48 {
49 std::string path;
50 std::optional<std::uint64_t> length;
51 };
52
53 public:
57 const std::string& getDatabase() const
58 {
59 return database;
60 }
61
65 BackupOptions& setDatabase(const std::string& value)
66 {
67 database = value;
68 return *this;
69 }
70
74 const std::vector<BackupFileSpec>& getBackupFiles() const
75 {
76 return backupFiles;
77 }
78
82 BackupOptions& addBackupFile(const std::string& value)
83 {
84 backupFiles.emplace_back(value, std::nullopt);
85 return *this;
86 }
87
91 BackupOptions& addBackupFile(const std::string& value, std::uint64_t length)
92 {
93 if (!backupFiles.empty() && !backupFiles.back().length)
94 throw std::invalid_argument{"Cannot add a backup file with length after a backup file without length"};
95
96 backupFiles.emplace_back(value, length);
97 return *this;
98 }
99
103 BackupOptions& setBackupFile(const std::string& value)
104 {
105 backupFiles = {{value, std::nullopt}};
106 return *this;
107 }
108
112 BackupOptions& setBackupFile(const std::string& value, std::uint64_t length)
113 {
114 backupFiles = {{value, length}};
115 return *this;
116 }
117
122 {
123 return verboseOutput;
124 }
125
130 {
131 verboseOutput = std::move(value);
132 return *this;
133 }
134
138 const std::optional<std::uint32_t>& getParallelWorkers() const
139 {
140 return parallelWorkers;
141 }
142
146 BackupOptions& setParallelWorkers(std::uint32_t value)
147 {
148 parallelWorkers = value;
149 return *this;
150 }
151
152 private:
153 std::string database;
154 std::vector<BackupFileSpec> backupFiles;
155 ServiceManager::VerboseOutput verboseOutput;
156 std::optional<std::uint32_t> parallelWorkers;
157 };
158
162 class RestoreOptions final
163 {
164 public:
165 struct DatabaseFileSpec final
166 {
167 std::string path;
168 std::optional<std::uint64_t> length;
169 };
170
171 public:
175 const std::vector<DatabaseFileSpec>& getDatabaseFiles() const
176 {
177 return databaseFiles;
178 }
179
183 RestoreOptions& setDatabase(const std::string& value)
184 {
185 databaseFiles = {{value, std::nullopt}};
186 return *this;
187 }
188
192 RestoreOptions& setDatabase(const std::string& value, std::uint64_t length)
193 {
194 databaseFiles = {{value, length}};
195 return *this;
196 }
197
201 RestoreOptions& addDatabaseFile(const std::string& value)
202 {
203 databaseFiles.emplace_back(value, std::nullopt);
204 return *this;
205 }
206
210 RestoreOptions& addDatabaseFile(const std::string& value, std::uint64_t length)
211 {
212 if (!databaseFiles.empty() && !databaseFiles.back().length)
213 {
214 throw std::invalid_argument{
215 "Cannot add a database file with length after a database file without length"};
216 }
217
218 databaseFiles.emplace_back(value, length);
219 return *this;
220 }
221
225 const std::vector<std::string>& getBackupFiles() const
226 {
227 return backupFiles;
228 }
229
233 RestoreOptions& addBackupFile(const std::string& value)
234 {
235 backupFiles.emplace_back(value);
236 return *this;
237 }
238
242 RestoreOptions& setBackupFile(const std::string& value)
243 {
244 backupFiles = {value};
245 return *this;
246 }
247
251 bool getReplace() const
252 {
253 return replace;
254 }
255
260 {
261 replace = value;
262 return *this;
263 }
264
269 {
270 return verboseOutput;
271 }
272
277 {
278 verboseOutput = std::move(value);
279 return *this;
280 }
281
285 const std::optional<std::uint32_t>& getParallelWorkers() const
286 {
287 return parallelWorkers;
288 }
289
293 RestoreOptions& setParallelWorkers(std::uint32_t value)
294 {
295 parallelWorkers = value;
296 return *this;
297 }
298
299 private:
300 std::vector<DatabaseFileSpec> databaseFiles;
301 std::vector<std::string> backupFiles;
302 bool replace = false;
303 ServiceManager::VerboseOutput verboseOutput;
304 std::optional<std::uint32_t> parallelWorkers;
305 };
306
310 class BackupManager final : public ServiceManager
311 {
312 public:
314
315 public:
319 void backup(const BackupOptions& options);
320
324 void restore(const RestoreOptions& options);
325 };
326} // namespace fbcpp
327
328
329#endif // FBCPP_BACKUP_MANAGER_H
Executes backup and restore operations through the Firebird service manager.
void restore(const RestoreOptions &options)
Runs a restore operation using the provided options.
void backup(const BackupOptions &options)
Runs a backup operation using the provided options.
Represents options used to run a backup operation through the service manager.
const std::vector< BackupFileSpec > & getBackupFiles() const
Returns the configured backup file specifications.
const std::string & getDatabase() const
Returns the database path to be backed up.
BackupOptions & addBackupFile(const std::string &value, std::uint64_t length)
Appends a backup file path with a split length.
BackupOptions & addBackupFile(const std::string &value)
Appends a backup file path.
BackupOptions & setBackupFile(const std::string &value)
Replaces the backup file paths with a single path.
BackupOptions & setVerboseOutput(ServiceManager::VerboseOutput value)
Sets the verbose output callback.
BackupOptions & setParallelWorkers(std::uint32_t value)
Sets the requested number of parallel workers.
BackupOptions & setDatabase(const std::string &value)
Sets the database path to be backed up.
const ServiceManager::VerboseOutput & getVerboseOutput() const
Returns the verbose output callback.
const std::optional< std::uint32_t > & getParallelWorkers() const
Returns the requested number of parallel workers.
BackupOptions & setBackupFile(const std::string &value, std::uint64_t length)
Replaces the backup file paths with a single path and split length.
Represents options used to run a restore operation through the service manager.
RestoreOptions & setReplace(bool value)
Sets whether the target database should be replaced.
bool getReplace() const
Returns whether the target database should be replaced.
RestoreOptions & setDatabase(const std::string &value)
Sets the database path to be restored.
RestoreOptions & addBackupFile(const std::string &value)
Appends a backup file path.
const std::optional< std::uint32_t > & getParallelWorkers() const
Returns the requested number of parallel workers.
const ServiceManager::VerboseOutput & getVerboseOutput() const
Returns the verbose output callback.
RestoreOptions & setDatabase(const std::string &value, std::uint64_t length)
Sets the database path to be restored with a split length.
const std::vector< DatabaseFileSpec > & getDatabaseFiles() const
Returns the configured database file specifications.
RestoreOptions & addDatabaseFile(const std::string &value, std::uint64_t length)
Appends a database file path with a split length.
RestoreOptions & setParallelWorkers(std::uint32_t value)
Sets the requested number of parallel workers.
RestoreOptions & setVerboseOutput(ServiceManager::VerboseOutput value)
Sets the verbose output callback.
RestoreOptions & addDatabaseFile(const std::string &value)
Appends a database file path.
RestoreOptions & setBackupFile(const std::string &value)
Replaces the backup file paths with a single path.
const std::vector< std::string > & getBackupFiles() const
Returns the backup file paths.
Represents a connection to the Firebird service manager.
ServiceManager(Client &client, const ServiceManagerOptions &options={})
Attaches to the service manager specified by the given options.
std::function< void(std::string_view line)> VerboseOutput
Function invoked when a verbose service output line is available.
fb-cpp namespace.
Definition Attachment.h:42