Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ndmp_dma_backup_common.cc « dird « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: efe385d0e6b2fc291f916f89e02350079f423c41 (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   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.
*/
/*
 * Routines common to NDMP_BAREOS and NDMP_NATIVE backups
 * extracted from ndmp_dma_backup
 *
 * Philipp Storz, April 2017
 */

#include "include/bareos.h"
#include "dird.h"
#include "dird/backup.h"
#include "dird/jcr_private.h"
#include "dird/job.h"
#include "dird/ndmp_dma_backup_common.h"
#include "lib/edit.h"

#if HAVE_NDMP
#  define NDMP_NEED_ENV_KEYWORDS 1
#  include "ndmp/ndmagents.h"
#  include "ndmp_dma_priv.h"
#endif /* HAVE_NDMP */

namespace directordaemon {

#if HAVE_NDMP
/*
 * Fill the NDMP backup environment table with the data for the data agent to
 * act on.
 */
bool FillBackupEnvironment(JobControlRecord* jcr,
                           IncludeExcludeItem* ie,
                           char* filesystem,
                           struct ndm_job_param* job)
{
  uint64_t i;
  int j, cnt;
  bool exclude;
  FileOptions* fo;
  ndmp9_pval pv;
  PoolMem pattern;
  PoolMem tape_device;
  ndmp_backup_format_option* nbf_options;

  // See if we know this backup format and get it options.
  nbf_options = ndmp_lookup_backup_format_options(job->bu_type);

  if (!nbf_options || nbf_options->uses_file_history) {
    // We want to receive file history info from the NDMP backup.
    pv.name = ndmp_env_keywords[NDMP_ENV_KW_HIST];
    pv.value = ndmp_env_values[NDMP_ENV_VALUE_YES];
    ndma_store_env_list(&job->env_tab, &pv);
  } else {
    // We don't want to receive file history info from the NDMP backup.
    pv.name = ndmp_env_keywords[NDMP_ENV_KW_HIST];
    pv.value = ndmp_env_values[NDMP_ENV_VALUE_NO];
    ndma_store_env_list(&job->env_tab, &pv);
  }

  // Tell the data agent what type of backup to make.
  pv.name = ndmp_env_keywords[NDMP_ENV_KW_TYPE];
  pv.value = job->bu_type;
  ndma_store_env_list(&job->env_tab, &pv);

  // See if we are doing a backup type that uses dumplevels.
  if (nbf_options && nbf_options->uses_level) {
    char text_level[50];

    // Set the dump level for the backup.
    jcr->impl->DumpLevel = NativeToNdmpLevel(jcr, filesystem);
    job->bu_level = jcr->impl->DumpLevel;
    if (job->bu_level == -1) { return false; }

    pv.name = ndmp_env_keywords[NDMP_ENV_KW_LEVEL];
    pv.value = edit_uint64(job->bu_level, text_level);
    ndma_store_env_list(&job->env_tab, &pv);

    // Update the dumpdates
    pv.name = ndmp_env_keywords[NDMP_ENV_KW_UPDATE];
    pv.value = ndmp_env_values[NDMP_ENV_VALUE_YES];
    ndma_store_env_list(&job->env_tab, &pv);
  }

  // Tell the data engine what to backup.
  pv.name = ndmp_env_keywords[NDMP_ENV_KW_FILESYSTEM];
  pv.value = filesystem;
  ndma_store_env_list(&job->env_tab, &pv);

  /*
   * Loop over each option block for this fileset and append any
   * INCLUDE/EXCLUDE and/or META tags to the env_tab of the NDMP backup.
   */
  for (i = 0; i < ie->file_options_list.size(); i++) {
    fo = ie->file_options_list[i];

    // Pickup any interesting patterns.
    cnt = 0;
    PmStrcpy(pattern, "");
    for (j = 0; j < fo->wild.size(); j++) {
      if (cnt != 0) { PmStrcat(pattern, ","); }
      PmStrcat(pattern, (char*)fo->wild.get(j));
      cnt++;
    }
    for (j = 0; j < fo->wildfile.size(); j++) {
      if (cnt != 0) { PmStrcat(pattern, ","); }
      PmStrcat(pattern, (char*)fo->wildfile.get(j));
      cnt++;
    }
    for (j = 0; j < fo->wilddir.size(); j++) {
      if (cnt != 0) { PmStrcat(pattern, ","); }
      PmStrcat(pattern, (char*)fo->wilddir.get(j));
      cnt++;
    }

    // See if this is a INCLUDE or EXCLUDE block.
    if (cnt > 0) {
      exclude = false;
      for (j = 0; fo->opts[j] != '\0'; j++) {
        if (fo->opts[j] == 'e') {
          exclude = true;
          break;
        }
      }

      if (exclude) {
        pv.name = ndmp_env_keywords[NDMP_ENV_KW_EXCLUDE];
      } else {
        pv.name = ndmp_env_keywords[NDMP_ENV_KW_INCLUDE];
      }

      pv.value = pattern.c_str();
      ndma_store_env_list(&job->env_tab, &pv);
    }

    // Parse all specific META tags for this option block.
    for (j = 0; j < fo->meta.size(); j++) {
      NdmpParseMetaTag(&job->env_tab, (char*)fo->meta.get(j));
    }
  }

  /*
   * If we have a paired storage definition we put the
   * - Storage Daemon Auth Key
   * - Filesystem
   * - Dumplevel
   * into the tape device name of the  NDMP session. This way the storage
   * daemon can link the NDMP data and the normal save session together.
   */
  if (jcr->store_bsock) {
    if (nbf_options && nbf_options->uses_level) {
      Mmsg(tape_device, "%s@%s%%%d", jcr->sd_auth_key, filesystem,
           jcr->impl->DumpLevel);
    } else {
      Mmsg(tape_device, "%s@%s", jcr->sd_auth_key, filesystem);
    }
    job->tape_device = strdup(tape_device.c_str());
  }

  return true;
}


// Translate bareos native backup level to NDMP backup level
int NativeToNdmpLevel(JobControlRecord* jcr, char* filesystem)
{
  int level = -1;

  if (!jcr->db->CreateNdmpLevelMapping(jcr, &jcr->impl->jr, filesystem)) {
    return -1;
  }

  switch (jcr->getJobLevel()) {
    case L_FULL:
      level = 0;
      break;
    case L_DIFFERENTIAL:
      level = 1;
      break;
    case L_INCREMENTAL:
      level = jcr->db->GetNdmpLevelMapping(jcr, &jcr->impl->jr, filesystem);
      break;
    default:
      Jmsg(jcr, M_FATAL, 0, _("Illegal Job Level %c for NDMP Job\n"),
           jcr->getJobLevel());
      break;
  }

  // Dump level can be from 0 - 9
  if (level < 0 || level > 9) {
    Jmsg(jcr, M_FATAL, 0,
         _("NDMP dump format doesn't support more than 8 "
           "incrementals, please run a Differential or a Full Backup\n"));
    level = -1;
  }

  return level;
}

// This glues the NDMP File Handle DB with internal code.
void RegisterCallbackHooks(struct ndmlog* ixlog)
{
#  ifdef HAVE_LMDB
  NIS* nis = (NIS*)ixlog->ctx;

  if (nis->jcr->impl->res.client->ndmp_use_lmdb) {
    NdmpFhdbLmdbRegister(ixlog);
  } else {
    NdmpFhdbMemRegister(ixlog);
  }
#  else
  NdmpFhdbMemRegister(ixlog);
#  endif
}

void UnregisterCallbackHooks(struct ndmlog* ixlog)
{
#  ifdef HAVE_LMDB
  NIS* nis = (NIS*)ixlog->ctx;

  if (nis->jcr->impl->res.client->ndmp_use_lmdb) {
    NdmpFhdbLmdbUnregister(ixlog);
  } else {
    NdmpFhdbMemUnregister(ixlog);
  }
#  else
  NdmpFhdbMemUnregister(ixlog);
#  endif
}

void ProcessFhdb(struct ndmlog* ixlog)
{
#  ifdef HAVE_LMDB
  NIS* nis = (NIS*)ixlog->ctx;

  if (nis->jcr->impl->res.client->ndmp_use_lmdb) {
    NdmpFhdbLmdbProcessDb(ixlog);
  } else {
    NdmpFhdbMemProcessDb(ixlog);
  }
#  else
  NdmpFhdbMemProcessDb(ixlog);
#  endif
}

// Cleanup a NDMP backup session.
void NdmpBackupCleanup(JobControlRecord* jcr, int TermCode)
{
  const char* TermMsg;
  char term_code[100];
  int msg_type = M_INFO;
  ClientDbRecord cr;

  Dmsg2(100, "Enter NdmpBackupCleanup %d %c\n", TermCode, TermCode);

  if (jcr->is_JobStatus(JS_Terminated)
      && (jcr->JobErrors || jcr->impl->SDErrors || jcr->JobWarnings)) {
    TermCode = JS_Warnings;
  }

  UpdateJobEnd(jcr, TermCode);

  if (!jcr->db->GetJobRecord(jcr, &jcr->impl->jr)) {
    Jmsg(jcr, M_WARNING, 0,
         _("Error getting Job record for Job report: ERR=%s"),
         jcr->db->strerror());
    jcr->setJobStatus(JS_ErrorTerminated);
  }

  bstrncpy(cr.Name, jcr->impl->res.client->resource_name_, sizeof(cr.Name));
  if (!jcr->db->GetClientRecord(jcr, &cr)) {
    Jmsg(jcr, M_WARNING, 0,
         _("Error getting Client record for Job report: ERR=%s"),
         jcr->db->strerror());
  }

  UpdateBootstrapFile(jcr);

  switch (jcr->JobStatus) {
    case JS_Terminated:
      TermMsg = _("Backup OK");
      break;
    case JS_Warnings:
      TermMsg = _("Backup OK -- with warnings");
      break;
    case JS_FatalError:
    case JS_ErrorTerminated:
      TermMsg = _("*** Backup Error ***");
      msg_type = M_ERROR; /* Generate error message */
      if (jcr->store_bsock) {
        jcr->store_bsock->signal(BNET_TERMINATE);
        if (jcr->impl->SD_msg_chan_started) {
          pthread_cancel(jcr->impl->SD_msg_chan);
        }
      }
      break;
    case JS_Canceled:
      TermMsg = _("Backup Canceled");
      if (jcr->store_bsock) {
        jcr->store_bsock->signal(BNET_TERMINATE);
        if (jcr->impl->SD_msg_chan_started) {
          pthread_cancel(jcr->impl->SD_msg_chan);
        }
      }
      break;
    default:
      TermMsg = term_code;
      sprintf(term_code, _("Inappropriate term code: %c\n"), jcr->JobStatus);
      break;
  }

  GenerateBackupSummary(jcr, &cr, msg_type, TermMsg);

  Dmsg0(100, "Leave NdmpBackupCleanup\n");
}

#else /* HAVE_NDMP */

void NdmpBackupCleanup(JobControlRecord* jcr, int)
{
  Jmsg(jcr, M_FATAL, 0, _("NDMP protocol not supported\n"));
}

#endif /* HAVE_NDMP */

} /* namespace directordaemon */