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

ndmp_dma_backup_NDMP_NATIVE.cc « dird « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9ae40d5e8fe765d9153de396f43a1b5bf27bd31c (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
   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.
*/
/*
 * Backup specific NDMP Data Management Application (DMA) routines
 * for NDMP_NATIVE Backups
 *
 * Philipp Storz, April 2017
 */

#include "include/bareos.h"
#include "dird.h"
#include "dird/director_jcr_impl.h"
#include "dird/dird_globals.h"
#include "dird/job.h"
#include "dird/next_vol.h"
#include "dird/quota.h"
#include "dird/storage.h"

#include "lib/edit.h"

#if HAVE_NDMP
#  include "dird/ndmp_dma_backup_common.h"
#  include "dird/ndmp_dma_generic.h"
#  include "dird/ndmp_dma_storage.h"

#  define NDMP_NEED_ENV_KEYWORDS 1

#  include "ndmp/ndmagents.h"
#  include "ndmp_dma_priv.h"
#endif /* HAVE_NDMP */

namespace directordaemon {

#if HAVE_NDMP
/* Forward referenced functions */
static inline bool extract_post_backup_stats_ndmp_native(
    JobControlRecord* jcr,
    char* filesystem,
    struct ndm_session* sess);


// callback to check if job is cancelled and NDMP should stop
int is_job_canceled_cb(struct ndm_session* sess)
{
  NIS* nis = (NIS*)sess->param->log.ctx;
  JobControlRecord* jcr = nis->jcr;

  return jcr->IsCanceled();
}


/**
 * Load next medium, also used as "load first" callback
 * find the next volume for append and add it to the
 * media tab of the current ndmp job so that the next
 * medium can be loaded.
 *
 * \return 0 if OK, -1 if NOT OK
 */

int NdmpLoadNext(struct ndm_session* sess)
{
  NIS* nis = (NIS*)sess->param->log.ctx;
  JobControlRecord* jcr = nis->jcr;
  struct ndm_media_table* media_tab;
  media_tab = &sess->control_acb->job.media_tab;
  MediaDbRecord mr;
  char* unwanted_volumes = (char*)"";
  bool create = false;
  bool prune = false;
  struct ndmmedia* media;
  int index = 1;
  StorageResource* store = jcr->dir_impl->res.write_storage;

  // get the poolid for pool name
  mr.PoolId = jcr->dir_impl->jr.PoolId;


  if (FindNextVolumeForAppend(jcr, &mr, index, unwanted_volumes, create,
                              prune)) {
    Jmsg(jcr, M_INFO, 0, _("Found volume for append: %s\n"), mr.VolumeName);

    // reserve medium so that it cannot be used by other job while we use it
    bstrncpy(mr.VolStatus, NT_("Used"), sizeof(mr.VolStatus));


    // set FirstWritten Timestamp
    mr.FirstWritten = (utime_t)time(NULL);

    if (!jcr->db->UpdateMediaRecord(jcr, &mr)) {
      Jmsg(jcr, M_FATAL, 0, _("Catalog error updating Media record. %s"),
           jcr->db->strerror());
      goto bail_out;
    }

    // insert volume info in ndmmedia list
    media = ndma_store_media(media_tab, mr.Slot);

    bstrncpy(media->label, mr.VolumeName, NDMMEDIA_LABEL_MAX - 1);
    media->valid_label = NDMP9_VALIDITY_VALID;


    // we want to append, so we need to skip what is already written
    media->file_mark_offset = mr.VolFiles + 1;
    media->valid_filemark = NDMP9_VALIDITY_VALID;

    media->slot_addr = mr.Slot;

    if (!NdmpUpdateStorageMappings(jcr, store)) {
      Jmsg(jcr, M_ERROR, 0, _("ERROR in NdmpUpdateStorageMappings\n"));
      goto bail_out;
    }

    slot_number_t slotnumber = GetElementAddressByBareosSlotNumber(
        &store->runtime_storage_status->storage_mapping,
        slot_type_t::kSlotTypeStorage, mr.Slot);
    /* check for success */
    if (!IsSlotNumberValid(slotnumber)) {
      Jmsg(jcr, M_FATAL, 0, _("GetElementAddressByBareosSlotNumber failed\n"));
      goto bail_out;
    }

    media->slot_addr = slotnumber;
    media->valid_slot = NDMP9_VALIDITY_VALID;

    ndmca_media_calculate_offsets(sess);

    return 0;

  } else {
  bail_out:
    Jmsg(jcr, M_INFO, 0, _("Error finding volume for append\n"));
    return -1;
  }
}

// Run a NDMP backup session for NDMP_NATIVE backup
bool DoNdmpBackupNdmpNative(JobControlRecord* jcr)
{
  int status;
  char ed1[100];
  NIS* nis = NULL;
  FilesetResource* fileset;
  struct ndm_job_param ndmp_job;
  struct ndm_session ndmp_sess;
  bool session_initialized = false;
  bool retval = false;
  uint32_t ndmp_log_level;

  char* item;

  ndmp_log_level
      = std::max(jcr->dir_impl->res.client->ndmp_loglevel, me->ndmp_loglevel);

  struct ndmca_media_callbacks media_callbacks;

  media_callbacks.load_first
      = NdmpLoadNext; /* we use the same callback for first and next*/
  media_callbacks.load_next = NdmpLoadNext;
  media_callbacks.unload_current = NULL;

  struct ndmca_jobcontrol_callbacks jobcontrol_callbacks;
  jobcontrol_callbacks.is_job_canceled = is_job_canceled_cb;


  // Print Job Start message
  Jmsg(jcr, M_INFO, 0, _("Start NDMP Backup JobId %s, Job=%s\n"),
       edit_uint64(jcr->JobId, ed1), jcr->Job);

  jcr->setJobStatusWithPriorityCheck(JS_Running);
  Dmsg2(100, "JobId=%d JobLevel=%c\n", jcr->dir_impl->jr.JobId,
        jcr->dir_impl->jr.JobLevel);
  if (!jcr->db->UpdateJobStartRecord(jcr, &jcr->dir_impl->jr)) {
    Jmsg(jcr, M_FATAL, 0, "%s", jcr->db->strerror());
    return false;
  }

  status = 0;
  StorageResource* store = jcr->dir_impl->res.write_storage;
  PoolMem virtual_filename(PM_FNAME);
  IncludeExcludeItem* ie;


  if (!NdmpBuildClientAndStorageJob(jcr, jcr->dir_impl->res.write_storage,
                                    jcr->dir_impl->res.client,
                                    true, /* init_tape */
                                    true, /* init_robot */
                                    NDM_JOB_OP_BACKUP, &ndmp_job)) {
    goto bail_out;
  }

  if (!ndmp_native_setup_robot_and_tape_for_native_backup_job(jcr, store,
                                                              ndmp_job)) {
    Jmsg(jcr, M_ERROR, 0,
         _("ndmp_native_setup_robot_and_tape_for_native_backup_job failed\n"));
    goto bail_out;
  }

  nis = (NIS*)malloc(sizeof(NIS));
  memset(nis, 0, sizeof(NIS));

  /*
   * Only one include set of the fileset  is allowed in NATIVE mode as
   * in NDMP also per job only one filesystem can be backed up
   */
  fileset = jcr->dir_impl->res.fileset;

  if (fileset->include_items.size() > 1) {
    Jmsg(jcr, M_ERROR, 0,
         "Exactly one include set is supported in NDMP NATIVE mode\n");
    return retval;
  }

  ie = fileset->include_items[0];

  /*
   * only one file = entry is allowed
   * and it is the ndmp filesystem to be backed up
   */
  if (ie->name_list.size() != 1) {
    Jmsg(jcr, M_ERROR, 0,
         "Exactly one  File specification is supported in NDMP NATIVE mode\n");
    return retval;
  }

  item = (char*)ie->name_list.first();

  /*
   * Perform the actual NDMP job.
   * Initialize a new NDMP session
   */
  memset(&ndmp_sess, 0, sizeof(ndmp_sess));
  ndmp_sess.conn_snooping = (me->ndmp_snooping) ? 1 : 0;
  ndmp_sess.control_agent_enabled = 1;

  ndmp_sess.param
      = (struct ndm_session_param*)malloc(sizeof(struct ndm_session_param));
  memset(ndmp_sess.param, 0, sizeof(struct ndm_session_param));
  ndmp_sess.param->log.deliver = NdmpLoghandler;
  ndmp_sess.param->log_level
      = NativeToNdmpLoglevel(ndmp_log_level, debug_level, nis);
  nis->filesystem = item;
  nis->FileIndex = 1;
  nis->jcr = jcr;
  nis->save_filehist = jcr->dir_impl->res.job->SaveFileHist;
  nis->filehist_size = jcr->dir_impl->res.job->FileHistSize;

  ndmp_sess.param->log.ctx = nis;
  ndmp_sess.param->log_tag = strdup("DIR-NDMP");
  ndmp_sess.dump_media_info = 1;

  // Initialize the session structure.
  if (ndma_session_initialize(&ndmp_sess)) { goto cleanup; }
  session_initialized = true;

  // Copy the actual job to perform.
  memcpy(&ndmp_sess.control_acb->job, &ndmp_job, sizeof(struct ndm_job_param));

  /*
   * We can use the same private pointer used in the logging with the JCR in
   * the file index generation. We don't setup a index_log.deliver
   * function as we catch the index information via callbacks.
   */
  ndmp_sess.control_acb->job.index_log.ctx = ndmp_sess.param->log.ctx;

  if (!FillBackupEnvironment(jcr, ie, nis->filesystem,
                             &ndmp_sess.control_acb->job)) {
    goto cleanup;
  }
  /* register the callbacks */
  ndmca_media_register_callbacks(&ndmp_sess, &media_callbacks);
  ndmca_jobcontrol_register_callbacks(&ndmp_sess, &jobcontrol_callbacks);

  /*
   * The full ndmp archive has a virtual filename, we need it to hardlink the
   * individual file records to it. So we allocate it here once so its available
   * during the whole NDMP session.
   */
  if (Bstrcasecmp(jcr->dir_impl->backup_format, "dump")) {
    Mmsg(virtual_filename, "/@NDMP%s%%%d", nis->filesystem,
         jcr->dir_impl->DumpLevel);
  } else {
    Mmsg(virtual_filename, "/@NDMP%s", nis->filesystem);
  }

  if (nis->virtual_filename) { free(nis->virtual_filename); }
  nis->virtual_filename = strdup(virtual_filename.c_str());

  // FIXME: disabled because of "missing media entry" error
  // if (!ndmp_validate_job(jcr, &ndmp_sess.control_acb->job)) {
  //   goto cleanup;
  //}

  // Commission the session for a run.
  if (ndma_session_commission(&ndmp_sess)) { goto cleanup; }

  // Setup the DMA.
  if (ndmca_connect_control_agent(&ndmp_sess)) { goto cleanup; }

  ndmp_sess.conn_open = 1;
  ndmp_sess.conn_authorized = 1;

  RegisterCallbackHooks(&ndmp_sess.control_acb->job.index_log);

  // Let the DMA perform its magic.
  if (ndmca_control_agent(&ndmp_sess) != 0) { goto cleanup; }

  if (!unreserve_ndmp_tapedevice_for_job(store, jcr)) {
    Jmsg(jcr, M_ERROR, 0, "could not free ndmp tape device %s from job %d",
         ndmp_job.tape_device, jcr->JobId);
  }

  // See if there were any errors during the backup.
  jcr->dir_impl->jr.FileIndex = 1;
  if (!extract_post_backup_stats_ndmp_native(jcr, item, &ndmp_sess)) {
    goto cleanup;
  }
  UnregisterCallbackHooks(&ndmp_sess.control_acb->job.index_log);

  // Reset the NDMP session states.
  ndma_session_decommission(&ndmp_sess);

  // Cleanup the job after it has run.
  ndma_destroy_env_list(&ndmp_sess.control_acb->job.env_tab);
  ndma_destroy_env_list(&ndmp_sess.control_acb->job.result_env_tab);
  ndma_destroy_nlist(&ndmp_sess.control_acb->job.nlist_tab);

  // Destroy the session.
  ndma_session_destroy(&ndmp_sess);

  // Free the param block.
  free(ndmp_sess.param->log_tag);
  free(ndmp_sess.param);
  ndmp_sess.param = NULL;

  // Reset the initialized state so we don't try to cleanup again.
  session_initialized = false;


  status = JS_Terminated;
  retval = true;


  /*
   * If we do incremental backups it can happen that the backup is empty if
   * nothing changed but we always write a filestream. So we use the counter
   * which counts the number of actual NDMP backup sessions we run to
   * completion.
   */
  if (jcr->JobFiles == 0) { jcr->JobFiles = 1; }

  // Jump to the generic cleanup done for every Job.
  goto ok_out;

cleanup:

  if (!unreserve_ndmp_tapedevice_for_job(store, jcr)) {
    Jmsg(jcr, M_ERROR, 0, "could not free ndmp tape device %s from job %d",
         ndmp_job.tape_device, jcr->JobId);
  }

  // Only need to cleanup when things are initialized.
  if (session_initialized) {
    ndma_destroy_env_list(&ndmp_sess.control_acb->job.env_tab);
    ndma_destroy_env_list(&ndmp_sess.control_acb->job.result_env_tab);
    ndma_destroy_nlist(&ndmp_sess.control_acb->job.nlist_tab);
    /*
       if (ndmp_sess.control_acb->job.tape_device) {
       free(ndmp_sess.control_acb->job.tape_device);
       }
       */
    UnregisterCallbackHooks(&ndmp_sess.control_acb->job.index_log);

    // Destroy the session.
    ndma_session_destroy(&ndmp_sess);
  }

  // Free the param block.
  free(ndmp_sess.param->log_tag);
  free(ndmp_sess.param);
  ndmp_sess.param = NULL;

bail_out:
  // Error handling of failed Job.
  status = JS_ErrorTerminated;
  jcr->setJobStatusWithPriorityCheck(JS_ErrorTerminated);

ok_out:
  if (nis) {
    if (nis->virtual_filename) { free(nis->virtual_filename); }
    free(nis);
  }

  if (status == JS_Terminated) { NdmpBackupCleanup(jcr, status); }

  return retval;
}

// Setup a NDMP backup session.
bool DoNdmpBackupInitNdmpNative(JobControlRecord* jcr)
{
  FreeRstorage(jcr); /* we don't read so release */

  if (!AllowDuplicateJob(jcr)) { return false; }

  jcr->dir_impl->jr.PoolId
      = GetOrCreatePoolRecord(jcr, jcr->dir_impl->res.pool->resource_name_);
  if (jcr->dir_impl->jr.PoolId == 0) { return false; }

  jcr->start_time = time(NULL);
  jcr->dir_impl->jr.StartTime = jcr->start_time;
  if (!jcr->db->UpdateJobStartRecord(jcr, &jcr->dir_impl->jr)) {
    Jmsg(jcr, M_FATAL, 0, "%s", jcr->db->strerror());
    return false;
  }

  // If pool storage specified, use it instead of job storage
  CopyWstorage(jcr, jcr->dir_impl->res.pool->storage, _("Pool resource"));

  if (!jcr->dir_impl->res.write_storage_list) {
    Jmsg(jcr, M_FATAL, 0,
         _("No Storage specification found in Job or Pool.\n"));
    return false;
  }

  // Validate the Job to have a NDMP client and NDMP storage.
  if (!NdmpValidateClient(jcr)) { return false; }

  if (!NdmpValidateStorage(jcr)) { return false; }

  return true;
}

// Extract any post backup statistics for native NDMP
static inline bool extract_post_backup_stats_ndmp_native(
    JobControlRecord* jcr,
    char* filesystem,
    struct ndm_session* sess)
{
  bool retval = true;
  struct ndmmedia* media;
  ndmp_backup_format_option* nbf_options;
  struct ndm_env_entry* ndm_ee;
  char mediabuf[100];
  // See if we know this backup format and get it options.
  nbf_options
      = ndmp_lookup_backup_format_options(sess->control_acb->job.bu_type);

  // See if an error was raised during the backup session.
  if (sess->error_raised) { return false; }

  // extract_post_backup_stats
  for (media = sess->control_acb->job.media_tab.head; media;
       media = media->next) {
    // translate Physical to Logical Slot before storing into database

    media->slot_addr = GetBareosSlotNumberByElementAddress(
        &jcr->dir_impl->res.write_storage->runtime_storage_status
             ->storage_mapping,
        slot_type_t::kSlotTypeStorage, media->slot_addr);
#  if 0
      Jmsg(jcr, M_INFO, 0, _("Physical Slot is %d\n"), media->slot_addr);
      Jmsg(jcr, M_INFO, 0, _("Logical slot is : %d\n"), media->slot_addr);
      Jmsg(jcr, M_INFO, 0, _("label           : %s\n"), media->label);
      Jmsg(jcr, M_INFO, 0, _("index           : %d\n"), media->index);
      Jmsg(jcr, M_INFO, 0, _("n_bytes         : %lld\n"), media->n_bytes);
      Jmsg(jcr, M_INFO, 0, _("begin_offset    : %u\n"), media->begin_offset);
      Jmsg(jcr, M_INFO, 0, _("end_offset      : %u\n"), media->end_offset);
#  endif

    StoreNdmmediaInfoInDatabase(media, jcr);

    ndmmedia_to_str(media, mediabuf);

    Jmsg(jcr, M_INFO, 0, _("Media: %s\n"), mediabuf);

    // See if there is any media error.
    if (media->media_open_error || media->media_io_error
        || media->label_io_error || media->label_mismatch
        || media->fmark_error) {
      retval = false;
    }
  }

  // Process the FHDB.
  ProcessFhdb(&sess->control_acb->job.index_log);

  // insert batched files into database

  jcr->db->WriteBatchFileRecords(jcr);
  // Update the Job statistics from the NDMP statistics.
  jcr->JobBytes += sess->control_acb->job.bytes_written;

  /*
   * After a successful backup we need to store all NDMP ENV variables
   * for doing a successful restore operation.
   */
  ndm_ee = sess->control_acb->job.result_env_tab.head;
  while (ndm_ee) {
    if (!jcr->db->CreateNdmpEnvironmentString(
            jcr, &jcr->dir_impl->jr, ndm_ee->pval.name, ndm_ee->pval.value)) {
      break;
    }
    ndm_ee = ndm_ee->next;
  }

  /*
   * If we are doing a backup type that uses dumplevels save the last used dump
   * level.
   */
  if (nbf_options && nbf_options->uses_level) {
    jcr->db->UpdateNdmpLevelMapping(jcr, &jcr->dir_impl->jr, filesystem,
                                    sess->control_acb->job.bu_level);
  }

  return retval;
}

#else

bool DoNdmpBackupInitNdmpNative(JobControlRecord* jcr)
{
  Jmsg(jcr, M_FATAL, 0, _("NDMP protocol not supported\n"));
  return false;
}

bool DoNdmpBackupNdmpNative(JobControlRecord* jcr)
{
  Jmsg(jcr, M_FATAL, 0, _("NDMP protocol not supported\n"));
  return false;
}

#endif /* HAVE_NDMP */
} /* namespace directordaemon */