restore_main.cpp 19.8 KB
Newer Older
1 2 3 4
/* Copyright (C) 2003 MySQL AB

   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
5
   the Free Software Foundation; version 2 of the License.
6 7 8 9 10 11 12 13 14 15

   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, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

16 17
#include <ndb_global.h>
#include <ndb_opts.h>
18 19
#include <Vector.hpp>
#include <ndb_limits.h>
20
#include <NdbTCP.h>
21
#include <NdbMem.h>
22
#include <NdbOut.hpp>
23
#include <OutputStream.hpp>
24
#include <NDBT_ReturnCodes.h>
25

26 27
#include "consumer_restore.hpp"
#include "consumer_printer.hpp"
28 29 30 31 32 33

extern FilteredNdbOut err;
extern FilteredNdbOut info;
extern FilteredNdbOut debug;

static int ga_nodeId = 0;
34
static int ga_nParallelism = 128;
35 36
static int ga_backupId = 0;
static bool ga_dont_ignore_systab_0 = false;
37
static Vector<class BackupConsumer *> g_consumers;
38
static BackupPrinter* g_printer = NULL;
39

40 41 42 43 44 45 46 47 48 49
static const char* default_backupPath = "." DIR_SEPARATOR;
static const char* ga_backupPath = default_backupPath;

const char *opt_ndb_database= NULL;
const char *opt_ndb_table= NULL;
unsigned int opt_verbose;
unsigned int opt_hex_format;
Vector<BaseString> g_databases;
Vector<BaseString> g_tables;
NdbRecordPrintFormat g_ndbrecord_print_format;
50

51
NDB_STD_OPTS_VARS;
52 53 54 55 56 57

/**
 * print and restore flags
 */
static bool ga_restore = false;
static bool ga_print = false;
58 59 60 61 62 63
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;
tomas@poseidon.ndb.mysql.com's avatar
ndb:  
tomas@poseidon.ndb.mysql.com committed
64 65
BaseString g_options("ndb_restore");

66 67
const char *load_default_groups[]= { "mysql_cluster","ndb_restore",0 };

68 69 70 71 72 73 74 75 76 77 78 79 80 81
enum ndb_restore_options {
  OPT_PRINT= NDB_STD_OPTIONS_LAST,
  OPT_PRINT_DATA,
  OPT_PRINT_LOG,
  OPT_PRINT_META,
  OPT_BACKUP_PATH,
  OPT_HEX_FORMAT,
  OPT_FIELDS_ENCLOSED_BY,
  OPT_FIELDS_TERMINATED_BY,
  OPT_FIELDS_OPTIONALLY_ENCLOSED_BY,
  OPT_LINES_TERMINATED_BY,
  OPT_APPEND,
  OPT_VERBOSE
};
82 83 84 85
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;
86 87 88 89

