Skip to content

Commit 358a014

Browse files
committed
Add BackupManager
1 parent 0b9de71 commit 358a014

8 files changed

Lines changed: 1269 additions & 7 deletions

File tree

src/fb-cpp/BackupManager.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
28+
using namespace fbcpp;
29+
using namespace fbcpp::impl;
30+
31+
void BackupManager::backup(const BackupOptions& options)
32+
{
33+
StatusWrapper statusWrapper{getClient()};
34+
auto builder =
35+
fbUnique(getClient().getUtil()->getXpbBuilder(&statusWrapper, fb::IXpbBuilder::SPB_START, nullptr, 0));
36+
builder->insertTag(&statusWrapper, isc_action_svc_backup);
37+
builder->insertString(&statusWrapper, isc_spb_dbname, options.getDatabase().c_str());
38+
39+
for (const auto& backupFile : options.getBackupFiles())
40+
{
41+
builder->insertString(&statusWrapper, isc_spb_bkp_file, backupFile.path.c_str());
42+
43+
if (backupFile.length)
44+
addSpbInt(builder.get(), &statusWrapper, isc_spb_bkp_length, *backupFile.length, "Backup file length");
45+
}
46+
47+
if (options.getVerboseOutput())
48+
builder->insertTag(&statusWrapper, isc_spb_verbose);
49+
50+
if (const auto parallelWorkers = options.getParallelWorkers())
51+
builder->insertInt(&statusWrapper, isc_spb_bkp_parallel_workers, static_cast<int>(*parallelWorkers));
52+
53+
const auto buffer = builder->getBuffer(&statusWrapper);
54+
const auto length = builder->getBufferLength(&statusWrapper);
55+
56+
startAction(std::vector<std::uint8_t>(buffer, buffer + length));
57+
waitForCompletion(options.getVerboseOutput());
58+
}
59+
60+
void BackupManager::restore(const RestoreOptions& options)
61+
{
62+
StatusWrapper statusWrapper{getClient()};
63+
auto builder =
64+
fbUnique(getClient().getUtil()->getXpbBuilder(&statusWrapper, fb::IXpbBuilder::SPB_START, nullptr, 0));
65+
builder->insertTag(&statusWrapper, isc_action_svc_restore);
66+
67+
for (const auto& databaseFile : options.getDatabaseFiles())
68+
{
69+
builder->insertString(&statusWrapper, isc_spb_dbname, databaseFile.path.c_str());
70+
71+
if (databaseFile.length)
72+
addSpbInt(builder.get(), &statusWrapper, isc_spb_res_length, *databaseFile.length, "Database file length");
73+
}
74+
75+
for (const auto& backupFile : options.getBackupFiles())
76+
builder->insertString(&statusWrapper, isc_spb_bkp_file, backupFile.c_str());
77+
78+
builder->insertInt(
79+
&statusWrapper, isc_spb_options, options.getReplace() ? isc_spb_res_replace : isc_spb_res_create);
80+
81+
if (options.getVerboseOutput())
82+
builder->insertTag(&statusWrapper, isc_spb_verbose);
83+
84+
if (const auto parallelWorkers = options.getParallelWorkers())
85+
builder->insertInt(&statusWrapper, isc_spb_res_parallel_workers, static_cast<int>(*parallelWorkers));
86+
87+
const auto buffer = builder->getBuffer(&statusWrapper);
88+
const auto length = builder->getBufferLength(&statusWrapper);
89+
90+
startAction(std::vector<std::uint8_t>(buffer, buffer + length));
91+
waitForCompletion(options.getVerboseOutput(), true);
92+
}

0 commit comments

Comments
 (0)