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 "fb-cpp-export.h"
30#include <cstdint>
31#include <optional>
32#include <stdexcept>
33#include <string>
34#include <vector>
35
36
40namespace fbcpp
41{
45 class BackupOptions final
46 {
47 public:
48 struct BackupFileSpec final
49 {
50 std::string path;
51 std::optional<std::uint64_t> length;
52 };
53
54 public:
58 const std::string& getDatabase() const
59 {
60 return database;
61 }
62
66 BackupOptions& setDatabase(const std::string& value)
67 {
68 database = value;
69 return *this;
70 }
71
75 const std::vector<BackupFileSpec>& getBackupFiles() const
76 {
77 return backupFiles;
78 }
79
83 BackupOptions& addBackupFile(const std::string& value)
84 {
85 backupFiles.emplace_back(value, std::nullopt);
86 return *this;
87 }
88
92 BackupOptions& addBackupFile(const std::string& value, std::uint64_t length)
93 {
94 if (!backupFiles.empty() && !backupFiles.back().length)
95 throw std::invalid_argument{"Cannot add a backup file with length after a backup file without length"};
96
97 backupFiles.emplace_back(value, length);
98 return *this;
99 }
100
104 BackupOptions& setBackupFile(const std::string& value)
105 {
106 backupFiles = {{value, std::nullopt}};
107 return *this;
108 }
109
113 BackupOptions& setBackupFile(const std::string& value, std::uint64_t length)
114 {
115 backupFiles = {{value, length}};
116 return *this;
117 }
118
123 {
124 return verboseOutput;
125 }
126
131 {
132 verboseOutput = std::move(value);
133 return *this;
134 }
135
139 const std::optional<std::uint32_t>& getParallelWorkers() const
140 {
141 return parallelWorkers;
142 }
143
147 BackupOptions& setParallelWorkers(std::uint32_t value)
148 {
149 parallelWorkers = value;
150 return *this;
151 }
152
153 private:
154 std::string database;
155 std::vector<BackupFileSpec> backupFiles;
156 ServiceManager::VerboseOutput verboseOutput;
157 std::optional<std::uint32_t> parallelWorkers;
158 };
159
163 class RestoreOptions final
164 {
165 public:
166 struct DatabaseFileSpec final
167 {
168 std::string path;
169 std::optional<std::uint64_t> length;
170 };
171
172 public:
176 const std::vector<DatabaseFileSpec>& getDatabaseFiles() const
177 {
178 return databaseFiles;
179 }
180
184 RestoreOptions& setDatabase(const std::string& value)
185 {
186 databaseFiles = {{value, std::nullopt}};
187 return *this;
188 }
189
193 RestoreOptions& setDatabase(const std::string& value, std::uint64_t length)
194 {
195 databaseFiles = {{value, length}};
196 return *this;
197 }
198
202 RestoreOptions& addDatabaseFile(const std::string& value)
203 {
204 databaseFiles.emplace_back(value, std::nullopt);
205 return *this;
206 }
207
211 RestoreOptions& addDatabaseFile(const std::string& value, std::uint64_t length)
212 {
213 if (!databaseFiles.empty() && !databaseFiles.back().length)
214 {
215 throw std::invalid_argument{
216 "Cannot add a database file with length after a database file without length"};
217 }
218
219 databaseFiles.emplace_back(value, length);
220 return *this;
221 }
222
226 const std::vector<std::string>& getBackupFiles() const
227 {
228 return backupFiles;
229 }
230
234 RestoreOptions& addBackupFile(const std::string& value)
235 {
236 backupFiles.emplace_back(value);
237 return *this;
238 }
239
243 RestoreOptions& setBackupFile(const std::string& value)
244 {
245 backupFiles = {value};
246 return *this;
247 }
248
252 bool getReplace() const
253 {
254 return replace;
255 }
256
261 {
262 replace = value;
263 return *this;
264 }
265
270 {
271 return verboseOutput;
272 }
273
278 {
279 verboseOutput = std::move(value);
280 return *this;
281 }
282
286 const std::optional<std::uint32_t>& getParallelWorkers() const
287 {
288 return parallelWorkers;
289 }
290
294 RestoreOptions& setParallelWorkers(std::uint32_t value)
295 {
296 parallelWorkers = value;
297 return *this;
298 }
299
303 const std::optional<ReplicaMode>& getReplicaMode() const
304 {
305 return replicaMode;
306 }
307
312 {
313 replicaMode = value;
314 return *this;
315 }
316
317 private:
318 std::vector<DatabaseFileSpec> databaseFiles;
319 std::vector<std::string> backupFiles;
320 bool replace = false;
321 ServiceManager::VerboseOutput verboseOutput;
322 std::optional<std::uint32_t> parallelWorkers;
323 std::optional<ReplicaMode> replicaMode;
324 };
325
329 class FB_CPP_EXPORT BackupManager final : public ServiceManager
330 {
331 public:
332 using ServiceManager::ServiceManager;
333
334 public:
338 void backup(const BackupOptions& options);
339
343 void restore(const RestoreOptions& options);
344 };
345} // namespace fbcpp
346
347
348#endif // FBCPP_BACKUP_MANAGER_H
Executes backup and restore operations through the Firebird service manager.
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 & setReplicaMode(ReplicaMode value)
Sets the replica mode.
RestoreOptions & setParallelWorkers(std::uint32_t value)
Sets the requested number of parallel workers.
const std::optional< ReplicaMode > & getReplicaMode() const
Returns the replica mode.
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.
std::function< void(std::string_view line)> VerboseOutput
Function invoked when a verbose service output line is available.
fb-cpp namespace.
Definition Attachment.h:46
ReplicaMode
Replica mode.