fb-cpp 0.0.2
A modern C++ wrapper for the Firebird database API
Loading...
Searching...
No Matches
BackupManager.cpp
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#include "BackupManager.h"
26#include "Client.h"
27#include <cassert>
28
29using namespace fbcpp;
30using namespace fbcpp::impl;
31
33{
34 StatusWrapper statusWrapper{getClient()};
35 auto builder =
36 fbUnique(getClient().getUtil()->getXpbBuilder(&statusWrapper, fb::IXpbBuilder::SPB_START, nullptr, 0));
37 builder->insertTag(&statusWrapper, isc_action_svc_backup);
38 builder->insertString(&statusWrapper, isc_spb_dbname, options.getDatabase().c_str());
39
40 for (const auto& backupFile : options.getBackupFiles())
41 {
42 builder->insertString(&statusWrapper, isc_spb_bkp_file, backupFile.path.c_str());
43
44 if (backupFile.length)
45 addSpbInt(builder.get(), &statusWrapper, isc_spb_bkp_length, *backupFile.length, "Backup file length");
46 }
47
48 if (options.getVerboseOutput())
49 builder->insertTag(&statusWrapper, isc_spb_verbose);
50
51 if (const auto parallelWorkers = options.getParallelWorkers())
52 builder->insertInt(&statusWrapper, isc_spb_bkp_parallel_workers, static_cast<int>(*parallelWorkers));
53
54 const auto buffer = builder->getBuffer(&statusWrapper);
55 const auto length = builder->getBufferLength(&statusWrapper);
56
57 startAction(std::vector<std::uint8_t>(buffer, buffer + length));
58 waitForCompletion(options.getVerboseOutput());
59}
60
62{
63 StatusWrapper statusWrapper{getClient()};
64 auto builder =
65 fbUnique(getClient().getUtil()->getXpbBuilder(&statusWrapper, fb::IXpbBuilder::SPB_START, nullptr, 0));
66 builder->insertTag(&statusWrapper, isc_action_svc_restore);
67
68 for (const auto& databaseFile : options.getDatabaseFiles())
69 {
70 builder->insertString(&statusWrapper, isc_spb_dbname, databaseFile.path.c_str());
71
72 if (databaseFile.length)
73 addSpbInt(builder.get(), &statusWrapper, isc_spb_res_length, *databaseFile.length, "Database file length");
74 }
75
76 for (const auto& backupFile : options.getBackupFiles())
77 builder->insertString(&statusWrapper, isc_spb_bkp_file, backupFile.c_str());
78
79 builder->insertInt(
80 &statusWrapper, isc_spb_options, options.getReplace() ? isc_spb_res_replace : isc_spb_res_create);
81
82 if (options.getVerboseOutput())
83 builder->insertTag(&statusWrapper, isc_spb_verbose);
84
85 if (const auto parallelWorkers = options.getParallelWorkers())
86 builder->insertInt(&statusWrapper, isc_spb_res_parallel_workers, static_cast<int>(*parallelWorkers));
87
88 if (const auto replicaMode = options.getReplicaMode())
89 {
90 std::uint8_t modeVal = 0;
91 switch (*replicaMode)
92 {
94 modeVal = isc_spb_res_rm_none;
95 break;
97 modeVal = isc_spb_res_rm_readonly;
98 break;
100 modeVal = isc_spb_res_rm_readwrite;
101 break;
102 default:
103 assert(false);
104 break;
105 }
106 builder->insertBytes(&statusWrapper, isc_spb_res_replica_mode, &modeVal, 1u);
107 }
108
109 const auto buffer = builder->getBuffer(&statusWrapper);
110 const auto length = builder->getBufferLength(&statusWrapper);
111
112 startAction(std::vector<std::uint8_t>(buffer, buffer + length));
113 waitForCompletion(options.getVerboseOutput(), true);
114}
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.
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.
Represents options used to run a restore operation through the service manager.
bool getReplace() const
Returns whether the target database should be replaced.
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.
const std::vector< DatabaseFileSpec > & getDatabaseFiles() const
Returns the configured database file specifications.
const std::optional< ReplicaMode > & getReplicaMode() const
Returns the replica mode.
const std::vector< std::string > & getBackupFiles() const
Returns the backup file paths.
Client & getClient() noexcept
Returns the Client object reference used to create this ServiceManager object.
fb-cpp namespace.
Definition Attachment.h:45
FbUniquePtr< T > fbUnique(T *obj) noexcept
Creates a unique pointer for a Firebird disposable object.
Definition SmartPtrs.h:59
@ READ_ONLY
Read-only replica.
@ READ_WRITE
Read-write replica.
@ NONE
The database is not a replica (operates as primary).