/
ndmp_dma_restore_common.cc
239 lines (207 loc) · 7.11 KB
/
ndmp_dma_restore_common.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2011-2015 Planets Communications B.V.
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
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
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Restore specific NDMP Data Management Application (DMA) routines
* common for NDMP_BAREOS and NDMP_NATIVE restores
*
* extracted and reorganized from ndmp_dma_restore.c
*
* Philipp Storz, April 2017
*/
#include "include/bareos.h"
#include "dird.h"
#include "dird/director_jcr_impl.h"
#include "dird/job.h"
#include "dird/restore.h"
#if HAVE_NDMP
# include "ndmp/ndmagents.h"
# include "ndmp_dma_priv.h"
#endif /* HAVE_NDMP */
namespace directordaemon {
#if HAVE_NDMP
/*
* Add a filename to the files we want to restore.
*
* The RFC says this:
*
* original_path - The original path name of the data to be recovered,
* relative to the backup root. If original_path is the null
* string, the server shall recover all data contained in the
* backup image.
*
* destination_path, name, other_name
* - Together, these identify the absolute path name to which
* data are to be recovered.
*
* If name is the null string:
* - destination_path identifies the name to which the data
* identified by original_path are to be recovered.
* - other_name must be the null string.
*
* If name is not the null string:
* - destination_path, when concatenated with the server-
* specific path name delimiter and name, identifies the
* name to which the data identified by original_path are
* to be recovered.
*
* If other_name is not the null string:
* - destination_path, when concatenated with the server-
* specific path name delimiter and other_name,
* identifies the alternate name-space name of the data
* to be recovered. The definition of such alternate
* name-space is server-specific.
*
* Neither name nor other_name may contain a path name delimiter.
*
* Under no circumstance may destination_path be the null string.
*
* If intermediate directories that lead to the path name to
* recover do not exist, the server should create them.
*/
void AddToNamelist(struct ndm_job_param* job,
char* filename,
const char* restore_prefix,
char* name,
char* other_name,
uint64_t node,
uint64_t fhinfo,
bool set_zero_for_invalid_u_quad)
{
ndmp9_name nl;
PoolMem destination_path;
memset(&nl, 0, sizeof(ndmp9_name));
// See if the filename is an absolute pathname.
if (*filename == '\0') {
PmStrcpy(destination_path, restore_prefix);
} else if (*filename == '/') {
Mmsg(destination_path, "%s%s", restore_prefix, filename);
} else {
Mmsg(destination_path, "%s/%s", restore_prefix, filename);
}
nl.original_path = filename;
nl.destination_path = destination_path.c_str();
nl.name = name;
nl.other_name = other_name;
// add fh_node and fh_info for DAR for native NDMP backup
nl.node = node;
if (fhinfo) {
nl.fh_info.value = fhinfo;
// check if we need to set 0 instead of NDMP_INVALID_U_QUAD
if (set_zero_for_invalid_u_quad && fhinfo == NDMP_INVALID_U_QUAD) {
nl.fh_info.value = 0;
}
nl.fh_info.valid = NDMP9_VALIDITY_VALID;
}
ndma_store_nlist(&job->nlist_tab, &nl);
}
/*
* Database handler that handles the returned environment data for a given
* JobId.
*/
int NdmpEnvHandler(void* ctx, int, char** row)
{
struct ndm_env_table* envtab;
ndmp9_pval pv;
if (row[0] && row[1]) {
envtab = (struct ndm_env_table*)ctx;
pv.name = row[0];
pv.value = row[1];
ndma_store_env_list(envtab, &pv);
}
return 0;
}
// Extract any post backup statistics.
bool ExtractPostRestoreStats(JobControlRecord* jcr, struct ndm_session* sess)
{
bool retval = true;
struct ndmmedia* media;
// See if an error was raised during the backup session.
if (sess->error_raised) { return false; }
// See if there is any media error.
for (media = sess->control_acb->job.result_media_tab.head; media;
media = media->next) {
if (media->media_open_error || media->media_io_error
|| media->label_io_error || media->label_mismatch
|| media->fmark_error) {
retval = false;
}
}
// Update the Job statistics from the NDMP statistics.
jcr->JobBytes += sess->control_acb->job.bytes_read;
jcr->JobFiles++;
return retval;
}
// Cleanup a NDMP restore session.
void NdmpRestoreCleanup(JobControlRecord* jcr, int TermCode)
{
char term_code[100];
const char* TermMsg;
int msg_type = M_INFO;
Dmsg0(20, "In NdmpRestoreCleanup\n");
UpdateJobEnd(jcr, TermCode);
if (jcr->dir_impl->unlink_bsr && jcr->RestoreBootstrap) {
SecureErase(jcr, jcr->RestoreBootstrap);
jcr->dir_impl->unlink_bsr = false;
}
if (jcr->IsJobCanceled()) { CancelStorageDaemonJob(jcr); }
switch (TermCode) {
case JS_Terminated:
if (jcr->dir_impl->ExpectedFiles > jcr->dir_impl->jr.JobFiles) {
TermMsg = _("Restore OK -- warning file count mismatch");
} else {
TermMsg = _("Restore OK");
}
break;
case JS_Warnings:
TermMsg = _("Restore OK -- with warnings");
break;
case JS_FatalError:
case JS_ErrorTerminated:
TermMsg = _("*** Restore Error ***");
msg_type = M_ERROR; /* Generate error message */
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->dir_impl->SD_msg_chan_started) {
pthread_cancel(jcr->dir_impl->SD_msg_chan);
}
}
break;
case JS_Canceled:
TermMsg = _("Restore Canceled");
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->dir_impl->SD_msg_chan_started) {
pthread_cancel(jcr->dir_impl->SD_msg_chan);
}
}
break;
default:
TermMsg = term_code;
sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
break;
}
GenerateRestoreSummary(jcr, msg_type, TermMsg);
Dmsg0(20, "Leaving NdmpRestoreCleanup\n");
}
#else /* HAVE_NDMP */
void NdmpRestoreCleanup(JobControlRecord* jcr, int)
{
Jmsg(jcr, M_FATAL, 0, _("NDMP protocol not supported\n"));
}
#endif /* HAVE_NDMP */
} /* namespace directordaemon */