forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore_main.cpp
1794 lines (1634 loc) · 51.5 KB
/
restore_main.cpp
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
/*
Copyright (c) 2003, 2021, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
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 General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ndb_global.h>
#include <ndb_opts.h>
#include <Vector.hpp>
#include <Properties.hpp>
#include <ndb_limits.h>
#include <NdbTCP.h>
#include <NdbMem.h>
#include <NdbOut.hpp>
#include <OutputStream.hpp>
#include <NDBT_ReturnCodes.h>
#include "consumer_restore.hpp"
#include "consumer_printer.hpp"
#include "../src/ndbapi/NdbDictionaryImpl.hpp"
#define TMP_TABLE_PREFIX "#sql"
#define TMP_TABLE_PREFIX_LEN 4
extern FilteredNdbOut err;
extern FilteredNdbOut info;
extern FilteredNdbOut debug;
static Uint32 g_tableCompabilityMask = 0;
static int ga_nodeId = 0;
static int ga_nParallelism = 128;
static int ga_backupId = 0;
bool ga_dont_ignore_systab_0 = false;
static bool ga_no_upgrade = false;
static bool ga_promote_attributes = false;
static bool ga_demote_attributes = false;
static Vector<class BackupConsumer *> g_consumers;
static BackupPrinter* g_printer = NULL;
static const char* default_backupPath = "." DIR_SEPARATOR;
static const char* ga_backupPath = default_backupPath;
static const char *opt_nodegroup_map_str= 0;
static unsigned opt_nodegroup_map_len= 0;
static NODE_GROUP_MAP opt_nodegroup_map[MAX_NODE_GROUP_MAPS];
#define OPT_NDB_NODEGROUP_MAP 'z'
const char *opt_ndb_database= NULL;
const char *opt_ndb_table= NULL;
unsigned int opt_verbose;
unsigned int opt_hex_format;
unsigned int opt_progress_frequency;
NDB_TICKS g_report_prev;
Vector<BaseString> g_databases;
Vector<BaseString> g_tables;
Vector<BaseString> g_include_tables, g_exclude_tables;
Vector<BaseString> g_include_databases, g_exclude_databases;
Properties g_rewrite_databases;
NdbRecordPrintFormat g_ndbrecord_print_format;
unsigned int opt_no_binlog;
class RestoreOption
{
public:
virtual ~RestoreOption() { }
int optid;
BaseString argument;
};
Vector<class RestoreOption *> g_include_exclude;
static void save_include_exclude(int optid, char * argument);
static inline void parse_rewrite_database(char * argument);
/**
* print and restore flags
*/
static bool ga_restore_epoch = false;
static bool ga_restore = false;
static bool ga_print = false;
static bool ga_skip_table_check = false;
static bool ga_exclude_missing_columns = false;
static bool ga_exclude_missing_tables = false;
static bool opt_exclude_intermediate_sql_tables = true;
#ifdef ERROR_INSERT
static unsigned int _error_insert = 0;
#endif
static int _print = 0;
static int _print_meta = 0;
static int _print_data = 0;
static int _print_log = 0;
static int _restore_data = 0;
static int _restore_meta = 0;
static int _no_restore_disk = 0;
static bool _preserve_trailing_spaces = false;
static bool ga_disable_indexes = false;
static bool ga_rebuild_indexes = false;
bool ga_skip_unknown_objects = false;
bool ga_skip_broken_objects = false;
BaseString g_options("ndb_restore");
const char *load_default_groups[]= { "mysql_cluster","ndb_restore",0 };
enum ndb_restore_options {
OPT_VERBOSE = NDB_STD_OPTIONS_LAST,
OPT_INCLUDE_TABLES,
OPT_EXCLUDE_TABLES,
OPT_INCLUDE_DATABASES,
OPT_EXCLUDE_DATABASES,
OPT_REWRITE_DATABASE
};
static const char *opt_fields_enclosed_by= NULL;
static const char *opt_fields_terminated_by= NULL;
static const char *opt_fields_optionally_enclosed_by= NULL;
static const char *opt_lines_terminated_by= NULL;
static const char *tab_path= NULL;
static int opt_append;
static const char *opt_exclude_tables= NULL;
static const char *opt_include_tables= NULL;
static const char *opt_exclude_databases= NULL;
static const char *opt_include_databases= NULL;
static const char *opt_rewrite_database= NULL;
static bool opt_restore_privilege_tables = false;
static struct my_option my_long_options[] =
{
NDB_STD_OPTS("ndb_restore"),
{ "connect", 'c', "same as --connect-string",
(uchar**) &opt_ndb_connectstring, (uchar**) &opt_ndb_connectstring, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "nodeid", 'n', "Backup files from node with id",
(uchar**) &ga_nodeId, (uchar**) &ga_nodeId, 0,
GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "backupid", 'b', "Backup id",
(uchar**) &ga_backupId, (uchar**) &ga_backupId, 0,
GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "restore_data", 'r',
"Restore table data/logs into NDB Cluster using NDBAPI",
(uchar**) &_restore_data, (uchar**) &_restore_data, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "restore_meta", 'm',
"Restore meta data into NDB Cluster using NDBAPI",
(uchar**) &_restore_meta, (uchar**) &_restore_meta, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "no-upgrade", 'u',
"Don't upgrade array type for var attributes, which don't resize VAR data and don't change column attributes",
(uchar**) &ga_no_upgrade, (uchar**) &ga_no_upgrade, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "promote-attributes", 'A',
"Allow attributes to be promoted when restoring data from backup",
(uchar**) &ga_promote_attributes, (uchar**) &ga_promote_attributes, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "lossy-conversions", 'L',
"Allow lossy conversions for attributes (type demotions or integral"
" signed/unsigned type changes) when restoring data from backup",
(uchar**) &ga_demote_attributes, (uchar**) &ga_demote_attributes, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "preserve-trailing-spaces", 'P',
"Allow to preserve the tailing spaces (including paddings) When char->varchar or binary->varbinary is promoted",
(uchar**) &_preserve_trailing_spaces, (uchar**)_preserve_trailing_spaces , 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "no-restore-disk-objects", 'd',
"Dont restore disk objects (tablespace/logfilegroups etc)",
(uchar**) &_no_restore_disk, (uchar**) &_no_restore_disk, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "restore_epoch", 'e',
"Restore epoch info into the status table. Convenient on a MySQL Cluster "
"replication slave, for starting replication. The row in "
NDB_REP_DB "." NDB_APPLY_TABLE " with id 0 will be updated/inserted.",
(uchar**) &ga_restore_epoch, (uchar**) &ga_restore_epoch, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "skip-table-check", 's', "Skip table structure check during restore of data",
(uchar**) &ga_skip_table_check, (uchar**) &ga_skip_table_check, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "parallelism", 'p',
"No of parallel transactions during restore of data."
"(parallelism can be 1 to 1024)",
(uchar**) &ga_nParallelism, (uchar**) &ga_nParallelism, 0,
GET_INT, REQUIRED_ARG, 128, 1, 1024, 0, 1, 0 },
{ "print", NDB_OPT_NOSHORT, "Print metadata, data and log to stdout",
(uchar**) &_print, (uchar**) &_print, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "print_data", NDB_OPT_NOSHORT, "Print data to stdout",
(uchar**) &_print_data, (uchar**) &_print_data, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "print_meta", NDB_OPT_NOSHORT, "Print meta data to stdout",
(uchar**) &_print_meta, (uchar**) &_print_meta, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "print_log", NDB_OPT_NOSHORT, "Print log to stdout",
(uchar**) &_print_log, (uchar**) &_print_log, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "backup_path", NDB_OPT_NOSHORT, "Path to backup files",
(uchar**) &ga_backupPath, (uchar**) &ga_backupPath, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "dont_ignore_systab_0", 'f',
"Do not ignore system table during --print-data.",
(uchar**) &ga_dont_ignore_systab_0, (uchar**) &ga_dont_ignore_systab_0, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "ndb-nodegroup-map", OPT_NDB_NODEGROUP_MAP,
"Nodegroup map for ndbcluster. Syntax: list of (source_ng, dest_ng)",
(uchar**) &opt_nodegroup_map_str,
(uchar**) &opt_nodegroup_map_str,
0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "fields-enclosed-by", NDB_OPT_NOSHORT,
"Fields are enclosed by ...",
(uchar**) &opt_fields_enclosed_by, (uchar**) &opt_fields_enclosed_by, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "fields-terminated-by", NDB_OPT_NOSHORT,
"Fields are terminated by ...",
(uchar**) &opt_fields_terminated_by,
(uchar**) &opt_fields_terminated_by, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "fields-optionally-enclosed-by", NDB_OPT_NOSHORT,
"Fields are optionally enclosed by ...",
(uchar**) &opt_fields_optionally_enclosed_by,
(uchar**) &opt_fields_optionally_enclosed_by, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "hex", NDB_OPT_NOSHORT, "print binary types in hex format",
(uchar**) &opt_hex_format, (uchar**) &opt_hex_format, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "tab", 'T', "Creates tab separated textfile for each table to "
"given path. (creates .txt files)",
(uchar**) &tab_path, (uchar**) &tab_path, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{ "append", NDB_OPT_NOSHORT, "for --tab append data to file",
(uchar**) &opt_append, (uchar**) &opt_append, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "lines-terminated-by", NDB_OPT_NOSHORT, "",
(uchar**) &opt_lines_terminated_by, (uchar**) &opt_lines_terminated_by, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "progress-frequency", NDB_OPT_NOSHORT,
"Print status uf restore periodically in given seconds",
(uchar**) &opt_progress_frequency, (uchar**) &opt_progress_frequency, 0,
GET_INT, REQUIRED_ARG, 0, 0, 65535, 0, 0, 0 },
{ "no-binlog", NDB_OPT_NOSHORT,
"If a mysqld is connected and has binary log, do not log the restored data",
(uchar**) &opt_no_binlog, (uchar**) &opt_no_binlog, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "verbose", OPT_VERBOSE,
"verbosity",
(uchar**) &opt_verbose, (uchar**) &opt_verbose, 0,
GET_INT, REQUIRED_ARG, 1, 0, 255, 0, 0, 0 },
{ "include-databases", OPT_INCLUDE_DATABASES,
"Comma separated list of databases to restore. Example: db1,db3",
(uchar**) &opt_include_databases, (uchar**) &opt_include_databases, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "exclude-databases", OPT_EXCLUDE_DATABASES,
"Comma separated list of databases to not restore. Example: db1,db3",
(uchar**) &opt_exclude_databases, (uchar**) &opt_exclude_databases, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "rewrite-database", OPT_REWRITE_DATABASE,
"A pair 'source,dest' of database names from/into which to restore. "
"Example: --rewrite-database=oldDb,newDb",
(uchar**) &opt_rewrite_database, (uchar**) &opt_rewrite_database, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "include-tables", OPT_INCLUDE_TABLES, "Comma separated list of tables to "
"restore. Table name should include database name. Example: db1.t1,db3.t1",
(uchar**) &opt_include_tables, (uchar**) &opt_include_tables, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "exclude-tables", OPT_EXCLUDE_TABLES, "Comma separated list of tables to "
"not restore. Table name should include database name. "
"Example: db1.t1,db3.t1",
(uchar**) &opt_exclude_tables, (uchar**) &opt_exclude_tables, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
{ "restore-privilege-tables", NDB_OPT_NOSHORT,
"Restore privilege tables (after they have been moved to ndb)",
(uchar**) &opt_restore_privilege_tables,
(uchar**) &opt_restore_privilege_tables, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "exclude-missing-columns", NDB_OPT_NOSHORT,
"Ignore columns present in backup but not in database",
(uchar**) &ga_exclude_missing_columns,
(uchar**) &ga_exclude_missing_columns, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "exclude-missing-tables", NDB_OPT_NOSHORT,
"Ignore tables present in backup but not in database",
(uchar**) &ga_exclude_missing_tables,
(uchar**) &ga_exclude_missing_tables, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "exclude-intermediate-sql-tables", NDB_OPT_NOSHORT,
"Do not restore intermediate tables with #sql-prefixed names",
(uchar**) &opt_exclude_intermediate_sql_tables,
(uchar**) &opt_exclude_intermediate_sql_tables, 0,
GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0 },
{ "disable-indexes", NDB_OPT_NOSHORT,
"Disable indexes and foreign keys",
(uchar**) &ga_disable_indexes,
(uchar**) &ga_disable_indexes, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "rebuild-indexes", NDB_OPT_NOSHORT,
"Rebuild indexes",
(uchar**) &ga_rebuild_indexes,
(uchar**) &ga_rebuild_indexes, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "skip-unknown-objects", 256, "Skip unknown object when parsing backup",
(uchar**) &ga_skip_unknown_objects, (uchar**) &ga_skip_unknown_objects, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
{ "skip-broken-objects", 256, "Skip broken object when parsing backup",
(uchar**) &ga_skip_broken_objects, (uchar**) &ga_skip_broken_objects, 0,
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
#ifdef ERROR_INSERT
{ "error-insert", NDB_OPT_NOSHORT,
"Insert errors (testing option)",
(uchar **)&_error_insert, (uchar **)&_error_insert, 0,
GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
#endif
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
};
static char* analyse_one_map(char *map_str, uint16 *source, uint16 *dest)
{
char *end_ptr;
int number;
DBUG_ENTER("analyse_one_map");
/*
Search for pattern ( source_ng , dest_ng )
*/
while (isspace(*map_str)) map_str++;
if (*map_str != '(')
{
DBUG_RETURN(NULL);
}
map_str++;
while (isspace(*map_str)) map_str++;
number= strtol(map_str, &end_ptr, 10);
if (!end_ptr || number < 0 || number >= MAX_NODE_GROUP_MAPS)
{
DBUG_RETURN(NULL);
}
*source= (uint16)number;
map_str= end_ptr;
while (isspace(*map_str)) map_str++;
if (*map_str != ',')
{
DBUG_RETURN(NULL);
}
map_str++;
number= strtol(map_str, &end_ptr, 10);
if (!end_ptr || number < 0 || number >= NDB_UNDEF_NODEGROUP)
{
DBUG_RETURN(NULL);
}
*dest= (uint16)number;
map_str= end_ptr;
if (*map_str != ')')
{
DBUG_RETURN(NULL);
}
map_str++;
while (isspace(*map_str)) map_str++;
DBUG_RETURN(map_str);
}
static bool insert_ng_map(NODE_GROUP_MAP *ng_map,
uint16 source_ng, uint16 dest_ng)
{
uint index= source_ng;
uint ng_index= ng_map[index].no_maps;
opt_nodegroup_map_len++;
if (ng_index >= MAX_MAPS_PER_NODE_GROUP)
return true;
ng_map[index].no_maps++;
ng_map[index].map_array[ng_index]= dest_ng;
return false;
}
static void init_nodegroup_map()
{
uint i,j;
NODE_GROUP_MAP *ng_map = &opt_nodegroup_map[0];
for (i = 0; i < MAX_NODE_GROUP_MAPS; i++)
{
ng_map[i].no_maps= 0;
for (j= 0; j < MAX_MAPS_PER_NODE_GROUP; j++)
ng_map[i].map_array[j]= NDB_UNDEF_NODEGROUP;
}
}
static bool analyse_nodegroup_map(const char *ng_map_str,
NODE_GROUP_MAP *ng_map)
{
uint16 source_ng, dest_ng;
char *local_str= (char*)ng_map_str;
DBUG_ENTER("analyse_nodegroup_map");
do
{
if (!local_str)
{
DBUG_RETURN(TRUE);
}
local_str= analyse_one_map(local_str, &source_ng, &dest_ng);
if (!local_str)
{
DBUG_RETURN(TRUE);
}
if (insert_ng_map(ng_map, source_ng, dest_ng))
{
DBUG_RETURN(TRUE);
}
if (!(*local_str))
break;
} while (TRUE);
DBUG_RETURN(FALSE);
}
static void short_usage_sub(void)
{
ndb_short_usage_sub("[<path to backup files>]");
}
static void usage()
{
ndb_usage(short_usage_sub, load_default_groups, my_long_options);
}
static my_bool
get_one_option(int optid, const struct my_option *opt MY_ATTRIBUTE((unused)),
char *argument)
{
#ifndef NDEBUG
opt_debug= "d:t:O,/tmp/ndb_restore.trace";
#endif
ndb_std_get_one_option(optid, opt, argument);
switch (optid) {
case OPT_VERBOSE:
info.setThreshold(255-opt_verbose);
break;
case 'n':
if (ga_nodeId == 0)
{
err << "Error in --nodeid,-n setting, see --help";
exit(NDBT_ProgramExit(NDBT_WRONGARGS));
}
info.setLevel(254);
info << "Nodeid = " << ga_nodeId << endl;
break;
case 'b':
if (ga_backupId == 0)
{
err << "Error in --backupid,-b setting, see --help";
exit(NDBT_ProgramExit(NDBT_WRONGARGS));
}
info.setLevel(254);
info << "Backup Id = " << ga_backupId << endl;
break;
case OPT_NDB_NODEGROUP_MAP:
/*
This option is used to set a map from nodegroup in original cluster
to nodegroup in new cluster.
*/
opt_nodegroup_map_len= 0;
info.setLevel(254);
info << "Analyse node group map" << endl;
if (analyse_nodegroup_map(opt_nodegroup_map_str,
&opt_nodegroup_map[0]))
{
exit(NDBT_ProgramExit(NDBT_WRONGARGS));
}
break;
case OPT_INCLUDE_DATABASES:
case OPT_EXCLUDE_DATABASES:
case OPT_INCLUDE_TABLES:
case OPT_EXCLUDE_TABLES:
save_include_exclude(optid, argument);
break;
case OPT_REWRITE_DATABASE:
parse_rewrite_database(argument);
break;
}
return 0;
}
static const char* SCHEMA_NAME="/def/";
static const int SCHEMA_NAME_SIZE= 5;
int
makeInternalTableName(const BaseString &externalName,
BaseString& internalName)
{
// Make dbname.table1 into dbname/def/table1
Vector<BaseString> parts;
// Must contain a dot
if (externalName.indexOf('.') == -1)
return -1;
externalName.split(parts,".");
// .. and only 1 dot
if (parts.size() != 2)
return -1;
internalName.clear();
internalName.append(parts[0]); // db name
internalName.append(SCHEMA_NAME); // /def/
internalName.append(parts[1]); // table name
return 0;
}
void
processTableList(const char* str, Vector<BaseString> &lst)
{
// Process tables list like db1.t1,db2.t1 and exits when
// it finds problems.
Vector<BaseString> tmp;
unsigned int i;
/* Split passed string on comma into 2 BaseStrings in the vector */
BaseString(str).split(tmp,",");
for (i=0; i < tmp.size(); i++)
{
BaseString internalName;
if (makeInternalTableName(tmp[i], internalName))
{
info << "`" << tmp[i] << "` is not a valid tablename!" << endl;
exit(NDBT_ProgramExit(NDBT_WRONGARGS));
}
lst.push_back(internalName);
}
}
BaseString
makeExternalTableName(const BaseString &internalName)
{
// Make dbname/def/table1 into dbname.table1
BaseString externalName;
ssize_t idx = internalName.indexOf('/');
externalName = internalName.substr(0,idx);
externalName.append(".");
externalName.append(internalName.substr(idx + SCHEMA_NAME_SIZE,
internalName.length()));
return externalName;
}
#include "../../../../sql/ndb_dist_priv_util.h"
// Exclude privilege tables unless explicitely included
void
exclude_privilege_tables()
{
const char* table_name;
Ndb_dist_priv_util dist_priv;
while((table_name= dist_priv.iter_next_table()))
{
BaseString priv_tab;
priv_tab.assfmt("%s.%s", dist_priv.database(), table_name);
g_exclude_tables.push_back(priv_tab);
save_include_exclude(OPT_EXCLUDE_TABLES, (char *)priv_tab.c_str());
}
}
bool
readArguments(int *pargc, char*** pargv)
{
Uint32 i;
BaseString tmp;
debug << "Load defaults" << endl;
const char *load_default_groups[]= { "mysql_cluster","ndb_restore",0 };
init_nodegroup_map();
ndb_load_defaults(NULL,load_default_groups,pargc,pargv);
debug << "handle_options" << endl;
ndb_opt_set_usage_funcs(short_usage_sub, usage);
if (handle_options(pargc, pargv, my_long_options, get_one_option))
{
exit(NDBT_ProgramExit(NDBT_WRONGARGS));
}
for (i = 0; i < MAX_NODE_GROUP_MAPS; i++)
opt_nodegroup_map[i].curr_index = 0;
#if 0
/*
Test code written t{
o verify nodegroup mapping
*/
printf("Handled options successfully\n");
Uint16 map_ng[16];
Uint32 j;
for (j = 0; j < 4; j++)
{
for (i = 0; i < 4 ; i++)
map_ng[i] = i;
map_nodegroups(&map_ng[0], (Uint32)4);
for (i = 0; i < 4 ; i++)
printf("NG %u mapped to %u \n", i, map_ng[i]);
}
for (j = 0; j < 4; j++)
{
for (i = 0; i < 8 ; i++)
map_ng[i] = i >> 1;
map_nodegroups(&map_ng[0], (Uint32)8);
for (i = 0; i < 8 ; i++)
printf("NG %u mapped to %u \n", i >> 1, map_ng[i]);
}
exit(NDBT_ProgramExit(NDBT_WRONGARGS));
#endif
g_printer = new BackupPrinter(opt_nodegroup_map,
opt_nodegroup_map_len);
if (g_printer == NULL)
return false;
BackupRestore* restore = new BackupRestore(opt_ndb_connectstring,
opt_ndb_nodeid,
opt_nodegroup_map,
opt_nodegroup_map_len,
ga_nodeId,
ga_nParallelism);
if (restore == NULL)
{
delete g_printer;
g_printer = NULL;
return false;
}
if (_print)
{
ga_print = true;
ga_restore = true;
g_printer->m_print = true;
}
if (_print_meta)
{
ga_print = true;
g_printer->m_print_meta = true;
}
if (_print_data)
{
ga_print = true;
g_printer->m_print_data = true;
}
if (_print_log)
{
ga_print = true;
g_printer->m_print_log = true;
}
if (_restore_data)
{
ga_restore = true;
restore->m_restore = true;
}
if (_restore_meta)
{
// ga_restore = true;
restore->m_restore_meta = true;
if(ga_exclude_missing_tables)
{
//conflict in options
err << "Conflicting arguments found : "
<< "Cannot use `restore-meta` and "
<< "`exclude-missing-tables` together. Exiting..." << endl;
return false;
}
}
if (_no_restore_disk)
{
restore->m_no_restore_disk = true;
}
if (ga_no_upgrade)
{
restore->m_no_upgrade = true;
}
if (_preserve_trailing_spaces)
{
restore->m_preserve_trailing_spaces = true;
}
if (ga_restore_epoch)
{
restore->m_restore_epoch = true;
}
if (ga_disable_indexes)
{
restore->m_disable_indexes = true;
}
if (ga_rebuild_indexes)
{
restore->m_rebuild_indexes = true;
}
{
BackupConsumer * c = g_printer;
g_consumers.push_back(c);
}
{
BackupConsumer * c = restore;
g_consumers.push_back(c);
}
for (;;)
{
int i= 0;
if (ga_backupPath == default_backupPath)
{
// Set backup file path
if ((*pargv)[i] == NULL)
break;
ga_backupPath = (*pargv)[i++];
}
if ((*pargv)[i] == NULL)
break;
g_databases.push_back((*pargv)[i++]);
while ((*pargv)[i] != NULL)
{
g_tables.push_back((*pargv)[i++]);
}
break;
}
info.setLevel(254);
info << "backup path = " << ga_backupPath << endl;
if (g_databases.size() > 0)
{
info << "WARNING! Using deprecated syntax for selective object restoration." << endl;
info << "Please use --include-*/--exclude-* options in future." << endl;
info << "Restoring only from database " << g_databases[0].c_str() << endl;
if (g_tables.size() > 0)
{
info << "Restoring tables:";
}
for (unsigned i= 0; i < g_tables.size(); i++)
{
info << " " << g_tables[i].c_str();
}
if (g_tables.size() > 0)
info << endl;
}
if (ga_restore)
{
// Exclude privilege tables unless explicitely included
if (!opt_restore_privilege_tables)
exclude_privilege_tables();
// Move over old style arguments to include/exclude lists
if (g_databases.size() > 0)
{
BaseString tab_prefix, tab;
tab_prefix.append(g_databases[0].c_str());
tab_prefix.append(".");
if (g_tables.size() == 0)
{
g_include_databases.push_back(g_databases[0]);
save_include_exclude(OPT_INCLUDE_DATABASES,
(char *)g_databases[0].c_str());
}
for (unsigned i= 0; i < g_tables.size(); i++)
{
tab.assign(tab_prefix);
tab.append(g_tables[i]);
g_include_tables.push_back(tab);
save_include_exclude(OPT_INCLUDE_TABLES, (char *)tab.c_str());
}
}
}
if (opt_include_databases)
{
tmp = BaseString(opt_include_databases);
tmp.split(g_include_databases,",");
info << "Including Databases: ";
for (i= 0; i < g_include_databases.size(); i++)
{
info << g_include_databases[i] << " ";
}
info << endl;
}
if (opt_exclude_databases)
{
tmp = BaseString(opt_exclude_databases);
tmp.split(g_exclude_databases,",");
info << "Excluding databases: ";
for (i= 0; i < g_exclude_databases.size(); i++)
{
info << g_exclude_databases[i] << " ";
}
info << endl;
}
if (opt_rewrite_database)
{
info << "Rewriting databases:";
Properties::Iterator it(&g_rewrite_databases);
const char * src;
for (src = it.first(); src != NULL; src = it.next()) {
const char * dst = NULL;
bool r = g_rewrite_databases.get(src, &dst);
require(r && (dst != NULL));
info << " (" << src << "->" << dst << ")";
}
info << endl;
}
if (opt_include_tables)
{
processTableList(opt_include_tables, g_include_tables);
info << "Including tables: ";
for (i= 0; i < g_include_tables.size(); i++)
{
info << makeExternalTableName(g_include_tables[i]).c_str() << " ";
}
info << endl;
}
if (opt_exclude_tables)
{
processTableList(opt_exclude_tables, g_exclude_tables);
info << "Excluding tables: ";
for (i= 0; i < g_exclude_tables.size(); i++)
{
info << makeExternalTableName(g_exclude_tables[i]).c_str() << " ";
}
info << endl;
}
/*
the below formatting follows the formatting from mysqldump
do not change unless to adopt to changes in mysqldump
*/
g_ndbrecord_print_format.fields_enclosed_by=
opt_fields_enclosed_by ? opt_fields_enclosed_by : "";
g_ndbrecord_print_format.fields_terminated_by=
opt_fields_terminated_by ? opt_fields_terminated_by : "\t";
g_ndbrecord_print_format.fields_optionally_enclosed_by=
opt_fields_optionally_enclosed_by ? opt_fields_optionally_enclosed_by : "";
g_ndbrecord_print_format.lines_terminated_by=
opt_lines_terminated_by ? opt_lines_terminated_by : "\n";
if (g_ndbrecord_print_format.fields_optionally_enclosed_by[0] == '\0')
g_ndbrecord_print_format.null_string= "\\N";
else
g_ndbrecord_print_format.null_string= "";
g_ndbrecord_print_format.hex_prefix= "";
g_ndbrecord_print_format.hex_format= opt_hex_format;
if (ga_skip_table_check)
{
g_tableCompabilityMask = ~(Uint32)0;
ga_skip_unknown_objects = true;
}
if (ga_promote_attributes)
{
g_tableCompabilityMask |= TCM_ATTRIBUTE_PROMOTION;
}
if (ga_demote_attributes)
{
g_tableCompabilityMask |= TCM_ATTRIBUTE_DEMOTION;
}
if (ga_exclude_missing_columns)
{
g_tableCompabilityMask |= TCM_EXCLUDE_MISSING_COLUMNS;
}
return true;
}
void
clearConsumers()
{
for(Uint32 i= 0; i<g_consumers.size(); i++)
delete g_consumers[i];
g_consumers.clear();
}
static inline bool
checkSysTable(const TableS* table)
{
return ! table->getSysTable();
}
static inline bool
checkSysTable(const RestoreMetaData& metaData, uint i)
{
assert(i < metaData.getNoOfTables());
return checkSysTable(metaData[i]);
}
static inline bool
isBlobTable(const TableS* table)
{
return table->getMainTable() != NULL;
}
static inline bool
isIndex(const TableS* table)
{
const NdbTableImpl & tmptab = NdbTableImpl::getImpl(* table->m_dictTable);
return (int) tmptab.m_indexType != (int) NdbDictionary::Index::Undefined;
}
static inline bool
isSYSTAB_0(const TableS* table)
{
return table->isSYSTAB_0();
}
static inline bool
isInList(BaseString &needle, Vector<BaseString> &lst){
unsigned int i= 0;
for (i= 0; i < lst.size(); i++)
{
if (strcmp(needle.c_str(), lst[i].c_str()) == 0)
return true;
}
return false;
}
const char*
getTableName(const TableS* table)
{
const char *table_name;
if (isBlobTable(table))
table_name= table->getMainTable()->getTableName();
else if (isIndex(table))
table_name=
NdbTableImpl::getImpl(*table->m_dictTable).m_primaryTable.c_str();
else
table_name= table->getTableName();
return table_name;
}
static void parse_rewrite_database(char * argument)
{
const BaseString arg(argument);
Vector<BaseString> args;
unsigned int n = arg.split(args, ",");
if ((n == 2)
&& (args[0].length() > 0)
&& (args[1].length() > 0)) {
const BaseString src = args[0];
const BaseString dst = args[1];
const bool replace = true;
bool r = g_rewrite_databases.put(src.c_str(), dst.c_str(), replace);
require(r);
return; // ok
}
info << "argument `" << arg.c_str()
<< "` is not a pair 'a,b' of non-empty names." << endl;
exit(NDBT_ProgramExit(NDBT_WRONGARGS));
}
static void save_include_exclude(int optid, char * argument)
{
BaseString arg = argument;
Vector<BaseString> args;
arg.split(args, ",");
for (uint i = 0; i < args.size(); i++)
{
RestoreOption * option = new RestoreOption();
BaseString arg;
option->optid = optid;
switch (optid) {
case OPT_INCLUDE_TABLES:
case OPT_EXCLUDE_TABLES:
if (makeInternalTableName(args[i], arg))
{
info << "`" << args[i] << "` is not a valid tablename!" << endl;