/
ndmp_dma.c
3090 lines (2671 loc) · 85.8 KB
/
ndmp_dma.c
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2013 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.
*/
/*
* ndmp_dma.c implements the NDMP Data Management Application (DMA)
* which controls all NDMP backups and restores.
*
* Marco van Wieringen, May 2012
*/
#include "bareos.h"
#include "dird.h"
#if HAVE_NDMP
#include "ndmp/ndmagents.h"
#define B_PAGE_SIZE 4096
#define MAX_PAGES 2400
#define MAX_BUF_SIZE (MAX_PAGES * B_PAGE_SIZE) /* approx 10MB */
#define SMTAPE_MIN_BLOCKSIZE 4096 /* 4 Kb */
#define SMTAPE_MAX_BLOCKSIZE 262144 /* 256 Kb */
#define SMTAPE_BLOCKSIZE_INCREMENTS 4096 /* 4 Kb */
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
/* Imported variables */
/* Forward referenced functions */
/*
* Lightweight version of Bareos tree layout for holding the NDMP
* filehandle index database. See lib/tree.[ch] for the full version.
*/
struct ndmp_fhdb_mem {
struct ndmp_fhdb_mem *next; /* next buffer */
int rem; /* remaining bytes */
char *mem; /* memory pointer */
char first[1]; /* first byte */
};
struct ndmp_fhdb_node {
/*
* KEEP sibling as the first member to avoid having to do initialization of child
*/
rblink sibling;
rblist child;
char *fname; /* File name */
char *attr; /* Encoded stat struct */
int8_t FileType; /* Type of File */
int32_t FileIndex; /* File index */
int32_t Offset; /* File Offset in NDMP stream */
uint32_t inode; /* Inode nr */
uint16_t fname_len; /* Filename length */
ndmp_fhdb_node *next;
ndmp_fhdb_node *parent;
};
typedef struct ndmp_fhdb_node N_TREE_NODE;
struct ndmp_fhdb_root {
/*
* KEEP sibling as the first member to avoid having to do initialization of child
*/
rblink sibling;
rblist child;
char *fname; /* File name */
char *attr; /* Encoded stat struct */
int8_t FileType; /* Type of File */
int32_t FileIndex; /* File index */
int32_t Offset; /* File Offset in NDMP stream */
uint32_t inode; /* Inode nr */
uint16_t fname_len; /* Filename length */
ndmp_fhdb_node *next;
ndmp_fhdb_node *parent;
/*
* The above ^^^ must be identical to a ndmp_fhdb_node structure
* The below vvv is only for the root of the tree.
*/
ndmp_fhdb_node *first; /* first entry in the tree */
ndmp_fhdb_node *last; /* last entry in the tree */
ndmp_fhdb_mem *mem; /* tree memory */
uint32_t total_size; /* total bytes allocated */
uint32_t blocks; /* total mallocs */
ndmp_fhdb_node *cached_parent; /* cached parent */
};
typedef struct ndmp_fhdb_root N_TREE_ROOT;
/*
* Internal structure to keep track of private data.
*/
struct ndmp_internal_state {
uint32_t LogLevel;
JCR *jcr;
UAContext *ua;
char *filesystem;
int32_t FileIndex;
char *virtual_filename;
bool save_filehist;
N_TREE_ROOT *fhdb_root;
};
typedef struct ndmp_internal_state NIS;
static char OKbootstrap[] =
"3000 OK bootstrap\n";
/*
* Array used for storing fixed NDMP env keywords.
* Anything special should go into a so called meta-tag in the fileset options.
*/
static char *ndmp_env_keywords[] = {
(char *)"HIST",
(char *)"TYPE",
(char *)"DIRECT",
(char *)"LEVEL",
(char *)"UPDATE",
(char *)"EXCLUDE",
(char *)"INCLUDE",
(char *)"FILESYSTEM",
(char *)"PREFIX"
};
/*
* Index values for above keyword.
*/
enum {
NDMP_ENV_KW_HIST = 0,
NDMP_ENV_KW_TYPE,
NDMP_ENV_KW_DIRECT,
NDMP_ENV_KW_LEVEL,
NDMP_ENV_KW_UPDATE,
NDMP_ENV_KW_EXCLUDE,
NDMP_ENV_KW_INCLUDE,
NDMP_ENV_KW_FILESYSTEM,
NDMP_ENV_KW_PREFIX
};
/*
* Array used for storing fixed NDMP env values.
* Anything special should go into a so called meta-tag in the fileset options.
*/
static char *ndmp_env_values[] = {
(char *)"n",
(char *)"y"
};
/*
* Index values for above values.
*/
enum {
NDMP_ENV_VALUE_NO = 0,
NDMP_ENV_VALUE_YES
};
struct ndmp_backup_format_option {
char *format;
bool uses_file_history;
bool uses_level;
bool restore_prefix_relative;
bool needs_namelist;
};
static ndmp_backup_format_option ndmp_backup_format_options[] = {
{ (char *)"dump", true, true, true, true },
{ (char *)"tar", true, false, true, true },
{ (char *)"smtape", false, true, false, true },
{ (char *)"zfs", false, true, false, true },
{ NULL, false, false, false }
};
static ndmp_backup_format_option *lookup_backup_format_options(const char *backup_format)
{
int i = 0;
while (ndmp_backup_format_options[i].format) {
if (bstrcasecmp(backup_format, ndmp_backup_format_options[i].format)) {
break;
}
i++;
}
if (ndmp_backup_format_options[i].format) {
return &ndmp_backup_format_options[i];
}
return (ndmp_backup_format_option *)NULL;
}
/*
* Lightweight version of Bareos tree functions for holding the NDMP
* filehandle index database. See lib/tree.[ch] for the full version.
*/
static void malloc_buf(N_TREE_ROOT *root, int size)
{
struct ndmp_fhdb_mem *mem;
mem = (struct ndmp_fhdb_mem *)malloc(size);
root->total_size += size;
root->blocks++;
mem->next = root->mem;
root->mem = mem;
mem->mem = mem->first;
mem->rem = (char *)mem + size - mem->mem;
}
/*
* Note, we allocate a big buffer in the tree root from which we
* allocate nodes. This runs more than 100 times as fast as directly
* using malloc() for each of the nodes.
*/
static inline N_TREE_ROOT *ndmp_fhdb_new_tree()
{
int count = 512;
N_TREE_ROOT *root;
uint32_t size;
root = (N_TREE_ROOT *)malloc(sizeof(N_TREE_ROOT));
memset(root, 0, sizeof(N_TREE_ROOT));
/*
* Assume filename + node = 40 characters average length
*/
size = count * (BALIGN(sizeof(N_TREE_ROOT)) + 40);
if (count > 1000000 || size > (MAX_BUF_SIZE / 2)) {
size = MAX_BUF_SIZE;
}
Dmsg2(400, "count=%d size=%d\n", count, size);
malloc_buf(root, size);
return root;
}
/*
* Allocate bytes for filename in tree structure.
* Keep the pointers properly aligned by allocating sizes that are aligned.
*/
static inline char *ndmp_fhdb_tree_alloc(N_TREE_ROOT *root, int size)
{
char *buf;
int asize = BALIGN(size);
if (root->mem->rem < asize) {
uint32_t mb_size;
if (root->total_size >= (MAX_BUF_SIZE / 2)) {
mb_size = MAX_BUF_SIZE;
} else {
mb_size = MAX_BUF_SIZE / 2;
}
malloc_buf(root, mb_size);
}
root->mem->rem -= asize;
buf = root->mem->mem;
root->mem->mem += asize;
return buf;
}
/*
* This routine can be called to release the previously allocated tree node.
*/
static inline void ndmp_fhdb_free_tree_node(N_TREE_ROOT *root)
{
int asize = BALIGN(sizeof(N_TREE_NODE));
root->mem->rem += asize;
root->mem->mem -= asize;
}
/*
* Create a new tree node.
*/
static N_TREE_NODE *ndmp_fhdb_new_tree_node(N_TREE_ROOT *root)
{
N_TREE_NODE *node;
int size = sizeof(N_TREE_NODE);
node = (N_TREE_NODE *)ndmp_fhdb_tree_alloc(root, size);
memset(node, 0, size);
return node;
}
/*
* This routine frees the whole tree
*/
static inline void ndmp_fhdb_free_tree(N_TREE_ROOT *root)
{
struct ndmp_fhdb_mem *mem, *rel;
uint32_t freed_blocks = 0;
for (mem = root->mem; mem; ) {
rel = mem;
mem = mem->next;
free(rel);
freed_blocks++;
}
Dmsg3(100, "Total size=%u blocks=%u freed_blocks=%u\n", root->total_size, root->blocks, freed_blocks);
free(root);
garbage_collect_memory();
return;
}
static int node_compare_by_name(void *item1, void *item2)
{
N_TREE_NODE *tn1 = (N_TREE_NODE *)item1;
N_TREE_NODE *tn2 = (N_TREE_NODE *)item2;
if (tn1->fname[0] > tn2->fname[0]) {
return 1;
} else if (tn1->fname[0] < tn2->fname[0]) {
return -1;
}
return strcmp(tn1->fname, tn2->fname);
}
static int node_compare_by_id(void *item1, void *item2)
{
N_TREE_NODE *tn1 = (N_TREE_NODE *)item1;
N_TREE_NODE *tn2 = (N_TREE_NODE *)item2;
if (tn1->inode > tn2->inode) {
return 1;
} else if (tn1->inode < tn2->inode) {
return -1;
} else {
return 0;
}
}
static inline N_TREE_NODE *search_and_insert_tree_node(char *fname, int32_t FileIndex, uint32_t inode,
N_TREE_ROOT *root, N_TREE_NODE *parent)
{
N_TREE_NODE *node, *found_node;
node = ndmp_fhdb_new_tree_node(root);
if (inode) {
node->inode = inode;
found_node = (N_TREE_NODE *)parent->child.insert(node, node_compare_by_id);
} else {
node->fname = fname;
found_node = (N_TREE_NODE *)parent->child.insert(node, node_compare_by_name);
}
/*
* Already in list ?
*/
if (found_node != node) {
/*
* Free node allocated above.
*/
ndmp_fhdb_free_tree_node(root);
return found_node;
}
/*
* Its was not found, but now inserted.
*
* Allocate a new entry with 2 bytes extra e.g. the extra slash
* needed for directories and the \0.
*/
node->FileIndex = FileIndex;
node->fname_len = strlen(fname);
node->fname = ndmp_fhdb_tree_alloc(root, node->fname_len + 2);
bstrncpy(node->fname, fname, node->fname_len + 1);
node->parent = parent;
/*
* Maintain a linear chain of nodes.
*/
if (!root->first) {
root->first = node;
root->last = node;
} else {
root->last->next = node;
root->last = node;
}
return node;
}
/*
* Recursively search the tree for a certain inode number.
*/
static inline N_TREE_NODE *find_tree_node(N_TREE_NODE *node, uint32_t inode)
{
N_TREE_NODE match_node;
N_TREE_NODE *found_node, *walker;
match_node.inode = inode;
/*
* Start searching in the children of this node.
*/
found_node = (N_TREE_NODE *)node->child.search(&match_node, node_compare_by_id);
if (found_node) {
return found_node;
}
/*
* The node we are searching for is not one of the top nodes so need to search deeper.
*/
foreach_rblist(walker, &node->child) {
/*
* See if the node has any children otherwise no need to search it.
*/
if (walker->child.empty()) {
continue;
}
found_node = find_tree_node(walker, inode);
if (found_node) {
return found_node;
}
}
return (N_TREE_NODE *)NULL;
}
/*
* Recursively search the tree for a certain inode number.
*/
static inline N_TREE_NODE *find_tree_node(N_TREE_ROOT *root, uint32_t inode)
{
N_TREE_NODE match_node;
N_TREE_NODE *found_node, *walker;
/*
* See if this is a request for the root of the tree.
*/
if (root->inode == inode) {
return (N_TREE_NODE *)root;
}
match_node.inode = inode;
/*
* First do the easy lookup e.g. is this inode part of the parent of the current parent.
*/
if (root->cached_parent && root->cached_parent->parent) {
found_node = (N_TREE_NODE *)root->cached_parent->parent->child.search(&match_node, node_compare_by_id);
if (found_node) {
return found_node;
}
}
/*
* Start searching from the root node.
*/
found_node = (N_TREE_NODE *)root->child.search(&match_node, node_compare_by_id);
if (found_node) {
return found_node;
}
/*
* The node we are searching for is not one of the top nodes so need to search deeper.
*/
foreach_rblist(walker, &root->child) {
/*
* See if the node has any children otherwise no need to search it.
*/
if (walker->child.empty()) {
continue;
}
found_node = find_tree_node(walker, inode);
if (found_node) {
return found_node;
}
}
return (N_TREE_NODE *)NULL;
}
static inline bool validate_client(JCR *jcr)
{
switch (jcr->res.client->Protocol) {
case APT_NDMPV2:
case APT_NDMPV3:
case APT_NDMPV4:
if (jcr->res.client->password.encoding != p_encoding_clear) {
Jmsg(jcr, M_FATAL, 0,
_("Client %s, has incompatible password encoding for running NDMP backup.\n"),
jcr->res.client->name());
return false;
}
break;
default:
Jmsg(jcr, M_FATAL, 0,
_("Client %s, with backup protocol %s not compatible for running NDMP backup.\n"),
jcr->res.client->name(), auth_protocol_to_str(jcr->res.client->Protocol));
return false;
}
return true;
}
static inline bool validate_storage(JCR *jcr, STORERES *store)
{
switch (store->Protocol) {
case APT_NDMPV2:
case APT_NDMPV3:
case APT_NDMPV4:
if (store->password.encoding != p_encoding_clear) {
Jmsg(jcr, M_FATAL, 0,
_("Storage %s, has incompatible password encoding for running NDMP backup.\n"),
store->name());
return false;
}
break;
default:
Jmsg(jcr, M_FATAL, 0, _("Storage %s has illegal backup protocol %s for NDMP backup\n"),
store->name(), auth_protocol_to_str(store->Protocol));
return false;
}
return true;
}
static inline bool validate_storage(JCR *jcr)
{
STORERES *store;
if (jcr->wstorage) {
foreach_alist(store, jcr->wstorage) {
if (!validate_storage(jcr, store)) {
return false;
}
}
} else {
foreach_alist(store, jcr->rstorage) {
if (!validate_storage(jcr, store)) {
return false;
}
}
}
return true;
}
/*
* Fill a ndmagent structure with the correct info. Instead of calling ndmagent_from_str
* we fill the structure ourself from info provides in a resource.
*/
static inline bool fill_ndmp_agent_config(JCR *jcr,
struct ndmagent *agent,
uint32_t protocol,
uint32_t authtype,
char *address,
uint32_t port,
char *username,
char *password)
{
agent->conn_type = NDMCONN_TYPE_REMOTE;
switch (protocol) {
case APT_NDMPV2:
agent->protocol_version = 2;
break;
case APT_NDMPV3:
agent->protocol_version = 3;
break;
case APT_NDMPV4:
agent->protocol_version = 4;
break;
default:
Jmsg(jcr, M_FATAL, 0, _("Illegal protocol %d for NDMP Job\n"), protocol);
return false;
}
switch (authtype) {
case AT_NONE:
agent->auth_type = 'n';
break;
case AT_CLEAR:
agent->auth_type = 't';
break;
case AT_MD5:
agent->auth_type = 'm';
break;
case AT_VOID:
agent->auth_type = 'v';
break;
default:
Jmsg(jcr, M_FATAL, 0, _("Illegal authtype %d for NDMP Job\n"), authtype);
return false;
}
agent->port = port;
bstrncpy(agent->host, address, sizeof(agent->host));
bstrncpy(agent->account, username, sizeof(agent->account));
bstrncpy(agent->password, password, sizeof(agent->password));
return true;
}
static inline int native_to_ndmp_level(JCR *jcr, char *filesystem)
{
int level = -1;
if (!db_create_ndmp_level_mapping(jcr, jcr->db, &jcr->jr, filesystem)) {
return -1;
}
switch (jcr->getJobLevel()) {
case L_FULL:
level = 0;
break;
case L_DIFFERENTIAL:
level = 1;
break;
case L_INCREMENTAL:
level = db_get_ndmp_level_mapping(jcr, jcr->db, &jcr->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;
}
/*
* Parse a meta-tag and convert it into a ndmp_pval
*/
static inline void parse_meta_tag(struct ndm_env_table *env_tab,
char *meta_tag)
{
char *p;
ndmp9_pval pv;
/*
* See if the meta-tag is parseable.
*/
if ((p = strchr(meta_tag, '=')) == NULL) {
return;
}
/*
* Split the tag on the '='
*/
*p = '\0';
pv.name = meta_tag;
pv.value = p + 1;
ndma_update_env_list(env_tab, &pv);
/*
* Restore the '='
*/
*p = '=';
}
/*
* Fill the NDMP backup environment table with the data for the data agent to act on.
*/
static inline bool fill_backup_environment(JCR *jcr,
INCEXE *ie,
char *filesystem,
struct ndm_job_param *job)
{
int i, j, cnt;
bool exclude;
FOPTS *fo;
ndmp9_pval pv;
POOL_MEM pattern;
POOL_MEM tape_device;
ndmp_backup_format_option *nbf_options;
/*
* See if we know this backup format and get it options.
*/
nbf_options = 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->DumpLevel = native_to_ndmp_level(jcr, filesystem);
job->bu_level = jcr->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->num_opts; i++) {
fo = ie->opts_list[i];
/*
* Pickup any interesting patterns.
*/
cnt = 0;
pm_strcpy(pattern, "");
for (j = 0; j < fo->wild.size(); j++) {
if (cnt != 0) {
pm_strcat(pattern, ",");
}
pm_strcat(pattern, (char *)fo->wild.get(j));
cnt++;
}
for (j = 0; j < fo->wildfile.size(); j++) {
if (cnt != 0) {
pm_strcat(pattern, ",");
}
pm_strcat(pattern, (char *)fo->wildfile.get(j));
cnt++;
}
for (j = 0; j < fo->wilddir.size(); j++) {
if (cnt != 0) {
pm_strcat(pattern, ",");
}
pm_strcat(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++) {
parse_meta_tag(&job->env_tab, (char *)fo->meta.get(j));
}
}
/*
* If we have a paired storage definition we put the storage daemon
* auth key and the filesystem 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) {
Mmsg(tape_device, "%s@%s", jcr->sd_auth_key, filesystem);
job->tape_device = bstrdup(tape_device.c_str());
}
return true;
}
/*
* Walk the tree of selected files for restore and lookup the
* correct fileid. Return the actual full pathname of the file
* corresponding to the given fileid.
*/
static inline char *lookup_fileindex(JCR *jcr, int32_t FileIndex)
{
TREE_NODE *node, *parent;
POOL_MEM restore_pathname, tmp;
node = first_tree_node(jcr->restore_tree_root);
while (node) {
/*
* See if this is the wanted FileIndex.
*/
if (node->FileIndex == FileIndex) {
pm_strcpy(restore_pathname, node->fname);
/*
* Walk up the parent until we hit the head of the list.
*/
for (parent = node->parent; parent; parent = parent->parent) {
pm_strcpy(tmp, restore_pathname.c_str());
Mmsg(restore_pathname, "%s/%s", parent->fname, tmp.c_str());
}
if (bstrncmp(restore_pathname.c_str(), "/@NDMP/", 7)) {
return bstrdup(restore_pathname.c_str());
}
}
node = next_tree_node(node);
}
return NULL;
}
/*
* 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.
*/
static inline void add_to_namelist(struct ndm_job_param *job,
char *filename,
char *restore_prefix,
char *name,
char *other_name,
int64_t node)
{
ndmp9_name nl;
POOL_MEM destination_path;
memset(&nl, 0, sizeof(ndmp9_name));
/*
* See if the filename is an absolute pathname.
*/
if (*filename == '\0') {
pm_strcpy(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;
nl.node = node;
ndma_store_nlist(&job->nlist_tab, &nl);
}
/*
* See in the tree with selected files what files were selected to be restored.
*/
static inline int set_files_to_restore(JCR *jcr, struct ndm_job_param *job,
int32_t FileIndex, char *restore_prefix)
{
int len;
int cnt = 0;
TREE_NODE *node, *parent;
POOL_MEM restore_pathname, tmp;
node = first_tree_node(jcr->restore_tree_root);
while (node) {
/*
* See if this is the wanted FileIndex and the user asked to extract it.
*/
if (node->FileIndex == FileIndex && node->extract) {
pm_strcpy(restore_pathname, node->fname);
/*
* Walk up the parent until we hit the head of the list.
*/
for (parent = node->parent; parent; parent = parent->parent) {
pm_strcpy(tmp, restore_pathname.c_str());
Mmsg(restore_pathname, "%s/%s", parent->fname, tmp.c_str());
}
/*
* We only want to restore the non pseudo NDMP names e.g. not the full backup stream name.
*/
if (!bstrncmp(restore_pathname.c_str(), "/@NDMP/", 7)) {
/*
* See if we need to strip the prefix from the filename.
*/
len = strlen(restore_prefix);
if (bstrncmp(restore_pathname.c_str(), restore_prefix, len)) {
add_to_namelist(job, restore_pathname.c_str() + len, restore_prefix,
(char *)"", (char *)"", NDMP_INVALID_U_QUAD);
} else {
add_to_namelist(job, restore_pathname.c_str(), restore_prefix,
(char *)"", (char *)"", NDMP_INVALID_U_QUAD);
}
cnt++;
}
}
node = next_tree_node(node);
}
return cnt;
}