-
Notifications
You must be signed in to change notification settings - Fork 437
/
mydumper_jobs.c
1152 lines (1030 loc) · 39.3 KB
/
mydumper_jobs.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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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 for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Authors: Domas Mituzas, Facebook ( domas at fb dot com )
Mark Leith, Oracle Corporation (mark dot leith at oracle dot com)
Andrew Hutchings, MariaDB Foundation (andrew at mariadb dot org)
Max Bubenick, Percona RDBA (max dot bubenick at percona dot com)
David Ducos, Percona (david dot ducos at percona dot com)
*/
#include <mysql.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <glib/gstdio.h>
#include <gio/gio.h>
#include "mydumper_start_dump.h"
#include "server_detect.h"
#include "common.h"
#include "regex.h"
#include "mydumper_common.h"
#include "mydumper_jobs.h"
#include "mydumper_database.h"
#include "mydumper_working_thread.h"
#include "mydumper_write.h"
#include "mydumper_chunks.h"
extern guint char_chunk;
extern gboolean load_data;
extern gchar *where_option;
extern gboolean success_on_1146;
extern int detected_server;
extern FILE * (*m_open)(const char *filename, const char *);
extern int (*m_close)(void *file);
extern guint errors;
extern guint statement_size;
extern int skip_tz;
extern gchar *set_names_str;
extern GAsyncQueue *stream_queue;
extern gboolean stream;
extern gboolean dump_routines;
extern gboolean dump_events;
extern gboolean use_savepoints;
extern gint database_counter;
extern GMutex *ready_database_dump_mutex;
//extern gint table_counter;
extern guint rows_per_file;
//extern gint non_innodb_table_counter;
extern GMutex *ready_table_dump_mutex;
gboolean dump_triggers = FALSE;
gboolean order_by_primary_key = FALSE;
gboolean ignore_generated_fields = FALSE;
gchar *exec_per_thread = NULL;
gchar *exec_per_thread_extension = NULL;
gboolean use_fifo = FALSE;
gchar **exec_per_thread_cmd=NULL;
extern gboolean schema_checksums;
extern gboolean routine_checksums;
extern gboolean use_fifo;
gboolean skip_definer = FALSE;
static GOptionEntry dump_into_file_entries[] = {
{"triggers", 'G', 0, G_OPTION_ARG_NONE, &dump_triggers, "Dump triggers. By default, it do not dump triggers",
NULL},
{ "no-check-generated-fields", 0, 0, G_OPTION_ARG_NONE, &ignore_generated_fields,
"Queries related to generated fields are not going to be executed."
"It will lead to restoration issues if you have generated columns", NULL },
{"order-by-primary", 0, 0, G_OPTION_ARG_NONE, &order_by_primary_key,
"Sort the data by Primary Key or Unique key if no primary key exists",
NULL},
{"exec-per-thread",0, 0, G_OPTION_ARG_STRING, &exec_per_thread,
"Set the command that will receive by STDIN and write in the STDOUT into the output file", NULL},
{"exec-per-thread-extension",0, 0, G_OPTION_ARG_STRING, &exec_per_thread_extension,
"Set the extension for the STDOUT file when --exec-per-thread is used", NULL},
{"skip-definer", 0, 0, G_OPTION_ARG_NONE, &skip_definer,
"Removes DEFINER from the CREATE statement. By default, statements are not modified", NULL},
{NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL}};
void load_dump_into_file_entries(GOptionGroup *main_group){
g_option_group_add_entries(main_group, dump_into_file_entries);
}
void initialize_jobs(){
initialize_database();
if (ignore_generated_fields)
g_warning("Queries related to generated fields are not going to be executed. It will lead to restoration issues if you have generated columns");
if (exec_per_thread_extension != NULL){
if(exec_per_thread == NULL)
g_error("--exec-per-thread needs to be set when --exec-per-thread-extension is used");
}
if (exec_per_thread!=NULL){
use_fifo=TRUE;
exec_per_thread_cmd=g_strsplit(exec_per_thread, " ", 0);
}
}
void write_checksum_into_file(MYSQL *conn, char *database, char *table, char *filename, gchar *fun()) {
int errn=0;
gchar * checksum=fun(conn, database, table, &errn);
if (errn != 0 && !(success_on_1146 && errn == 1146)) {
errors++;
return;
}
if (checksum == NULL)
checksum = g_strdup("0");
void *outfile = NULL;
outfile = g_fopen(filename, "w");
if (!outfile) {
g_critical("Error: DB: %s TABLE: %s Could not create output file %s (%d)",
database, table, filename, errno);
errors++;
return;
}
fprintf(outfile, "%s\n", checksum);
fclose(outfile);
if (stream) g_async_queue_push(stream_queue, g_strdup(filename));
g_free(checksum);
return;
}
void write_my_data_into_file(const char *filename, gchar * str){
FILE *table_meta = g_fopen(filename, "w");
write_file(table_meta, str, strlen(str));
fclose(table_meta);
}
void write_table_metadata_into_file(struct db_table * dbt){
char *filename = build_meta_filename(dbt->database->filename, dbt->table_filename, "metadata");
FILE *table_meta = g_fopen(filename, "w");
if (!table_meta) {
g_critical("Couldn't write table metadata file %s (%d)", filename, errno);
exit(EXIT_FAILURE);
}
fprintf(table_meta, "%"G_GUINT64_FORMAT"\n", dbt->rows);
fclose(table_meta);
if (stream) g_async_queue_push(stream_queue, g_strdup(filename));
g_free(filename);
}
gchar * get_tablespace_query(){
if ( get_product() == SERVER_TYPE_PERCONA || get_product() == SERVER_TYPE_MYSQL){
if ( get_major() == 5 && get_secondary() == 7)
return g_strdup("select NAME, PATH, FS_BLOCK_SIZE from information_schema.INNODB_SYS_TABLESPACES join information_schema.INNODB_SYS_DATAFILES using (space) where SPACE_TYPE='General' and NAME != 'mysql';");
if ( get_major() == 8 )
return g_strdup("select NAME,PATH,FS_BLOCK_SIZE,ENCRYPTION from information_schema.INNODB_TABLESPACES join information_schema.INNODB_DATAFILES using (space) where SPACE_TYPE='General' and NAME != 'mysql';");
}
return NULL;
}
void write_tablespace_definition_into_file(MYSQL *conn,char *filename){
void *outfile = NULL;
char *query = NULL;
MYSQL_RES *result = NULL;
MYSQL_ROW row;
outfile = m_open(filename,"w");
if (!outfile) {
g_critical("Error: Could not create output file %s (%d)",
filename, errno);
errors++;
return;
}
query=get_tablespace_query();
if (query == NULL ){
g_warning("Tablespace resquested, but not possible due to server version not supported");
return;
}
if (mysql_query(conn, query) || !(result = mysql_use_result(conn))) {
if (success_on_1146 && mysql_errno(conn) == 1146) {
g_warning("Error dumping create tablespace: %s",
mysql_error(conn));
} else {
g_critical("Error dumping create tablespace: %s",
mysql_error(conn));
errors++;
}
g_free(query);
return;
}
GString *statement = g_string_sized_new(statement_size);
while ((row = mysql_fetch_row(result))) {
g_string_printf(statement, "CREATE TABLESPACE `%s` ADD DATAFILE '%s' FILE_BLOCK_SIZE = %s ENGINE=INNODB;\n", row[0],row[1],row[2]);
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write tablespace data for %s", row[0]);
errors++;
return;
}
g_string_set_size(statement, 0);
}
}
void write_schema_definition_into_file(MYSQL *conn, char *database, char *filename, char *checksum_filename) {
void *outfile = NULL;
char *query = NULL;
MYSQL_RES *result = NULL;
MYSQL_ROW row;
outfile = m_open(filename,"w");
if (!outfile) {
g_critical("Error: DB: %s Could not create output file %s (%d)", database,
filename, errno);
errors++;
return;
}
GString *statement = g_string_sized_new(statement_size);
query = g_strdup_printf("SHOW CREATE DATABASE IF NOT EXISTS `%s`", database);
if (mysql_query(conn, query) || !(result = mysql_use_result(conn))) {
if (success_on_1146 && mysql_errno(conn) == 1146) {
g_warning("Error dumping create database (%s): %s", database,
mysql_error(conn));
} else {
g_critical("Error dumping create database (%s): %s", database,
mysql_error(conn));
errors++;
}
g_free(query);
return;
}
/* There should never be more than one row */
row = mysql_fetch_row(result);
g_string_append(statement, row[1]);
g_string_append(statement, ";\n");
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write create database for %s", database);
errors++;
}
g_free(query);
m_close(outfile);
if (stream) g_async_queue_push(stream_queue, g_strdup(filename));
g_string_free(statement, TRUE);
if (result)
mysql_free_result(result);
if (schema_checksums)
write_checksum_into_file(conn, database, NULL, checksum_filename, checksum_database_defaults);
return;
}
void write_table_definition_into_file(MYSQL *conn, char *database, char *table,
char *filename, char *checksum_filename, char *checksum_index_filename) {
void *outfile;
char *query = NULL;
MYSQL_RES *result = NULL;
MYSQL_ROW row;
outfile = m_open(filename,"w");
if (!outfile) {
g_critical("Error: DB: %s Could not create output file %s (%d)", database,
filename, errno);
errors++;
return;
}
GString *statement = g_string_sized_new(statement_size);
if (detected_server == SERVER_TYPE_MYSQL) {
if (set_names_str)
g_string_printf(statement,"%s;\n",set_names_str);
g_string_append(statement, "/*!40014 SET FOREIGN_KEY_CHECKS=0*/;\n\n");
if (!skip_tz) {
g_string_append(statement, "/*!40103 SET TIME_ZONE='+00:00' */;\n");
}
} else if (detected_server == SERVER_TYPE_TIDB) {
if (!skip_tz) {
g_string_printf(statement, "/*!40103 SET TIME_ZONE='+00:00' */;\n");
}
} else {
g_string_printf(statement, "SET FOREIGN_KEY_CHECKS=0;\n");
}
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write schema data for %s.%s", database, table);
errors++;
return;
}
query = g_strdup_printf("SHOW CREATE TABLE `%s`.`%s`", database, table);
if (mysql_query(conn, query) || !(result = mysql_use_result(conn))) {
if (success_on_1146 && mysql_errno(conn) == 1146) {
g_warning("Error dumping schemas (%s.%s): %s", database, table,
mysql_error(conn));
} else {
g_critical("Error dumping schemas (%s.%s): %s", database, table,
mysql_error(conn));
errors++;
}
g_free(query);
return;
}
g_string_set_size(statement, 0);
/* There should never be more than one row */
row = mysql_fetch_row(result);
g_string_append(statement, row[1]);
g_string_append(statement, ";\n");
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write schema for %s.%s", database, table);
errors++;
}
g_free(query);
m_close(outfile);
if (stream) g_async_queue_push(stream_queue, g_strdup(filename));
g_string_free(statement, TRUE);
if (result)
mysql_free_result(result);
if (checksum_filename)
write_checksum_into_file(conn, database, table, checksum_filename, checksum_table_structure);
if (checksum_index_filename)
write_checksum_into_file(conn, database, table, checksum_index_filename, checksum_table_indexes);
return;
}
void write_triggers_definition_into_file(MYSQL *conn, char *database, char *table, char *filename, char *checksum_filename) {
void *outfile;
char *query = NULL;
MYSQL_RES *result = NULL;
MYSQL_RES *result2 = NULL;
MYSQL_ROW row;
MYSQL_ROW row2;
gchar **splited_st = NULL;
outfile = m_open(filename,"w");
if (!outfile) {
g_critical("Error: DB: %s Could not create output file %s (%d)", database,
filename, errno);
errors++;
return;
}
GString *statement = g_string_sized_new(statement_size);
// get triggers
query = g_strdup_printf("SHOW TRIGGERS FROM `%s` LIKE '%s'", database, table);
if (mysql_query(conn, query) || !(result = mysql_store_result(conn))) {
if (success_on_1146 && mysql_errno(conn) == 1146) {
g_warning("Error dumping triggers (%s.%s): %s", database, table,
mysql_error(conn));
} else {
g_critical("Error dumping triggers (%s.%s): %s", database, table,
mysql_error(conn));
errors++;
}
g_free(query);
return;
}
while ((row = mysql_fetch_row(result))) {
set_charset(statement, row[8], row[9]);
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write triggers data for %s.%s", database, table);
errors++;
return;
}
g_string_set_size(statement, 0);
query = g_strdup_printf("SHOW CREATE TRIGGER `%s`.`%s`", database, row[0]);
mysql_query(conn, query);
result2 = mysql_store_result(conn);
row2 = mysql_fetch_row(result2);
g_string_append_printf(statement, "%s", row2[2]);
splited_st = g_strsplit(statement->str, ";\n", 0);
g_string_printf(statement, "%s", g_strjoinv("; \n", splited_st));
g_string_append(statement, ";\n");
restore_charset(statement);
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write triggers data for %s.%s", database, table);
errors++;
return;
}
g_string_set_size(statement, 0);
}
g_free(query);
m_close(outfile);
if (stream) g_async_queue_push(stream_queue, g_strdup(filename));
g_string_free(statement, TRUE);
g_strfreev(splited_st);
if (result)
mysql_free_result(result);
if (result2)
mysql_free_result(result2);
if (checksum_filename)
write_checksum_into_file(conn, database, table, checksum_filename, checksum_trigger_structure);
return;
}
void write_view_definition_into_file(MYSQL *conn, char *database, char *table, char *filename, char *filename2, char *checksum_filename) {
void *outfile, *outfile2;
char *query = NULL;
MYSQL_RES *result = NULL;
MYSQL_ROW row;
GString *statement = g_string_sized_new(statement_size);
mysql_select_db(conn, database);
outfile = m_open(filename,"w");
outfile2 = m_open(filename2,"w");
if (!outfile || !outfile2) {
g_critical("Error: DB: %s Could not create output file (%d)", database,
errno);
errors++;
return;
}
if (detected_server == SERVER_TYPE_MYSQL && set_names_str) {
g_string_printf(statement,"%s;\n",set_names_str);
}
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write schema data for %s.%s", database, table);
errors++;
return;
}
g_string_append_printf(statement, "DROP TABLE IF EXISTS `%s`;\n", table);
g_string_append_printf(statement, "DROP VIEW IF EXISTS `%s`;\n", table);
if (!write_data((FILE *)outfile2, statement)) {
g_critical("Could not write schema data for %s.%s", database, table);
errors++;
return;
}
// we create tables as workaround
// for view dependencies
query = g_strdup_printf("SHOW FIELDS FROM `%s`.`%s`", database, table);
if (mysql_query(conn, query) || !(result = mysql_use_result(conn))) {
if (success_on_1146 && mysql_errno(conn) == 1146) {
g_warning("Error dumping schemas (%s.%s): %s", database, table,
mysql_error(conn));
} else {
g_critical("Error dumping schemas (%s.%s): %s", database, table,
mysql_error(conn));
errors++;
}
g_free(query);
return;
}
g_free(query);
g_string_set_size(statement, 0);
g_string_append_printf(statement, "CREATE TABLE IF NOT EXISTS `%s`(\n", table);
row = mysql_fetch_row(result);
g_string_append_printf(statement, "`%s` int", row[0]);
while ((row = mysql_fetch_row(result))) {
g_string_append(statement, ",\n");
g_string_append_printf(statement, "`%s` int", row[0]);
}
g_string_append(statement, "\n);\n");
if (result)
mysql_free_result(result);
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write view schema for %s.%s", database, table);
errors++;
}
// real view
query = g_strdup_printf("SHOW CREATE VIEW `%s`.`%s`", database, table);
if (mysql_query(conn, query) || !(result = mysql_use_result(conn))) {
if (success_on_1146 && mysql_errno(conn) == 1146) {
g_warning("Error dumping schemas (%s.%s): %s", database, table,
mysql_error(conn));
} else {
g_critical("Error dumping schemas (%s.%s): %s", database, table,
mysql_error(conn));
errors++;
}
g_free(query);
return;
}
g_string_set_size(statement, 0);
/* There should never be more than one row */
row = mysql_fetch_row(result);
set_charset(statement, row[2], row[3]);
if ( skip_definer && g_str_has_prefix(row[1],"CREATE")){
remove_definer_from_gchar(row[1]);
}
g_string_append(statement, row[1]);
g_string_append(statement, ";\n");
restore_charset(statement);
if (!write_data((FILE *)outfile2, statement)) {
g_critical("Could not write schema for %s.%s", database, table);
errors++;
}
g_free(query);
m_close(outfile);
if (stream) g_async_queue_push(stream_queue, g_strdup(filename));
m_close(outfile2);
if (stream) g_async_queue_push(stream_queue, g_strdup(filename2));
g_string_free(statement, TRUE);
if (result)
mysql_free_result(result);
if (checksum_filename)
// build_meta_filename(database,table,"schema-view-checksum"),
write_checksum_into_file(conn, database, table, checksum_filename, checksum_view_structure);
return;
}
// Routines, Functions and Events
// TODO: We need to split it in 3 functions
void write_routines_definition_into_file(MYSQL *conn, struct database *database, char *filename, char *checksum_filename) {
void *outfile;
char *query = NULL;
MYSQL_RES *result = NULL;
MYSQL_RES *result2 = NULL;
MYSQL_ROW row;
MYSQL_ROW row2;
gchar **splited_st = NULL;
outfile = m_open(filename,"w");
if (!outfile) {
g_critical("Error: DB: %s Could not create output file %s (%d)", database->name,
filename, errno);
errors++;
return;
}
GString *statement = g_string_sized_new(statement_size);
if (dump_routines) {
// get functions
query = g_strdup_printf("SHOW FUNCTION STATUS WHERE CAST(Db AS BINARY) = '%s'", database->escaped);
if (mysql_query(conn, query) || !(result = mysql_store_result(conn))) {
if (success_on_1146 && mysql_errno(conn) == 1146) {
g_warning("Error dumping functions from %s: %s", database->name,
mysql_error(conn));
} else {
g_critical("Error dumping functions from %s: %s", database->name,
mysql_error(conn));
errors++;
}
g_free(query);
return;
}
while ((row = mysql_fetch_row(result))) {
set_charset(statement, row[8], row[9]);
g_string_append_printf(statement, "DROP FUNCTION IF EXISTS `%s`;\n",
row[1]);
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write stored procedure data for %s.%s", database->name,
row[1]);
errors++;
return;
}
g_string_set_size(statement, 0);
query =
g_strdup_printf("SHOW CREATE FUNCTION `%s`.`%s`", database->name, row[1]);
mysql_query(conn, query);
result2 = mysql_store_result(conn);
row2 = mysql_fetch_row(result2);
g_string_printf(statement, "%s", row2[2]);
if ( skip_definer && g_str_has_prefix(statement->str,"CREATE")){
remove_definer(statement);
}
splited_st = g_strsplit(statement->str, ";\n", 0);
g_string_printf(statement, "%s", g_strjoinv("; \n", splited_st));
g_string_append(statement, ";\n");
restore_charset(statement);
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write function data for %s.%s", database->name, row[1]);
errors++;
return;
}
g_string_set_size(statement, 0);
}
// get sp
query = g_strdup_printf("SHOW PROCEDURE STATUS WHERE CAST(Db AS BINARY) = '%s'", database->escaped);
if (mysql_query(conn, query) || !(result = mysql_store_result(conn))) {
if (success_on_1146 && mysql_errno(conn) == 1146) {
g_warning("Error dumping stored procedures from %s: %s", database->name,
mysql_error(conn));
} else {
g_critical("Error dumping stored procedures from %s: %s", database->name,
mysql_error(conn));
errors++;
}
g_free(query);
return;
}
while ((row = mysql_fetch_row(result))) {
set_charset(statement, row[8], row[9]);
g_string_append_printf(statement, "DROP PROCEDURE IF EXISTS `%s`;\n",
row[1]);
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write stored procedure data for %s.%s", database->name,
row[1]);
errors++;
return;
}
g_string_set_size(statement, 0);
query =
g_strdup_printf("SHOW CREATE PROCEDURE `%s`.`%s`", database->name, row[1]);
mysql_query(conn, query);
result2 = mysql_store_result(conn);
row2 = mysql_fetch_row(result2);
g_string_printf(statement, "%s", row2[2]);
if ( skip_definer && g_str_has_prefix(statement->str,"CREATE")){
remove_definer(statement);
}
splited_st = g_strsplit(statement->str, ";\n", 0);
g_string_printf(statement, "%s", g_strjoinv("; \n", splited_st));
g_string_append(statement, ";\n");
restore_charset(statement);
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write stored procedure data for %s.%s", database->name,
row[1]);
errors++;
return;
}
g_string_set_size(statement, 0);
}
if (checksum_filename)
write_checksum_into_file(conn, database->name, NULL, checksum_filename, checksum_process_structure);
}
// get events
if (dump_events) {
query = g_strdup_printf("SHOW EVENTS FROM `%s`", database->name);
if (mysql_query(conn, query) || !(result = mysql_store_result(conn))) {
if (success_on_1146 && mysql_errno(conn) == 1146) {
g_warning("Error dumping events from %s: %s", database->name,
mysql_error(conn));
} else {
g_critical("Error dumping events from %s: %s", database->name,
mysql_error(conn));
errors++;
}
g_free(query);
return;
}
while ((row = mysql_fetch_row(result))) {
set_charset(statement, row[12], row[13]);
g_string_append_printf(statement, "DROP EVENT IF EXISTS `%s`;\n", row[1]);
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write stored procedure data for %s.%s", database->name,
row[1]);
errors++;
return;
}
query = g_strdup_printf("SHOW CREATE EVENT `%s`.`%s`", database->name, row[1]);
mysql_query(conn, query);
result2 = mysql_store_result(conn);
// DROP EVENT IF EXISTS event_name
row2 = mysql_fetch_row(result2);
g_string_printf(statement, "%s", row2[3]);
if ( skip_definer && g_str_has_prefix(statement->str,"CREATE")){
remove_definer(statement);
}
splited_st = g_strsplit(statement->str, ";\n", 0);
g_string_printf(statement, "%s", g_strjoinv("; \n", splited_st));
g_string_append(statement, ";\n");
restore_charset(statement);
if (!write_data((FILE *)outfile, statement)) {
g_critical("Could not write event data for %s.%s", database->name, row[1]);
errors++;
return;
}
g_string_set_size(statement, 0);
}
}
g_free(query);
m_close(outfile);
if (stream) g_async_queue_push(stream_queue, g_strdup(filename));
g_string_free(statement, TRUE);
g_strfreev(splited_st);
if (result)
mysql_free_result(result);
if (result2)
mysql_free_result(result2);
return;
}
void free_schema_job(struct schema_job *sj){
if (sj->table){
g_free(sj->table);
sj->table=NULL;
}
if (sj->filename){
g_free(sj->filename);
sj->filename=NULL;
}
g_free(sj);
}
void free_view_job(struct view_job *vj){
if (vj->table)
g_free(vj->table);
if (vj->filename)
g_free(vj->filename);
if (vj->filename2)
g_free(vj->filename2);
// g_free(vj);
}
void free_schema_post_job(struct schema_post_job *sp){
if (sp->filename)
g_free(sp->filename);
// g_free(sp);
}
void free_create_database_job(struct create_database_job * cdj){
if (cdj->filename)
g_free(cdj->filename);
if (cdj->database)
g_free(cdj->database);
g_free(cdj);
}
void free_create_tablespace_job(struct create_tablespace_job * ctj){
if (ctj->filename)
g_free(ctj->filename);
// g_free(cdj);
}
void free_table_checksum_job(struct table_checksum_job*tcj){
if (tcj->table)
g_free(tcj->table);
if (tcj->filename)
g_free(tcj->filename);
g_free(tcj);
}
void do_JOB_CREATE_DATABASE(struct thread_data *td, struct job *job){
struct create_database_job * cdj = (struct create_database_job *)job->job_data;
g_message("Thread %d: dumping schema create for `%s`", td->thread_id,
cdj->database);
write_schema_definition_into_file(td->thrconn, cdj->database, cdj->filename, cdj->checksum_filename);
free_create_database_job(cdj);
g_free(job);
}
void do_JOB_CREATE_TABLESPACE(struct thread_data *td, struct job *job){
struct create_tablespace_job * ctj = (struct create_tablespace_job *)job->job_data;
g_message("Thread %d: dumping create tablespace if any", td->thread_id);
write_tablespace_definition_into_file(td->thrconn, ctj->filename);
free_create_tablespace_job(ctj);
g_free(job);
}
void do_JOB_SCHEMA_POST(struct thread_data *td, struct job *job){
struct schema_post_job * sp = (struct schema_post_job *)job->job_data;
g_message("Thread %d: dumping SP and VIEWs for `%s`", td->thread_id,
sp->database->name);
write_routines_definition_into_file(td->thrconn, sp->database, sp->filename, sp->checksum_filename);
free_schema_post_job(sp);
g_free(job);
}
void do_JOB_VIEW(struct thread_data *td, struct job *job){
struct view_job * vj = (struct view_job *)job->job_data;
g_message("Thread %d: dumping view for `%s`.`%s`", td->thread_id,
vj->database, vj->table);
write_view_definition_into_file(td->thrconn, vj->database, vj->table, vj->filename,
vj->filename2, vj->checksum_filename);
// free_view_job(vj);
g_free(job);
}
void do_JOB_SCHEMA(struct thread_data *td, struct job *job){
struct schema_job *sj = (struct schema_job *)job->job_data;
g_message("Thread %d: dumping schema for `%s`.`%s`", td->thread_id,
sj->database, sj->table);
write_table_definition_into_file(td->thrconn, sj->database, sj->table, sj->filename, sj->checksum_filename, sj->checksum_index_filename);
// free_schema_job(sj);
g_free(job);
// if (g_atomic_int_dec_and_test(&table_counter)) {
// g_message("Unlocing ready_table_dump_mutex");
// g_mutex_unlock(ready_table_dump_mutex);
// }
}
void do_JOB_TRIGGERS(struct thread_data *td, struct job *job){
struct schema_job * sj = (struct schema_job *)job->job_data;
g_message("Thread %d: dumping triggers for `%s`.`%s`", td->thread_id,
sj->database, sj->table);
write_triggers_definition_into_file(td->thrconn, sj->database, sj->table, sj->filename, sj->checksum_filename);
free_schema_job(sj);
g_free(job);
}
void do_JOB_CHECKSUM(struct thread_data *td, struct job *job){
struct table_checksum_job *tcj = (struct table_checksum_job *)job->job_data;
g_message("Thread %d: dumping checksum for `%s`.`%s`", td->thread_id,
tcj->database, tcj->table);
if (use_savepoints && mysql_query(td->thrconn, "SAVEPOINT mydumper")) {
g_critical("Savepoint failed: %s", mysql_error(td->thrconn));
}
write_checksum_into_file(td->thrconn, tcj->database, tcj->table, tcj->filename, checksum_table);
if (use_savepoints &&
mysql_query(td->thrconn, "ROLLBACK TO SAVEPOINT mydumper")) {
g_critical("Rollback to savepoint failed: %s", mysql_error(td->thrconn));
}
free_table_checksum_job(tcj);
g_free(job);
}
void create_job_to_dump_metadata(struct configuration *conf, FILE *mdfile){
struct job *j = g_new0(struct job, 1);
j->job_data = (void *)mdfile;
// j->conf = conf;
j->type = JOB_WRITE_MASTER_STATUS;
g_async_queue_push(conf->schema_queue, j);
}
void create_job_to_dump_tablespaces(struct configuration *conf){
struct job *j = g_new0(struct job, 1);
struct create_tablespace_job *ctj = g_new0(struct create_tablespace_job, 1);
j->job_data = (void *)ctj;
// j->conf = conf;
j->type = JOB_CREATE_TABLESPACE;
ctj->filename = build_tablespace_filename();
g_async_queue_push(conf->schema_queue, j);
}
void create_job_to_dump_schema(char *database, struct configuration *conf) {
struct job *j = g_new0(struct job, 1);
struct create_database_job *cdj = g_new0(struct create_database_job, 1);
j->job_data = (void *)cdj;
gchar *d=get_ref_table(database);
cdj->database = g_strdup(database);
// j->conf = conf;
j->type = JOB_CREATE_DATABASE;
cdj->filename = build_schema_filename(d, "schema-create");
if (schema_checksums)
cdj->checksum_filename = build_meta_filename(database,NULL,"schema-create-checksum");
g_async_queue_push(conf->schema_queue, j);
return;
}
void create_job_to_dump_triggers(MYSQL *conn, struct db_table *dbt, struct configuration *conf) {
if (dump_triggers) {
char *query = NULL;
MYSQL_RES *result = NULL;
query =
g_strdup_printf("SHOW TRIGGERS FROM `%s` LIKE '%s'", dbt->database->name, dbt->escaped_table);
if (mysql_query(conn, query) || !(result = mysql_store_result(conn))) {
g_critical("Error Checking triggers for %s.%s. Err: %s St: %s", dbt->database->name, dbt->table,
mysql_error(conn),query);
errors++;
} else {
if (mysql_num_rows(result)) {
struct job *t = g_new0(struct job, 1);
struct schema_job *st = g_new0(struct schema_job, 1);
t->job_data = (void *)st;
st->database = dbt->database->name;
st->table = g_strdup(dbt->table);
// t->conf = conf;
t->type = JOB_TRIGGERS;
st->filename = build_schema_table_filename(dbt->database->filename, dbt->table_filename, "schema-triggers");
if ( routine_checksums )
st->checksum_filename=build_meta_filename(dbt->database->filename,dbt->table_filename,"schema-triggers-checksum");
g_async_queue_push(conf->post_data_queue, t);
}
}
g_free(query);
if (result) {
mysql_free_result(result);
}
}
}
void create_job_to_dump_table_schema(struct db_table *dbt, struct configuration *conf) {
struct job *j = g_new0(struct job, 1);
struct schema_job *sj = g_new0(struct schema_job, 1);
j->job_data = (void *)sj;
sj->database = dbt->database->name;
sj->table = g_strdup(dbt->table);
// j->conf = conf;
j->type = JOB_SCHEMA;
sj->filename = build_schema_table_filename(dbt->database->filename, dbt->table_filename, "schema");
if ( schema_checksums ){
sj->checksum_filename=build_meta_filename(dbt->database->filename,dbt->table_filename,"schema-checksum");
sj->checksum_index_filename = build_meta_filename(dbt->database->filename,dbt->table_filename,"schema-indexes-checksum");
}
g_async_queue_push(conf->schema_queue, j);
}
void create_job_to_dump_view(struct db_table *dbt, struct configuration *conf) {
struct job *j = g_new0(struct job, 1);
struct view_job *vj = g_new0(struct view_job, 1);
j->job_data = (void *)vj;
vj->database = dbt->database->name;
vj->table = g_strdup(dbt->table);
// j->conf = conf;
j->type = JOB_VIEW;
vj->filename = build_schema_table_filename(dbt->database->filename, dbt->table_filename, "schema");
vj->filename2 = build_schema_table_filename(dbt->database->filename, dbt->table_filename, "schema-view");
if ( schema_checksums )
vj->checksum_filename = build_meta_filename(dbt->database->filename, dbt->table_filename, "schema-view-checksum");
g_async_queue_push(conf->post_data_queue, j);
return;
}
void create_job_to_dump_post(struct database *database, struct configuration *conf) {
struct job *j = g_new0(struct job, 1);
struct schema_post_job *sp = g_new0(struct schema_post_job, 1);
j->job_data = (void *)sp;
sp->database = database;
// j->conf = conf;
j->type = JOB_SCHEMA_POST;
sp->filename = build_schema_filename(sp->database->filename,"schema-post");
if ( routine_checksums )
sp->checksum_filename = build_meta_filename(sp->database->filename, NULL, "schema-post-checksum");
g_async_queue_push(conf->post_data_queue, j);
return;
}
void create_job_to_dump_checksum(struct db_table * dbt, struct configuration *conf) {
struct job *j = g_new0(struct job, 1);
struct table_checksum_job *tcj = g_new0(struct table_checksum_job, 1);
j->job_data = (void *)tcj;
tcj->database = dbt->database->name;
tcj->table = g_strdup(dbt->table);
// j->conf = conf;
j->type = JOB_CHECKSUM;
tcj->filename = build_meta_filename(dbt->database->filename, dbt->table_filename,"checksum");
g_async_queue_push(conf->post_data_queue, j);
return;
}
void execute_file_per_thread( gchar *sql_fn, gchar *sql_fn3){
int childpid=fork();
if(!childpid){
FILE *sql_file2 = m_open(sql_fn,"r");
FILE *sql_file3 = m_open(sql_fn3,"w");
dup2(fileno(sql_file2), STDIN_FILENO);
dup2(fileno(sql_file3), STDOUT_FILENO);
execv(exec_per_thread_cmd[0],exec_per_thread_cmd);
m_close(sql_file2);
m_close(sql_file3);
}
}
void initialize_fn(gchar ** sql_filename, struct db_table * dbt, FILE ** sql_file, guint fn, guint sub_part, const gchar *extension, gchar * f()){
gchar *stdout_fn=NULL;
/* if (*sql_filename != NULL){
remove(*sql_filename);
g_free(*sql_filename);
}
*/
if (use_fifo){
*sql_filename = build_fifo_filename(dbt->database->filename, dbt->table_filename, fn, sub_part, extension);
mkfifo(*sql_filename,0666);
stdout_fn = build_stdout_filename(dbt->database->filename, dbt->table_filename, fn, sub_part, extension, exec_per_thread_extension);
execute_file_per_thread(*sql_filename,stdout_fn);
}else{
*sql_filename = f(dbt->database->filename, dbt->table_filename, fn, sub_part);
}
*sql_file = m_open(*sql_filename,"w");
}
void initialize_sql_fn(struct table_job * tj){