static const char *tab_path= NULL;
static int opt_append;

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
static struct my_option my_long_options[] =
{
  NDB_STD_OPTS("ndb_restore"),
  { "connect", 'c', "same as --connect-string",
    (gptr*) &opt_connect_str, (gptr*) &opt_connect_str, 0,
    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
  { "nodeid", 'n', "Backup files from node with id",
    (gptr*) &ga_nodeId, (gptr*) &ga_nodeId, 0,
    GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
  { "backupid", 'b', "Backup id",
    (gptr*) &ga_backupId, (gptr*) &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", 
    (gptr*) &_restore_data, (gptr*) &_restore_data,  0,
    GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
  { "restore_meta", 'm',
    "Restore meta data into NDB Cluster using NDBAPI",
    (gptr*) &_restore_meta, (gptr*) &_restore_meta,  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)", 
    (gptr*) &ga_nParallelism, (gptr*) &ga_nParallelism, 0,
114
    GET_INT, REQUIRED_ARG, 128, 1, 1024, 0, 1, 0 },
115
  { "print", OPT_PRINT, "Print data and log to stdout",
116 117
    (gptr*) &_print, (gptr*) &_print, 0,
    GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
118
  { "print_data", OPT_PRINT_DATA, "Print data to stdout", 
119 120
    (gptr*) &_print_data, (gptr*) &_print_data, 0,
    GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
121
  { "print_meta", OPT_PRINT_META, "Print meta data to stdout",
122 123
    (gptr*) &_print_meta, (gptr*) &_print_meta,  0,
    GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
124
  { "print_log", OPT_PRINT_LOG, "Print log to stdout",
125 126
    (gptr*) &_print_log, (gptr*) &_print_log,  0,
    GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
127 128 129
  { "backup_path", OPT_BACKUP_PATH, "Path to backup files",
    (gptr*) &ga_backupPath, (gptr*) &ga_backupPath, 0,
    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
130 131 132 133
  { "dont_ignore_systab_0", 'f',
    "Experimental. Do not ignore system table during restore.", 
    (gptr*) &ga_dont_ignore_systab_0, (gptr*) &ga_dont_ignore_systab_0, 0,
    GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
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
  { "fields-enclosed-by", OPT_FIELDS_ENCLOSED_BY,
    "Fields are enclosed by ...",
    (gptr*) &opt_fields_enclosed_by, (gptr*) &opt_fields_enclosed_by, 0,
    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
  { "fields-terminated-by", OPT_FIELDS_TERMINATED_BY,
    "Fields are terminated by ...",
    (gptr*) &opt_fields_terminated_by,
    (gptr*) &opt_fields_terminated_by, 0,
    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
  { "fields-optionally-enclosed-by", OPT_FIELDS_OPTIONALLY_ENCLOSED_BY,
    "Fields are optionally enclosed by ...",
    (gptr*) &opt_fields_optionally_enclosed_by,
    (gptr*) &opt_fields_optionally_enclosed_by, 0,
    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
  { "hex", OPT_HEX_FORMAT, "print binary types in hex format", 
    (gptr*) &opt_hex_format, (gptr*) &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)",
   (gptr*) &tab_path, (gptr*) &tab_path, 0,
    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  { "append", OPT_APPEND, "for --tab append data to file", 
    (gptr*) &opt_append, (gptr*) &opt_append, 0,
    GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0 },
  { "lines-terminated-by", OPT_LINES_TERMINATED_BY, "",
    (gptr*) &opt_lines_terminated_by, (gptr*) &opt_lines_terminated_by, 0,
    GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0 },
  { "verbose", OPT_VERBOSE,
    "verbosity", 
    (gptr*) &opt_verbose, (gptr*) &opt_verbose, 0,
    GET_INT, REQUIRED_ARG, 1, 0, 255, 0, 0, 0 },
165 166
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
};
167

168 169 170 171 172 173 174
static void short_usage_sub(void)
{
  printf("Usage: %s [OPTIONS] [<path to backup files>]\n", my_progname);
}
static void usage()
{
  short_usage_sub();
175
  ndb_std_print_version();
176 177
  print_defaults(MYSQL_CONFIG_NAME,load_default_groups);
  puts("");
178 179 180 181 182 183 184
  my_print_help(my_long_options);
  my_print_variables(my_long_options);
}
static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
	       char *argument)
{
185 186 187 188
#ifndef DBUG_OFF
  opt_debug= "d:t:O,/tmp/ndb_restore.trace";
#endif
  ndb_std_get_one_option(optid, opt, argument);
189
  switch (optid) {
190 191 192
  case OPT_VERBOSE:
    info.setThreshold(255-opt_verbose);
    break;
193 194 195
  case 'n':
    if (ga_nodeId == 0)
    {
196
      err << "Error in --nodeid,-n setting, see --help";
197
      exit(NDBT_ProgramExit(NDBT_WRONGARGS));
198
    }
199 200
    info.setLevel(254);
    info << "Nodeid = " << ga_nodeId << endl;
201
    break;
202 203 204
  case 'b':
    if (ga_backupId == 0)
    {
205
      err << "Error in --backupid,-b setting, see --help";
206
      exit(NDBT_ProgramExit(NDBT_WRONGARGS));
207
    }
208 209
    info.setLevel(254);
    info << "Backup Id = " << ga_backupId << endl;
210
    break;
211 212 213 214 215 216
  }
  return 0;
}
bool
readArguments(int *pargc, char*** pargv) 
{
217 218 219 220
  Uint32 i;
  debug << "Load defaults" << endl;
  const char *load_default_groups[]= { "mysql_cluster","ndb_restore",0 };

221
  load_defaults("my",load_default_groups,pargc,pargv);
222
  debug << "handle_options" << endl;
223 224
  if (handle_options(pargc, pargv, my_long_options, get_one_option))
  {
225
    exit(NDBT_ProgramExit(NDBT_WRONGARGS));
226 227
  }

228 229
  g_printer = new BackupPrinter();
  if (g_printer == NULL)
230 231 232 233 234
    return false;

  BackupRestore* restore = new BackupRestore(ga_nParallelism);
  if (restore == NULL) 
  {
235 236
    delete g_printer;
    g_printer = NULL;
237 238 239 240 241 242 243
    return false;
  }

  if (_print) 
  {
    ga_print = true;
    ga_restore = true;
244
    g_printer->m_print = true;
245 246 247 248
  } 
  if (_print_meta) 
  {
    ga_print = true;
249
    g_printer->m_print_meta = true;
250 251 252 253
  }
  if (_print_data) 
  {
    ga_print = true;
254
    g_printer->m_print_data = true;
255 256 257 258
  }
  if (_print_log) 
  {
    ga_print = true;
259
    g_printer->m_print_log = true;
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
  }

  if (_restore_data)
  {
    ga_restore = true;
    restore->m_restore = true; 
  }

  if (_restore_meta)
  {
    //    ga_restore = true;
    restore->m_restore_meta = true;
  }

  {
275
    BackupConsumer * c = g_printer;
276 277 278
    g_consumers.push_back(c);
  }
  {
279
    BackupConsumer * c = restore;
280 281
    g_consumers.push_back(c);
  }
282
  for (;;)
283
  {
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    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;
300
  }
301 302 303
  info.setLevel(254);
  info << "backup path = " << ga_backupPath << endl;
  if (g_databases.size() > 0)
304
  {
305 306 307 308 309 310 311 312 313
    info << "Restoring only from database " << g_databases[0].c_str() << endl;
    if (g_tables.size() > 0)
      info << "Restoring only tables:";
    for (unsigned i= 0; i < g_tables.size(); i++)
    {
      info << " " << g_tables[i].c_str();
    }
    if (g_tables.size() > 0)
      info << endl;
314
  }
315 316 317 318 319
  /*
    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=
320
    opt_fields_enclosed_by ? opt_fields_enclosed_by : "";
321
  g_ndbrecord_print_format.fields_terminated_by=
322
    opt_fields_terminated_by ? opt_fields_terminated_by : "\t";
323
  g_ndbrecord_print_format.fields_optionally_enclosed_by=
324
    opt_fields_optionally_enclosed_by ? opt_fields_optionally_enclosed_by : "";
325
  g_ndbrecord_print_format.lines_terminated_by=
326
    opt_lines_terminated_by ? opt_lines_terminated_by : "\n";
327 328 329 330 331 332
  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;
333 334 335 336 337 338
  return true;
}

void
clearConsumers()
{
339
  for(Uint32 i= 0; i<g_consumers.size(); i++)
340 341 342 343
    delete g_consumers[i];
  g_consumers.clear();
}

344 345 346 347 348 349 350 351
static inline bool
checkSysTable(const TableS* table)
{
  return ga_dont_ignore_systab_0 || ! table->getSysTable();
}

static inline bool
checkSysTable(const RestoreMetaData& metaData, uint i)
352
{
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
  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
checkDbAndTableName(const TableS* table)
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
  if (g_tables.size() == 0 &&
      g_databases.size() == 0)
    return true;
  if (g_databases.size() == 0)
    g_databases.push_back("TEST_DB");

  // Filter on the main table name for indexes and blobs
  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();

  unsigned i;
  for (i= 0; i < g_databases.size(); i++)
  {
    if (strncmp(table_name, g_databases[i].c_str(),
                g_databases[i].length()) == 0 &&
        table_name[g_databases[i].length()] == '/')
    {
      // we have a match
      if (g_databases.size() > 1 || g_tables.size() == 0)
        return true;
      break;
    }
  }
  if (i == g_databases.size())
    return false; // no match found

  while (*table_name != '/') table_name++;
  table_name++;
  while (*table_name != '/') table_name++;
  table_name++;

  for (i= 0; i < g_tables.size(); i++)
  {
    if (strcmp(table_name, g_tables[i].c_str()) == 0)
    {
      // we have a match
      return true;
    }
  }
  return false;
419 420
}

421 422 423
static void
free_data_callback()
{
424
  for(Uint32 i= 0; i < g_consumers.size(); i++) 
425 426 427
    g_consumers[i]->tuple_free();
}

428
const char * g_connect_string = 0;
429 430 431 432 433 434 435 436
static void exitHandler(int code)
{
  NDBT_ProgramExit(code);
  if (opt_core)
    abort();
  else
    exit(code);
}
437

438
int
439
main(int argc, char** argv)
440
{
441 442
  NDB_INIT(argv[0]);

443
  debug << "Start readArguments" << endl;
444
  if (!readArguments(&argc, &argv))
445
  {
446
    exitHandler(NDBT_FAILED);
447 448
  }

tomas@poseidon.ndb.mysql.com's avatar
ndb:  
tomas@poseidon.ndb.mysql.com committed
449 450 451 452 453 454 455 456
  g_options.appfmt(" -b %d", ga_backupId);
  g_options.appfmt(" -n %d", ga_nodeId);
  if (_restore_meta)
    g_options.appfmt(" -m");
  if (_restore_data)
    g_options.appfmt(" -r");
  g_options.appfmt(" -p %d", ga_nParallelism);

457
  g_connect_string = opt_connect_str;
458

459 460 461
  /**
   * we must always load meta data, even if we will only print it to stdout
   */
462
  debug << "Start restoring meta data" << endl;
463 464 465
  RestoreMetaData metaData(ga_backupPath, ga_nodeId, ga_backupId);
  if (!metaData.readHeader())
  {
466
    err << "Failed to read " << metaData.getFilename() << endl << endl;
467
    exitHandler(NDBT_FAILED);
468
  }
469 470 471 472

  const BackupFormat::FileHeader & tmp = metaData.getFileHeader();
  const Uint32 version = tmp.NdbVersion;
  
473
  char buf[NDB_VERSION_STRING_BUF_SZ];
474 475 476
  info.setLevel(254);
  info << "Ndb version in backup files: " 
       <<  getVersionString(version, 0, buf, sizeof(buf)) << endl;
477
  
478
  /**
479
   * check wheater we can restore the backup (right version).
480
   */
481 482 483 484 485 486 487 488
  if (version > NDB_VERSION)
  {
    err << "Restore program older than backup version. Not supported. "
        << "Use new restore program" << endl;
    exitHandler(NDBT_FAILED);
  }

  debug << "Load content" << endl;
489
  int res  = metaData.loadContent();
490
  
491
  if (res == 0)
492
  {
493
    err << "Restore: Failed to load content" << endl;
494
    exitHandler(NDBT_FAILED);
495
  }
496
  debug << "Get no of Tables" << endl; 
497
  if (metaData.getNoOfTables() == 0) 
498
  {
499
    err << "The backup contains no tables" << endl;
500
    exitHandler(NDBT_FAILED);
501
  }
502
  debug << "Validate Footer" << endl;
503 504 505

  if (!metaData.validateFooter()) 
  {
506
    err << "Restore: Failed to validate footer." << endl;
507
    exitHandler(NDBT_FAILED);
508
  }
509
  debug << "Init Backup objects" << endl;
510 511
  Uint32 i;
  for(i= 0; i < g_consumers.size(); i++)
512 513 514 515
  {
    if (!g_consumers[i]->init())
    {
      clearConsumers();
516
      err << "Failed to initialize consumers" << endl;
517
      exitHandler(NDBT_FAILED);
518 519 520 521
    }

  }

522 523
  Vector<OutputStream *> table_output(metaData.getNoOfTables());
  debug << "Restoring tables" << endl;
524
  for(i = 0; i<metaData.getNoOfTables(); i++)
525
  {
526 527 528 529 530
    const TableS *table= metaData[i];
    table_output.push_back(NULL);
    if (!checkDbAndTableName(table))
      continue;
    if (checkSysTable(table))
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
      if (!tab_path || isBlobTable(table) || isIndex(table))
      {
        table_output[i]= ndbout.m_out;
      }
      else
      {
        FILE* res;
        char filename[FN_REFLEN], tmp_path[FN_REFLEN];
        const char *table_name;
        table_name= table->getTableName();
        while (*table_name != '/') table_name++;
        table_name++;
        while (*table_name != '/') table_name++;
        table_name++;
        convert_dirname(tmp_path, tab_path, NullS);
        res= my_fopen(fn_format(filename, table_name, tmp_path, ".txt", 4),
                      opt_append ?
                      O_WRONLY|O_APPEND|O_CREAT :
                      O_WRONLY|O_TRUNC|O_CREAT,
                      MYF(MY_WME));
        if (res == 0)
        {
          exitHandler(NDBT_FAILED);
        }
        FileOutputStream *f= new FileOutputStream(res);
        table_output[i]= f;
      }
559
      for(Uint32 j= 0; j < g_consumers.size(); j++)
560
	if (!g_consumers[j]->table(* table))
561
	{
562 563
	  err << "Restore: Failed to restore table: ";
          err << table->getTableName() << " ... Exiting " << endl;
564
	  exitHandler(NDBT_FAILED);
565
        }
566 567
    }
  }
568
  debug << "Close tables" << endl; 
569 570 571
  for(i= 0; i < g_consumers.size(); i++)
    if (!g_consumers[i]->endOfTables())
    {
572
      err << "Restore: Failed while closing tables" << endl;
573
      exitHandler(NDBT_FAILED);
574
    } 
575
  debug << "Iterate over data" << endl; 
576 577
  if (ga_restore || ga_print) 
  {
578
    if(_restore_data || _print_data)
579 580 581 582 583
    {
      RestoreDataIterator dataIter(metaData, &free_data_callback);
      
      // Read data file header
      if (!dataIter.readHeader())
584
      {
585
	err << "Failed to read header of data file. Exiting..." << endl;
586
	exitHandler(NDBT_FAILED);
587 588 589 590 591 592 593
      }
      
      
      while (dataIter.readFragmentHeader(res= 0))
      {
	const TupleS* tuple;
	while ((tuple = dataIter.getNextTuple(res= 1)) != 0)
594
	{
595 596 597 598 599 600
          const TableS* table = tuple->getTable();
          OutputStream *output = table_output[table->getLocalId()];
          if (!output)
            continue;
          OutputStream *tmp = ndbout.m_out;
          ndbout.m_out = output;
601 602
          for(Uint32 j= 0; j < g_consumers.size(); j++) 
            g_consumers[j]->tuple(* tuple);
603
          ndbout.m_out =  tmp;
604
	} // while (tuple != NULL);
605 606 607
	
	if (res < 0)
	{
608 609
	  err <<" Restore: An error occured while restoring data. Exiting...";
          err << endl;
610
	  exitHandler(NDBT_FAILED);
611
	}
612
	if (!dataIter.validateFragmentFooter()) {
613 614
	  err << "Restore: Error validating fragment footer. ";
          err << "Exiting..." << endl;
615
	  exitHandler(NDBT_FAILED);
616
	}
617 618 619 620
      } // while (dataIter.readFragmentHeader(res))
      
      if (res < 0)
      {
621
	err << "Restore: An error occured while restoring data. Exiting... "
622
	    << "res= " << res << endl;
623
	exitHandler(NDBT_FAILED);
624 625 626 627 628 629 630
      }
      
      
      dataIter.validateFooter(); //not implemented
      
      for (i= 0; i < g_consumers.size(); i++)
	g_consumers[i]->endOfTuples();
631
    }
632

633 634
    if(_restore_data || _print_log)
    {
635 636 637 638
      RestoreLogIterator logIter(metaData);
      if (!logIter.readHeader())
      {
	err << "Failed to read header of data file. Exiting..." << endl;
639
	exitHandler(NDBT_FAILED);
640 641 642 643 644
      }
      
      const LogEntry * logEntry = 0;
      while ((logEntry = logIter.getNextLogEntry(res= 0)) != 0)
      {
645 646 647 648
        const TableS* table = logEntry->m_table;
        OutputStream *output = table_output[table->getLocalId()];
        if (!output)
          continue;
649 650
        for(Uint32 j= 0; j < g_consumers.size(); j++)
          g_consumers[j]->logEntry(* logEntry);
651 652 653
      }
      if (res < 0)
      {
654 655
	err << "Restore: An restoring the data log. Exiting... res=" 
	    << res << endl;
656
	exitHandler(NDBT_FAILED);
657
      }
658 659 660
      logIter.validateFooter(); //not implemented
      for (i= 0; i < g_consumers.size(); i++)
	g_consumers[i]->endOfLogEntrys();
661 662 663 664
    }
    
    if(_restore_data)
    {
665 666
      for(i = 0; i<metaData.getNoOfTables(); i++)
      {
667 668 669 670 671 672 673 674 675 676 677
        const TableS* table = metaData[i];
        OutputStream *output = table_output[table->getLocalId()];
        if (!output)
          continue;
        for(Uint32 j= 0; j < g_consumers.size(); j++)
          if (!g_consumers[j]->finalize_table(*table))
          {
            err << "Restore: Failed to finalize restore table: %s. ";
            err << "Exiting... " << metaData[i]->getTableName() << endl;
            exitHandler(NDBT_FAILED);
          }
678
      }
679
    }
680
  }
681
  for(Uint32 j= 0; j < g_consumers.size(); j++) 
682
  {
683
    if (g_consumers[j]->has_temp_error())
684 685 686 687
    {
      clearConsumers();
      ndbout_c("\nRestore successful, but encountered temporary error, "
               "please look at configuration.");
688 689 690 691 692 693 694 695 696 697 698 699 700
    }               
  }
  
  clearConsumers();

  for(i = 0; i < metaData.getNoOfTables(); i++)
  {
    if (table_output[i] &&
        table_output[i] != ndbout.m_out)
    {
      my_fclose(((FileOutputStream *)table_output[i])->getFile(), MYF(MY_WME));
      delete table_output[i];
      table_output[i] = NULL;
701 702 703
    }
  }

704 705 706 707
  if (opt_verbose)
    return NDBT_ProgramExit(NDBT_OK);
  else
    return 0;
708 709
} // main

710
template class Vector<BackupConsumer*>;
711
template class Vector<OutputStream*>;