From: Cheng-Ling Lai Date: Tue, 9 Dec 2025 08:45:34 +0000 (+0800) Subject: Implemented snbk restore command X-Git-Tag: v0.13.1~62^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c4a58569b6869327db2cd1f9a2244df73c0c263b;p=thirdparty%2Fsnapper.git Implemented snbk restore command --- diff --git a/client/snbk/Makefile.am b/client/snbk/Makefile.am index 4a0f06ad..d229e1b6 100644 --- a/client/snbk/Makefile.am +++ b/client/snbk/Makefile.am @@ -12,6 +12,7 @@ snbk_SOURCES = \ cmd-list-configs.cc \ cmd-list.cc \ cmd-transfer.cc \ + cmd-restore.cc \ cmd-delete.cc \ cmd-transfer-and-delete.cc \ BackupConfig.cc BackupConfig.h \ diff --git a/client/snbk/cmd-restore.cc b/client/snbk/cmd-restore.cc new file mode 100644 index 00000000..78a4e2e8 --- /dev/null +++ b/client/snbk/cmd-restore.cc @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2024 SUSE LLC + * + * All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of version 2 of the GNU General Public License as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, contact Novell, Inc. + * + * To contact Novell about this file by physical or electronic mail, you may + * find current contact information at www.novell.com. + */ + + +#include +#include + +#include + +#include "../proxy/errors.h" +#include "../utils/text.h" + +#include "BackupConfig.h" +#include "GlobalOptions.h" +#include "TheBigThing.h" +#include "cmd.h" + + +namespace snapper +{ + + using namespace std; + + + void + help_restore() + { + cout << " " << _("Restore:") << '\n' + << "\t" << _("snbk restore [numbers]") << '\n' + << '\n'; + } + + + void + command_restore(const GlobalOptions& global_options, GetOpts& get_opts, + BackupConfigs& backup_configs, ProxySnappers* snappers) + { + static const regex num_regex("[0-9]+", regex::extended); + + ParsedOpts opts = get_opts.parse("restore", GetOpts::no_options); + + vector nums; + + while (get_opts.has_args()) + { + string arg = get_opts.pop_arg(); + + if (!regex_match(arg, num_regex)) + SN_THROW(Exception(_("Failed to parse number."))); + + nums.push_back(stoi(arg)); + } + + unsigned int errors = 0; + + for (const BackupConfig& backup_config : backup_configs) + { + if (!global_options.quiet()) + cout << sformat(_("Running restore for backup config '%s'."), + backup_config.name.c_str()) << endl; + + try + { + TheBigThings the_big_things(backup_config, snappers, + global_options.verbose()); + + if (nums.empty()) + { + the_big_things.restore(backup_config, global_options.quiet(), + global_options.quiet()); + } + else + { + for (unsigned int num : nums) + { + TheBigThings::iterator it = the_big_things.find(num); + if (it == the_big_things.end()) + { + string error = sformat(_("Snapshot number %d not found."), + num); + SN_THROW(Exception(error)); + } + + it->restore(backup_config, the_big_things, + global_options.quiet()); + } + } + } + catch (const DBus::ErrorException& e) + { + SN_CAUGHT(e); + + cerr << error_description(e) << endl; + + ++errors; + } + catch (const Exception& e) + { + SN_CAUGHT(e); + + cerr << e.what() << '\n'; + + cerr << sformat(_("Running restore for backup config '%s' failed."), + backup_config.name.c_str()) << endl; + + ++errors; + } + } + + if (errors != 0) + { + string error = sformat( + _("Running restore failed for %d of %ld backup config.", + "Running restore failed for %d of %ld backup configs.", + backup_configs.size()), errors, backup_configs.size() + ); + + SN_THROW(Exception(error)); + } + } + +} diff --git a/client/snbk/cmd.h b/client/snbk/cmd.h index 5a5a9c88..681c0043 100644 --- a/client/snbk/cmd.h +++ b/client/snbk/cmd.h @@ -53,6 +53,14 @@ namespace snapper ProxySnappers* snappers); + void + help_restore(); + + void + command_restore(const GlobalOptions& global_options, GetOpts& get_opts, BackupConfigs& backup_configs, + ProxySnappers* snappers); + + void help_delete(); diff --git a/client/snbk/snbk.cc b/client/snbk/snbk.cc index bad92906..753bf340 100644 --- a/client/snbk/snbk.cc +++ b/client/snbk/snbk.cc @@ -125,6 +125,7 @@ main(int argc, char** argv) Cmd("list-configs", command_list_configs, help_list_configs, false), Cmd("list", { "ls" }, command_list, help_list, true), Cmd("transfer", command_transfer, help_transfer, true), + Cmd("restore", command_restore, help_restore, true), Cmd("delete", { "remove", "rm" }, command_delete, help_delete, true), Cmd("transfer-and-delete", command_transfer_and_delete, help_transfer_and_delete, true), };