1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Dietmar Maurer <dietmar@proxmox.com>
3 Date: Mon, 6 Apr 2020 12:17:01 +0200
4 Subject: [PATCH] PVE-Backup: pbs-restore - new command to restore from proxmox
7 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
10 pbs-restore.c | 223 ++++++++++++++++++++++++++++++++++++++++++++++++++
11 2 files changed, 227 insertions(+)
12 create mode 100644 pbs-restore.c
14 diff --git a/meson.build b/meson.build
15 index 782756162c..63ea813a9a 100644
18 @@ -3602,6 +3602,10 @@ if have_tools
19 vma = executable('vma', files('vma.c', 'vma-reader.c') + genh,
20 dependencies: [authz, block, crypto, io, qom], install: true)
22 + pbs_restore = executable('pbs-restore', files('pbs-restore.c') + genh,
23 + dependencies: [authz, block, crypto, io, qom,
24 + libproxmox_backup_qemu], install: true)
26 subdir('storage-daemon')
27 subdir('contrib/rdmacm-mux')
28 subdir('contrib/elf2dmp')
29 diff --git a/pbs-restore.c b/pbs-restore.c
31 index 0000000000..2f834cf42e
36 + * Qemu image restore helper for Proxmox Backup
38 + * Copyright (C) 2019 Proxmox Server Solutions
41 + * Dietmar Maurer (dietmar@proxmox.com)
43 + * This work is licensed under the terms of the GNU GPL, version 2 or later.
44 + * See the COPYING file in the top-level directory.
48 +#include "qemu/osdep.h"
53 +#include "qemu/module.h"
54 +#include "qemu/error-report.h"
55 +#include "qemu/main-loop.h"
56 +#include "qemu/cutils.h"
57 +#include "qapi/error.h"
58 +#include "qapi/qmp/qdict.h"
59 +#include "sysemu/block-backend.h"
61 +#include <proxmox-backup-qemu.h>
63 +static void help(void)
65 + const char *help_msg =
66 + "usage: pbs-restore [--repository <repo>] snapshot archive-name target [command options]\n"
69 + printf("%s", help_msg);
73 +typedef struct CallbackData {
74 + BlockBackend *target;
75 + uint64_t last_offset;
79 +static int write_callback(
80 + void *callback_data_ptr,
82 + const unsigned char *data,
87 + CallbackData *callback_data = (CallbackData *)callback_data_ptr;
89 + uint64_t last_offset = callback_data->last_offset;
90 + if (offset > last_offset) callback_data->last_offset = offset;
93 + if (callback_data->skip_zero && offset > last_offset) {
96 + res = blk_pwrite_zeroes(callback_data->target, offset, data_len, 0);
98 + res = blk_pwrite(callback_data->target, offset, data_len, data, 0);
102 + fprintf(stderr, "blk_pwrite failed at offset %ld length %ld (%d) - %s\n", offset, data_len, res, strerror(-res));
109 +int main(int argc, char **argv)
111 + Error *main_loop_err = NULL;
112 + const char *format = "raw";
113 + const char *repository = NULL;
114 + const char *keyfile = NULL;
115 + int verbose = false;
116 + bool skip_zero = false;
118 + error_init(argv[0]);
121 + static const struct option long_options[] = {
122 + {"help", no_argument, 0, 'h'},
123 + {"skip-zero", no_argument, 0, 'S'},
124 + {"verbose", no_argument, 0, 'v'},
125 + {"format", required_argument, 0, 'f'},
126 + {"repository", required_argument, 0, 'r'},
127 + {"keyfile", required_argument, 0, 'k'},
130 + int c = getopt_long(argc, argv, "hvf:r:k:", long_options, NULL);
136 + fprintf(stderr, "missing argument for option '%s'\n", argv[optind - 1]);
139 + fprintf(stderr, "unrecognized option '%s'\n", argv[optind - 1]);
142 + format = g_strdup(argv[optind - 1]);
145 + repository = g_strdup(argv[optind - 1]);
148 + keyfile = g_strdup(argv[optind - 1]);
162 + if (optind >= argc - 2) {
163 + fprintf(stderr, "missing arguments\n");
168 + if (repository == NULL) {
169 + repository = getenv("PBS_REPOSITORY");
172 + if (repository == NULL) {
173 + fprintf(stderr, "no repository specified\n");
178 + char *snapshot = argv[optind++];
179 + char *archive_name = argv[optind++];
180 + char *target = argv[optind++];
182 + const char *password = getenv("PBS_PASSWORD");
183 + const char *fingerprint = getenv("PBS_FINGERPRINT");
184 + const char *key_password = getenv("PBS_ENCRYPTION_PASSWORD");
186 + if (qemu_init_main_loop(&main_loop_err)) {
187 + g_error("%s", error_get_pretty(main_loop_err));
191 + module_call_init(MODULE_INIT_QOM);
194 + fprintf(stderr, "connecting to repository '%s'\n", repository);
196 + char *pbs_error = NULL;
197 + ProxmoxRestoreHandle *conn = proxmox_restore_new(
198 + repository, snapshot, password, keyfile, key_password, fingerprint, &pbs_error);
199 + if (conn == NULL) {
200 + fprintf(stderr, "restore failed: %s\n", pbs_error);
204 + int res = proxmox_restore_connect(conn, &pbs_error);
205 + if (res < 0 || pbs_error) {
206 + fprintf(stderr, "restore failed (connection error): %s\n", pbs_error);
210 + QDict *options = qdict_new();
213 + qdict_put_str(options, "driver", format);
218 + fprintf(stderr, "open block backend for target '%s'\n", target);
220 + Error *local_err = NULL;
221 + int flags = BDRV_O_RDWR;
222 + BlockBackend *blk = blk_new_open(target, NULL, options, flags, &local_err);
224 + fprintf(stderr, "%s\n", error_get_pretty(local_err));
228 + CallbackData *callback_data = calloc(sizeof(CallbackData), 1);
230 + callback_data->target = blk;
231 + callback_data->skip_zero = skip_zero;
232 + callback_data->last_offset = 0;
234 + // blk_set_enable_write_cache(blk, !writethrough);
237 + fprintf(stderr, "starting to restore snapshot '%s'\n", snapshot);
238 + fflush(stderr); // ensure we do not get printed after the progress log
240 + res = proxmox_restore_image(
248 + proxmox_restore_disconnect(conn);
252 + fprintf(stderr, "restore failed: %s\n", pbs_error);