76
114
@param lex results of parsing the statement.
78
116
@note This function sends response to the client (ok, result set or error).
118
@returns 0 on success, error code otherwise.
82
122
execute_backup_command(THD *thd, LEX *lex)
84
ulonglong backup_prog_id= 0;
85
time_t start=0, stop=0;
87
126
DBUG_ENTER("execute_backup_command");
88
127
DBUG_ASSERT(thd && lex);
90
BACKUP_BREAKPOINT("backup_command");
128
DEBUG_SYNC(thd, "before_backup_command");
92
130
using namespace backup;
94
mysql_reset_errors(thd, 0);
95
thd->no_warnings_for_error= FALSE;
98
Check access for SUPER rights. If user does not have SUPER, fail with error.
100
if (check_global_access(thd, SUPER_ACL))
102
my_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, MYF(0), "SUPER");
103
DBUG_RETURN(ER_SPECIFIC_ACCESS_DENIED_ERROR);
107
Check for progress tables.
109
if (check_ob_progress_tables(thd))
111
my_error(ER_BACKUP_PROGRESS_TABLES, MYF(0));
112
DBUG_RETURN(ER_BACKUP_PROGRESS_TABLES);
115
Location *loc= Location::find(lex->backup_dir);
117
if (!loc || !loc->is_valid())
119
my_error(ER_BACKUP_INVALID_LOC,MYF(0),lex->backup_dir.str);
120
DBUG_RETURN(ER_BACKUP_INVALID_LOC);
124
Start_backup_or_restore() will check if another BACKUP/RESTORE command is
125
running now and inform us about that. If this is the case we report error.
127
if ((lex->sql_command == SQLCOM_RESTORE)
128
|| (lex->sql_command == SQLCOM_BACKUP))
129
if(start_backup_or_restore())
131
my_error(ER_BACKUP_RUNNING,MYF(0));
132
DBUG_RETURN(ER_BACKUP_RUNNING);
135
prepare_stream_memory();
139
Important: above start_backup_or_restore() and prepare_stream_memory()
140
*must* be matched by finish_backup_or_restore() and free_stream_memory(),
141
respectively. Therefore be careful with early return with DBUG_RETURN() -
142
use "goto backup/restore_error" instead.
132
Backup_restore_ctx context(thd); // reports errors
134
if (!context.is_valid())
135
DBUG_RETURN(send_error(context, ER_BACKUP_CONTEXT_CREATE));
145
137
switch (lex->sql_command) {
147
case SQLCOM_SHOW_ARCHIVE:
141
// prepare for backup operation
143
Backup_info *info= context.prepare_for_backup(lex->backup_dir, thd->query,
144
lex->backup_compression);
147
if (!info || !info->is_valid())
148
DBUG_RETURN(send_error(context, ER_BACKUP_BACKUP_PREPARE));
150
DEBUG_SYNC(thd, "after_backup_start_backup");
152
// select objects to backup
154
if (lex->db_list.is_empty())
156
context.write_message(log_level::INFO, "Backing up all databases");
157
res= info->add_all_dbs(); // backup all databases
161
context.write_message(log_level::INFO, "Backing up selected databases");
162
res= info->add_dbs(lex->db_list); // backup databases specified by user
165
info->close(); // close catalogue after filling it with objects to backup
167
if (res || !info->is_valid())
168
DBUG_RETURN(send_error(context, ER_BACKUP_BACKUP_PREPARE));
170
if (info->db_count() == 0)
172
context.fatal_error(ER_BACKUP_NOTHING_TO_BACKUP);
173
DBUG_RETURN(send_error(context, ER_BACKUP_NOTHING_TO_BACKUP));
178
res= context.do_backup();
181
DBUG_RETURN(send_error(context, ER_BACKUP_BACKUP));
148
186
case SQLCOM_RESTORE:
150
backup::IStream *stream= open_for_read(*loc);
154
my_error(ER_BACKUP_READ_LOC,MYF(0),loc->describe());
159
if (lex->sql_command == SQLCOM_SHOW_ARCHIVE)
161
my_error(ER_NOT_ALLOWED_COMMAND,MYF(0));
167
backup_prog_id= report_ob_init(thd->id, BUP_STARTING, OP_RESTORE,
168
0, "", lex->backup_dir.str, thd->query);
169
report_ob_time(backup_prog_id, start, 0);
170
BACKUP_BREAKPOINT("bp_starting_state");
172
Restore_info info(thd,*stream);
174
info.backup_prog_id= backup_prog_id;
176
if (check_info(thd,info))
182
info.report_error(log_level::INFO,ER_BACKUP_RESTORE_START);
183
info.save_start_time(start);
185
report_ob_state(backup_prog_id, BUP_RUNNING);
186
BACKUP_BREAKPOINT("bp_running_state");
189
Freeze all DDL operations by turning on DDL blocker.
191
if (!DDL_blocker->block_DDL(thd))
194
info.save_end_time(stop);
199
info.restore_all_dbs();
201
if (check_info(thd,info))
204
info.save_end_time(stop);
208
info.clear_saved_errors();
210
res= mysql_restore(thd,info,*stream);
212
info.save_end_time(stop);
216
report_errors(thd,info,ER_BACKUP_RESTORE);
220
report_ob_num_objects(backup_prog_id, info.table_count);
221
report_ob_size(backup_prog_id, info.data_size);
222
report_ob_time(backup_prog_id, 0, stop);
223
report_ob_state(backup_prog_id, BUP_COMPLETE);
224
BACKUP_BREAKPOINT("bp_complete_state");
226
info.report_error(log_level::INFO,ER_BACKUP_RESTORE_DONE);
227
send_summary(thd,info);
234
res= res ? res : ERROR;
236
report_ob_error(backup_prog_id, res);
239
report_ob_time(backup_prog_id, 0, stop);
241
report_ob_state(backup_prog_id, BUP_ERRORS);
242
BACKUP_BREAKPOINT("bp_error_state");
247
Unfreeze all DDL operations by turning off DDL blocker.
249
DDL_blocker->unblock_DDL();
250
BACKUP_BREAKPOINT("DDL_unblocked");
260
/* if set to true, backup location will be removed (e.g., upon failure) */
261
bool remove_location= FALSE;
262
backup::OStream *stream= open_for_write(*loc);
266
my_error(ER_BACKUP_WRITE_LOC,MYF(0),loc->describe());
274
Freeze all DDL operations by turning on DDL blocker.
276
Note: The block_ddl() call must occur before the information_schema
277
is read so that any new tables (e.g. CREATE in progress) can
278
be counted. Waiting until after this step caused backup to
279
skip new or dropped tables.
281
if (!DDL_blocker->block_DDL(thd))
284
Backup_info info(thd);
286
backup_prog_id= report_ob_init(thd->id, BUP_STARTING, OP_BACKUP,
287
0, "", lex->backup_dir.str, thd->query);
288
report_ob_time(backup_prog_id, start, 0);
289
BACKUP_BREAKPOINT("bp_starting_state");
291
info.backup_prog_id= backup_prog_id;
293
if (check_info(thd,info))
296
info.report_error(log_level::INFO,ER_BACKUP_BACKUP_START);
297
info.save_start_time(start);
298
report_ob_state(backup_prog_id, BUP_RUNNING);
299
BACKUP_BREAKPOINT("bp_running_state");
303
if (lex->db_list.is_empty())
305
info.write_message(log_level::INFO,"Backing up all databases");
306
info.add_all_dbs(); // backup all databases
310
info.write_message(log_level::INFO,"Backing up selected databases");
311
res= info.add_dbs(lex->db_list); // backup databases specified by user
313
if ((info.db_count() == 0) && (res != ERROR))
316
info.report_error(log_level::ERROR, ER_BACKUP_NOTHING_TO_BACKUP, MYF(0));
318
info.save_end_time(stop);
319
report_errors(thd, info, ER_BACKUP_BACKUP);
323
report_ob_num_objects(backup_prog_id, info.table_count);
325
if (check_info(thd,info))
328
info.save_end_time(stop);
334
if (check_info(thd,info))
337
info.save_end_time(stop);
341
info.clear_saved_errors();
343
res= mysql_backup(thd,info,*stream);
345
info.save_end_time(stop);
349
report_errors(thd,info,ER_BACKUP_BACKUP);
353
report_ob_size(info.backup_prog_id, info.data_size);
354
report_ob_time(info.backup_prog_id, 0, stop);
355
report_ob_state(info.backup_prog_id, BUP_COMPLETE);
356
BACKUP_BREAKPOINT("bp_complete_state");
358
info.report_error(log_level::INFO,ER_BACKUP_BACKUP_DONE);
359
send_summary(thd,info);
367
res= res ? res : ERROR;
369
report_ob_error(backup_prog_id, res);
370
report_ob_state(backup_prog_id, BUP_ERRORS);
373
report_ob_time(backup_prog_id, 0, stop);
376
If the output stream was opened, a file or other system resource
377
(depending on the type of the backup location) could be created. Since
378
we failed to write backup image, this resource should be removed.
379
We set remove_location flag so that loc->remove() will be called after
380
closing the output stream.
382
remove_location= TRUE;
384
BACKUP_BREAKPOINT("bp_error_state");
389
Unfreeze all DDL operations by turning off DDL blocker.
391
DDL_blocker->unblock_DDL();
392
BACKUP_BREAKPOINT("DDL_unblocked");
399
if (loc->remove() != OK)
400
res= res ? res : ERROR;
188
Restore_info *info= context.prepare_for_restore(lex->backup_dir, thd->query);
190
if (!info || !info->is_valid())
191
DBUG_RETURN(send_error(context, ER_BACKUP_RESTORE_PREPARE));
193
DEBUG_SYNC(thd, "after_backup_start_restore");
195
res= context.do_restore();
197
DEBUG_SYNC(thd, "restore_before_end");
200
DBUG_RETURN(send_error(context, ER_BACKUP_RESTORE));
413
212
} // switch(lex->sql_command)
416
free_stream_memory();
417
finish_backup_or_restore();
215
DBUG_RETURN(send_error(context, ER_BACKUP_CONTEXT_REMOVE));
217
// All seems OK - send positive reply to client
219
DBUG_RETURN(send_reply(context));
225
Current implementation reports the last error saved in the logger if it exist.
226
Otherwise it reports error given by @c error_code.
228
@returns 0 on success, error code otherwise.
230
int send_error(Backup_restore_ctx &log, int error_code, ...)
232
MYSQL_ERROR *error= log.last_saved_error();
234
if (error && !util::report_mysql_error(log.thd(), error, error_code))
237
error_code= error->code;
239
else // there are no error information in the logger - report error_code
241
char buf[ERRMSGSIZE + 20];
243
va_start(args, error_code);
245
my_vsnprintf(buf, sizeof(buf), ER_SAFE(error_code), args);
246
my_printf_error(error_code, buf, MYF(0));
251
if (log.backup::Logger::m_state == backup::Logger::RUNNING)
252
log.report_stop(my_time(0), FALSE); // FASLE = no success
258
Send positive reply after a backup/restore operation.
260
Currently the id of the operation is returned. It can be used to select
261
correct entries form the backup progress tables.
263
int send_reply(Backup_restore_ctx &context)
265
Protocol *protocol= context.thd()->protocol; // client comms
266
List<Item> field_list; // list of fields to send
267
char buf[255]; // buffer for llstr
269
DBUG_ENTER("send_reply");
274
field_list.push_back(new Item_empty_string(STRING_WITH_LEN("backup_id")));
275
protocol->send_fields(&field_list, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
280
protocol->prepare_for_resend();
281
llstr(context.op_id(), buf);
282
protocol->store(buf, system_charset_info);
285
my_eof(context.thd());
293
This class provides memory allocation services for backup stream library.
295
An instance of this class is created during preparations for backup/restore
296
operation. When it is deleted, all allocated memory is freed.
311
node *first; ///< Pointer to the first segment in the list.
315
} // backup namespace
423
318
/*************************************************
320
Implementation of Backup_restore_ctx class
427
322
*************************************************/
429
// Declarations for functions used in backup operation
433
// defined in data_backup.cc
434
int write_table_data(THD*, Backup_info&, OStream&);
436
} // backup namespace
326
bool Backup_restore_ctx::is_running= FALSE;
327
pthread_mutex_t Backup_restore_ctx::run_lock;
328
backup::Mem_allocator *Backup_restore_ctx::mem_alloc= NULL;
331
Backup_restore_ctx::Backup_restore_ctx(THD *thd)
332
:Logger(thd), m_state(CREATED), m_thd_options(thd->options),
333
m_error(0), m_path(NULL), m_remove_loc(FALSE), m_stream(NULL),
334
m_catalog(NULL), m_tables_locked(FALSE)
337
Check for progress tables.
339
if (check_ob_progress_tables(thd))
340
m_error= ER_BACKUP_PROGRESS_TABLES;
343
Backup_restore_ctx::~Backup_restore_ctx()
352
Do preparations common to backup and restore operations.
354
It is checked if another operation is in progress and if yes then
355
error is reported. Otherwise the current operation is registered so that
356
no other can be started. All preparations common to backup and restore
357
operations are done. In particular, all changes to meta data are blocked
360
@returns 0 on success, error code otherwise.
362
int Backup_restore_ctx::prepare(LEX_STRING location)
367
// Prepare error reporting context.
369
mysql_reset_errors(m_thd, 0);
370
m_thd->no_warnings_for_error= FALSE;
375
Check access for SUPER rights. If user does not have SUPER, fail with error.
377
if (check_global_access(m_thd, SUPER_ACL))
379
fatal_error(ER_SPECIFIC_ACCESS_DENIED_ERROR, "SUPER");
384
Check if another BACKUP/RESTORE is running and if not, register
388
pthread_mutex_lock(&run_lock);
393
fatal_error(ER_BACKUP_RUNNING);
395
pthread_mutex_unlock(&run_lock);
400
// check if location is valid (we assume it is a file path)
402
bool bad_filename= (location.length == 0);
405
On some systems certain file names are invalid. We use
406
check_if_legal_filename() function from mysys to detect this.
408
#if defined(__WIN__) || defined(__EMX__)
410
bad_filename = bad_filename || check_if_legal_filename(location.str);
416
fatal_error(ER_BAD_PATH, location.str);
420
m_path= location.str;
422
// create new instance of memory allocator for backup stream library
424
using namespace backup;
427
mem_alloc= new Mem_allocator();
431
fatal_error(ER_OUT_OF_RESOURCES);
435
// Freeze all meta-data.
437
if (obs::ddl_blocker_enable(m_thd))
439
fatal_error(ER_DDL_BLOCK);
447
Prepare for backup operation.
449
@param[in] location path to the file where backup image should be stored
450
@param[in] query BACKUP query starting the operation
451
@param[in] with_compression backup image compression switch
453
@returns Pointer to a @c Backup_info instance which can be used for selecting
454
which objects to backup. NULL if an error was detected.
456
@note This function reports errors.
458
@note It is important that changes of meta-data are blocked as part of the
459
preparations. The set of server objects and their definitions should not
460
change after the backup context has been prepared and before the actual backup
461
is performed using @c do_backup() method.
464
Backup_restore_ctx::prepare_for_backup(LEX_STRING location, const char *query,
465
bool with_compression)
467
using namespace backup;
472
if (Logger::init(BACKUP, location, query))
474
fatal_error(ER_BACKUP_LOGGER_INIT);
478
time_t when= my_time(0);
482
Do preparations common to backup and restore operations. After call
483
to prepare() all meta-data changes are blocked.
485
if (prepare(location))
488
backup::String path(location);
494
Output_stream *s= new Output_stream(*this, path, with_compression);
499
fatal_error(ER_OUT_OF_RESOURCES);
505
fatal_error(ER_BACKUP_WRITE_LOC, path.ptr());
510
Create backup catalogue.
513
Backup_info *info= new Backup_info(*this); // reports errors
517
fatal_error(ER_OUT_OF_RESOURCES);
521
if (!info->is_valid())
524
info->save_start_time(when);
526
m_state= PREPARED_FOR_BACKUP;
532
Prepare for restore operation.
534
@param[in] location path to the file where backup image is stored
535
@param[in] query RESTORE query starting the operation
537
@returns Pointer to a @c Restore_info instance containing catalogue of the
538
backup image (read from the image). NULL if errors were detected.
540
@note This function reports errors.
543
Backup_restore_ctx::prepare_for_restore(LEX_STRING location, const char *query)
545
using namespace backup;
550
if (Logger::init(RESTORE, location, query))
552
fatal_error(ER_BACKUP_LOGGER_INIT);
556
time_t when= my_time(0);
560
Do preparations common to backup and restore operations. After this call
561
changes of meta-data are blocked.
563
if (prepare(location))
570
backup::String path(location);
571
Input_stream *s= new Input_stream(*this, path);
576
fatal_error(ER_OUT_OF_RESOURCES);
582
fatal_error(ER_BACKUP_READ_LOC, path.ptr());
587
Create restore catalogue.
590
Restore_info *info= new Restore_info(*this); // reports errors
594
fatal_error(ER_OUT_OF_RESOURCES);
598
if (!info->is_valid())
601
info->save_start_time(when);
605
Read catalogue from the input stream.
608
if (read_header(*info, *s))
610
fatal_error(ER_BACKUP_READ_HEADER);
614
if (s->next_chunk() != BSTREAM_OK)
616
fatal_error(ER_BACKUP_NEXT_CHUNK);
620
if (read_catalog(*info, *s))
622
fatal_error(ER_BACKUP_READ_HEADER);
626
if (s->next_chunk() != BSTREAM_OK)
628
fatal_error(ER_BACKUP_NEXT_CHUNK);
632
m_state= PREPARED_FOR_RESTORE;
638
Lock tables being restored.
640
Backup kernel ensures that all tables being restored are exclusively locked.
642
We use open_and_lock_tables() for locking. This is a temporary solution until
643
a better mechanism is devised - open_and_lock_tables() is not good if there
644
are many tables to be processed.
646
The built-in restore drivers need to open tables to write rows to them. Since
647
we have opened tables here, we store pointers to opened TABLE_LIST structures
648
in the restore catalogue so that the built-in drivers can access them later.
650
@todo Replace open_and_lock_tables() by a lighter solution.
651
@todo Hide table locking behind the server API.
653
int Backup_restore_ctx::lock_tables_for_restore()
655
TABLE_LIST *tables= NULL;
658
Iterate over all tables in all snapshots and create a linked TABLE_LIST
659
for call to open_and_lock_tables(). Store pointers to TABLE_LIST structures
660
in the restore catalogue for later access to opened tables.
663
for (uint s= 0; s < m_catalog->snap_count(); ++s)
665
backup::Snapshot_info *snap= m_catalog->m_snap[s];
667
for (ulong t=0; t < snap->table_count(); ++t)
669
backup::Image_info::Table *tbl= snap->get_table(t);
670
DBUG_ASSERT(tbl); // All tables should be present in the catalogue.
672
TABLE_LIST *ptr= backup::mk_table_list(*tbl, TL_WRITE, m_thd->mem_root);
673
DBUG_ASSERT(ptr); // FIXME: report error instead
675
tables= backup::link_table_list(*ptr, tables);
681
Open and lock the tables.
683
Note: simple_open_n_lock_tables() must be used here since we don't want
684
to do derived tables processing. Processing derived tables even leads
685
to crashes as those reported in BUG#34758.
687
if (simple_open_n_lock_tables(m_thd,tables))
689
fatal_error(ER_BACKUP_OPEN_TABLES,"RESTORE");
693
m_tables_locked= TRUE;
698
Unlock tables which were locked by @c lock_tables_for_restore.
700
int Backup_restore_ctx::unlock_tables()
702
// Do nothing if tables are not locked.
703
if (!m_tables_locked)
706
DBUG_PRINT("restore",("unlocking tables"));
708
close_thread_tables(m_thd);
709
m_tables_locked= FALSE;
715
Destroy a backup/restore context.
717
This should reverse all settings made when context was created and prepared.
718
If it was requested, the backup/restore location is removed. Also, the backup
719
stream memory allocator is shut down. Any other allocated resources are
720
deleted in the destructor. Changes to meta-data are unblocked.
722
@returns 0 or error code if error was detected.
724
@note This function reports errors.
726
int Backup_restore_ctx::close()
728
if (m_state == CLOSED)
731
using namespace backup;
733
time_t when= my_time(0);
735
// If auto commit is turned off, be sure to commit the transaction
736
// TODO: move it to the big switch, case: MYSQLCOM_BACKUP?
738
if (m_thd->options & (OPTION_NOT_AUTOCOMMIT | OPTION_BEGIN))
740
ha_autocommit_or_rollback(m_thd, 0);
741
end_active_trans(m_thd);
744
// unlock tables if they are still locked
748
// unfreeze meta-data
750
obs::ddl_blocker_disable();
752
// restore thread options
754
m_thd->options= m_thd_options;
762
m_catalog->save_end_time(when);
764
// destroy backup stream's memory allocator (this frees memory)
769
// deregister this operation
771
pthread_mutex_lock(&run_lock);
773
pthread_mutex_unlock(&run_lock);
776
Remove the location, if asked for.
778
Important: This is done only for backup operation - RESTORE should never
779
remove the specified backup image!
781
if (m_remove_loc && m_state == PREPARED_FOR_BACKUP)
783
int res= my_delete(m_path, MYF(0));
786
Ignore ENOENT error since it is ok if the file doesn't exist.
788
if (res && my_errno != ENOENT)
790
report_error(ER_CANT_DELETE_FILE, m_path, my_errno);
792
m_error= ER_CANT_DELETE_FILE;
796
// We report completion of the operation only if no errors were detected.
799
report_stop(when, TRUE);
440
806
Create backup archive.
808
@pre @c prepare_for_backup() method was called.
810
@returns 0 on success, error code otherwise.
443
int mysql_backup(THD *thd,
444
backup::Backup_info &info,
812
int Backup_restore_ctx::do_backup()
447
DBUG_ENTER("mysql_backup");
814
DBUG_ENTER("do_backup");
816
// This function should not be called when context is not valid
817
DBUG_ASSERT(is_valid());
818
DBUG_ASSERT(m_state == PREPARED_FOR_BACKUP);
820
DBUG_ASSERT(m_stream);
821
DBUG_ASSERT(m_catalog);
449
823
using namespace backup;
451
// This function should not be called with invalid backup info.
452
DBUG_ASSERT(info.is_valid());
454
BACKUP_BREAKPOINT("backup_meta");
825
Output_stream &s= *static_cast<Output_stream*>(m_stream);
826
Backup_info &info= *static_cast<Backup_info*>(m_catalog);
828
DEBUG_SYNC(m_thd, "before_backup_meta");
830
report_stats_pre(info);
456
832
DBUG_PRINT("backup",("Writing preamble"));
458
if (write_preamble(info,s))
834
if (write_preamble(info, s))
836
fatal_error(ER_BACKUP_WRITE_HEADER);
837
DBUG_RETURN(m_error);
461
840
DBUG_PRINT("backup",("Writing table data"));
463
BACKUP_BREAKPOINT("backup_data");
842
DEBUG_SYNC(m_thd, "before_backup_data");
465
if (write_table_data(thd,info,s))
844
if (write_table_data(m_thd, info, s)) // reports errors
845
DBUG_RETURN(send_error(*this, ER_BACKUP_BACKUP));
468
847
DBUG_PRINT("backup",("Writing summary"));
470
if (write_summary(info,s))
849
if (write_summary(info, s))
851
fatal_error(ER_BACKUP_WRITE_SUMMARY);
852
DBUG_RETURN(m_error);
855
report_stats_post(info);
473
857
DBUG_PRINT("backup",("Backup done."));
474
BACKUP_BREAKPOINT("backup_done");
858
DEBUG_SYNC(m_thd, "before_backup_done");
480
DDL_blocker->unblock_DDL();
488
Find backup engine which can backup data of a given table.
490
When necessary, a @c Snapshot_info object is created and added to the
493
@param t pointer to table's opened TABLE structure
494
@param tbl Table_ref describing the table
496
@return position in @c m_snap[] of the @c Snapshot_info object to which the
497
table has been added or -1 on error.
499
@todo Add error messages.
501
int Backup_info::find_backup_engine(const ::TABLE *const t,
502
const Table_ref &tbl)
504
handlerton *hton= t->s->db_type();
505
Table_ref::describe_buf buf;
508
DBUG_ENTER("Backup_info::find_backup_engine");
511
DBUG_PRINT("backup",("Locating backup engine for table %s which uses"
512
" storage engine %s%s",
514
hton ? ::ha_resolve_storage_engine_name(hton) : "(unknown engine)",
515
hton->get_backup_engine ? " (has native backup)." : "."));
518
Note: at backup time the native and CS snapshot info objects are always
519
located at m_snap[0] and m_snap[1], respectively. They are created in
520
the Backup_info constructor.
523
// try native driver if table has native backup engine
525
if (hton->get_backup_engine)
527
// see if the snapshot exists already (skip default ones)
528
for (no=2; no < 256 && m_snap[no] ; ++no)
529
if (m_snap[no]->accept(tbl,hton))
534
// TODO: report error
538
// We need to create native snapshot for this table
540
m_snap[no]= new Native_snapshot(t->s->db_plugin);
544
// TODO: report error
548
if (!m_snap[no]->accept(tbl,hton))
550
// TODO: report error
558
Try default drivers in decreasing order of preferrence
559
(first snapshot, then default)
561
for (no=1; no >=0; --no)
563
if (!m_snap[no]->accept(tbl,hton))
569
report_error(ER_BACKUP_NO_BACKUP_DRIVER,tbl.describe(buf,sizeof(buf)));
573
} // backup namespace
576
/****************************
578
Backup_info implementation
580
****************************/
584
// Returns tmp table containing records from a given I_S table
585
TABLE* get_schema_table(THD *thd, ST_SCHEMA_TABLE *st);
589
Create @c Backup_info structure and prepare it for populating with meta-data
592
When adding a complete database to the archive, all its tables are added.
593
These are found by reading INFORMATION_SCHEMA.TABLES table. The table is
594
opened here so that it is ready for use in @c add_db_items() method. It is
595
closed when the structure is closed with the @c close() method.
599
Backup_info::Backup_info(THD *thd):
600
Logger(Logger::BACKUP),
602
m_thd(thd), i_s_tables(NULL)
604
i_s_tables= get_schema_table(m_thd, ::get_schema_table(SCH_TABLES));
607
report_error(ER_BACKUP_LIST_TABLES);
611
// create default and CS snapshot objects
613
m_snap[0]= new Default_snapshot();
616
// TODO: report error
621
m_snap[1]= new CS_snapshot();
624
// TODO: report error
630
Backup_info::~Backup_info()
634
name_strings.delete_elements();
635
// Note: snapshot objects are deleted in ~Image_info()
639
Store information about table data snapshot inside @c st_bstream_snapshot_info
642
void save_snapshot_info(const Snapshot_info &snap, st_bstream_snapshot_info &info)
644
bzero(&info,sizeof(st_bstream_snapshot_info));
645
info.type= enum_bstream_snapshot_type(snap.type());
646
info.version= snap.version;
647
info.table_count= snap.table_count();
649
if (snap.type() == Snapshot_info::NATIVE_SNAPSHOT)
651
const Native_snapshot &nsnap= static_cast<const Native_snapshot&>(snap);
653
info.engine.major= nsnap.se_ver >> 8;
654
info.engine.minor= nsnap.se_ver & 0xFF;
655
info.engine.name.begin= (byte*)nsnap.m_name;
656
info.engine.name.end= info.engine.name.begin + strlen(nsnap.m_name);
661
Close @c Backup_info object after populating it with items.
663
After this call the @c Backup_info object is ready for use as a catalogue
664
for backup stream functions such as @c bstream_wr_preamble().
666
bool Backup_info::close()
671
::free_tmp_table(m_thd,i_s_tables);
675
Go through snapshots and save their descriptions inside snapshots[] table.
677
for (uint no=0; no < 256; ++no)
679
Snapshot_info *snap= m_snap[no];
684
if (snap->m_no == 0 || snap->table_count() == 0)
686
DBUG_ASSERT(snap->m_no == 0);
687
DBUG_ASSERT(snap->table_count() == 0);
693
save_snapshot_info(*snap,snapshot[snap->m_no-1]);
704
Add to backup image all databases in the list.
706
For each database, all objects stored in that database are also added to
711
int Backup_info::add_dbs(List< ::LEX_STRING > &dbs)
715
List_iterator< ::LEX_STRING > it(dbs);
717
String unknown_dbs; // comma separated list of databases which don't exist
723
if (is_internal_db_name(&db_name))
725
report_error(log_level::ERROR, ER_BACKUP_CANNOT_INCLUDE_DB,
730
Obj *db= get_database(&db_name);
732
if (db && !check_db_existence(db->get_name()))
734
if (!unknown_dbs.is_empty()) // we just compose unknown_dbs list
740
Db_item *it= add_db(db);
745
// TODO: report error
749
if (add_db_items(*it))
754
if (!unknown_dbs.is_empty())
755
unknown_dbs.append(",");
756
unknown_dbs.append(*db->get_name());
761
if (!unknown_dbs.is_empty())
763
report_error(ER_BAD_DB_ERROR,unknown_dbs.c_ptr());
772
return backup::ERROR;
776
Add all databases to backup image (except the internal ones).
778
For each database, all objects stored in that database are also added to
783
int Backup_info::add_all_dbs()
788
ObjIterator *dbit= get_databases(m_thd);
792
report_error(ER_BACKUP_LIST_DBS);
798
while ((db= dbit->next()))
800
// skip internal databases
801
if (is_internal_db_name(db->get_name()))
803
DBUG_PRINT("backup",(" Skipping internal database %s",db->get_name()->ptr()));
808
DBUG_PRINT("backup", (" Found database %s", db->get_name()->ptr()));
810
Db_item *it= add_db(db);
820
Note: the db instance is now owned by *this and will be deleted at
824
if (add_db_items(*it))
831
DBUG_PRINT("backup", ("No more databases in I_S"));
864
Create all triggers and events from restore catalogue.
866
This helper method iterates over all triggers and events stored in the
867
restore catalogue and creates them. When metadata section of the backup image
868
is read, trigger and event objects are materialized and stored in the
869
catalogue but they are not executed then (see @c bcat_create_item()).
870
This method can be used to re-create the corresponding server objects after
871
all other objects and table data have been restored.
873
Note that we first restore all triggers and then the events.
875
@returns 0 on success, error code otherwise.
877
int Backup_restore_ctx::restore_triggers_and_events()
879
using namespace backup;
881
DBUG_ASSERT(m_catalog);
883
Image_info::Iterator *dbit= m_catalog->get_dbs();
884
Image_info::Obj *obj;
885
List<Image_info::Obj> events;
886
Image_info::Obj::describe_buf buf;
888
DBUG_ENTER("restore_triggers_and_events");
890
// create all trigers and collect events in the events list
892
while ((obj= (*dbit)++))
894
Image_info::Iterator *it=
895
m_catalog->get_db_objects(*static_cast<Image_info::Db*>(obj));
897
while ((obj= (*it)++))
898
switch (obj->type()) {
900
case BSTREAM_IT_EVENT:
901
DBUG_ASSERT(obj->m_obj_ptr);
902
events.push_back(obj);
905
case BSTREAM_IT_TRIGGER:
906
DBUG_ASSERT(obj->m_obj_ptr);
907
if (obj->m_obj_ptr->execute(m_thd))
911
fatal_error(ER_BACKUP_CANT_RESTORE_TRIGGER,obj->describe(buf));
912
DBUG_RETURN(m_error);
845
Add to archive all objects belonging to a given database.
847
@todo Handle other types of objects - not only tables.
848
@todo Use WHERE clauses when reading I_S.TABLES
850
int Backup_info::add_db_items(Db_item &dbi)
854
ObjIterator *it= get_db_tables(m_thd,&dbi.name());
857
If error debugging is switched on (see debug.h) then I_S.TABLES access
858
error will be triggered when backing up database whose name starts with 'a'.
860
TEST_ERROR_IF(dbi.name().ptr()[0]=='a');
862
if (!it || TEST_ERROR)
864
report_error(ER_BACKUP_LIST_DB_TABLES,dbi.name().ptr());
871
while ((t= it->next()))
873
DBUG_PRINT("backup", ("Found table %s for database %s",
874
t->get_name()->ptr(), dbi.name().ptr()));
877
add_table() method selects/creates a snapshot to which this table is added.
878
The backup engine is chooden in Backup_info::find_backup_engine() method.
880
Table_item *ti= add_table(dbi,Table_ref(dbi,t));
924
// now create all events
926
List_iterator<Image_info::Obj> it(events);
930
if (ev->m_obj_ptr->execute(m_thd))
888
if (add_table_items(*ti))
893
it= get_db_stored_procedures(m_thd, &dbi.name());
897
// TODO: report error
901
add_objects(dbi, BSTREAM_IT_SPROC, *it);
904
it= get_db_stored_functions(m_thd, &dbi.name());
908
// TODO: report error
912
add_objects(dbi, BSTREAM_IT_SFUNC, *it);
915
it= get_db_views(m_thd, &dbi.name());
919
// TODO: report error
923
add_objects(dbi, BSTREAM_IT_VIEW, *it);
926
it= get_db_events(m_thd, &dbi.name());
930
// TODO: report error
934
add_objects(dbi, BSTREAM_IT_EVENT, *it);
937
it= get_db_triggers(m_thd, &dbi.name());
941
// TODO: report error
945
add_objects(dbi, BSTREAM_IT_TRIGGER, *it);
951
res= res ? res : ERROR;
961
Add table to archive's list of meta-data items.
963
@todo Correctly handle temporary tables.
964
@todo Avoid opening tables here - open them only in bcat_get_create_stmt().
966
Image_info::Table_item*
967
Backup_info::add_table(Db_item &dbi, const Table_ref &t)
969
Table_ref::describe_buf buf;
970
// TODO: skip table if it is a tmp one
972
Table_item *ti= NULL;
975
open table temporarily to:
977
- get a CREATE statement for it
980
TABLE_LIST entry, *tl= &entry;
981
bzero(&entry,sizeof(entry));
983
// FIXME: table/db name mangling
984
entry.db= const_cast<char*>(t.db().name().ptr());
985
entry.alias= entry.table_name= const_cast<char*>(t.name().ptr());
988
int res= ::open_tables(m_thd,&tl,&cnt,0);
990
if (res || !tl->table)
992
report_error(ER_BACKUP_TABLE_OPEN,t.describe(buf));
997
alternative way of opening a single tmp table - but it
998
doesn't initialize TABLE_LIST structure which we need for getting
1001
char path[FN_REFLEN];
1002
const char *db= t.db().name().ptr();
1003
const char *name= t.name().ptr();
1005
::build_table_filename(path, sizeof(path), db, name, "", 0);
1007
::TABLE *table= ::open_temporary_table(m_thd, path, db, name,
1008
FALSE /=* don't link to thd->temporary_tables *=/);
1012
::intern_close_table(table);
1013
my_free(table, MYF(0));
1016
int no= find_backup_engine(tl->table,t); // Note: reports errors
1018
DBUG_PRINT("backup",(" table %s backed-up with %s engine",
1020
m_snap[no]->name()));
1023
If error debugging is switched on (see debug.h) then any table whose
1024
name starts with 'a' will trigger "no backup driver" error.
1026
TEST_ERROR_IF(t.name().ptr()[0]=='a');
1028
if (no < 0 || TEST_ERROR)
1031
// add table to the catalogue
1033
ti= Image_info::add_table(dbi,t,no);
1036
If error debugging is switched on (see debug.h) then any table whose
1037
name starts with 'b' will trigger error when added to backup image.
1039
TEST_ERROR_IF(t.name().ptr()[0]=='b');
1041
if (!ti || TEST_ERROR)
1043
report_error(ER_OUT_OF_RESOURCES);
1049
::close_thread_tables(m_thd);
1055
Add to archive all items belonging to a given table.
1057
@todo Implement this.
1059
int Backup_info::add_table_items(Table_item&)
1061
// TODO: Implement when we handle per-table meta-data.
1065
} // backup namespace
1068
/*************************************************
1072
*************************************************/
1074
// Declarations of functions used in restore operation
1078
// defined in data_backup.cc
1079
int restore_table_data(THD*, Restore_info&, IStream&);
1081
} // backup namespace
1084
Toggle foreign key constraints on and off.
1086
@param THD thd Current thread structure.
1087
@param my_bool turn_on TRUE = turn on, FALSE = turn off.
1089
@returns TRUE if foreign key contraints are turned on already
1090
@returns FALSE if foreign key contraints are turned off
1092
my_bool fkey_constr(THD *thd, my_bool turn_on)
1094
my_bool fk_status= FALSE;
1096
DBUG_ENTER("mysql_restore");
1098
thd->options&= ~OPTION_NO_FOREIGN_KEY_CHECKS;
1101
fk_status= (thd->options & OPTION_NO_FOREIGN_KEY_CHECKS)? FALSE : TRUE;
1102
thd->options|= OPTION_NO_FOREIGN_KEY_CHECKS;
1104
DBUG_RETURN(fk_status);
932
fatal_error(ER_BACKUP_CANT_RESTORE_EVENT,ev->describe(buf));
933
DBUG_RETURN(m_error);
1108
940
Restore objects saved in backup image.
1110
@pre The header and catalogue of backup image has been already read with
1111
@c bstream_rd_header() function and stored inside the @c info object.
942
@pre @c prepare_for_restore() method was called.
944
@returns 0 on success, error code otherwise.
946
@todo Remove the @c reset_diagnostic_area() hack.
1113
int mysql_restore(THD *thd, backup::Restore_info &info, backup::IStream &s)
948
int Backup_restore_ctx::do_restore()
1115
my_bool using_fkey_constr= FALSE;
950
DBUG_ENTER("do_restore");
1117
DBUG_ENTER("mysql_restore");
952
DBUG_ASSERT(is_valid());
953
DBUG_ASSERT(m_state == PREPARED_FOR_RESTORE);
955
DBUG_ASSERT(m_stream);
956
DBUG_ASSERT(m_catalog);
1119
958
using namespace backup;
1123
DBUG_PRINT("restore",("Restoring meta-data"));
1126
Turn off foreign key constraints (if turned on)
1128
using_fkey_constr= fkey_constr(thd, FALSE);
1130
if (read_meta_data(info, s) == ERROR)
961
Input_stream &s= *static_cast<Input_stream*>(m_stream);
962
Restore_info &info= *static_cast<Restore_info*>(m_catalog);
964
report_stats_pre(info);
966
DBUG_PRINT("restore", ("Restoring meta-data"));
968
disable_fkey_constraints();
970
if (read_meta_data(info, s))
1132
fkey_constr(thd, using_fkey_constr);
972
fatal_error(ER_BACKUP_READ_META);
973
DBUG_RETURN(m_error);
1138
thd->main_da.reset_diagnostics_area();
1140
978
DBUG_PRINT("restore",("Restoring table data"));
981
FIXME: this call is here because object services doesn't clean the
982
statement execution context properly, which leads to assertion failure.
983
It should be fixed inside object services implementation and then the
984
following line should be removed.
986
close_thread_tables(m_thd);
987
m_thd->main_da.reset_diagnostics_area();
989
if (lock_tables_for_restore()) // reports errors
990
DBUG_RETURN(m_error);
1142
992
// Here restore drivers are created to restore table data
1143
if (restore_table_data(thd,info,s) == ERROR)
1145
fkey_constr(thd, using_fkey_constr);
1150
Turn on foreign key constraints (if previously turned on)
993
err= restore_table_data(m_thd, info, s); // reports errors
998
DBUG_RETURN(ER_BACKUP_RESTORE);
1001
Re-create all triggers and events (it was not done in @c bcat_create_item()).
1003
Note: it is important to do that after tables are unlocked, otherwise
1004
creation of these objects will fail.
1152
fkey_constr(thd, using_fkey_constr);
1007
if (restore_triggers_and_events())
1008
DBUG_RETURN(ER_BACKUP_RESTORE);
1154
1010
DBUG_PRINT("restore",("Done."));
1156
if (read_summary(info,s) == ERROR)
1012
if (read_summary(info, s))
1014
fatal_error(ER_BACKUP_READ_SUMMARY);
1015
DBUG_RETURN(m_error);
1019
FIXME: this call is here because object services doesn't clean the
1020
statement execution context properly, which leads to assertion failure.
1021
It should be fixed inside object services implementation and then the
1022
following line should be removed.
1024
close_thread_tables(m_thd);
1025
m_thd->main_da.reset_diagnostics_area();
1027
report_stats_post(info);
1159
1029
DBUG_RETURN(0);
1162
/****************************
1164
Restore_info implementation
1166
****************************/
1168
1033
namespace backup {
1171
Initialize @c Restore_info instance and load the catalogue from
1172
the given backup stream.
1174
Restore_info::Restore_info(THD *thd, IStream &s):
1175
Logger(Logger::RESTORE), m_valid(TRUE), m_thd(thd), curr_db(NULL),
1176
system_charset(NULL), same_sys_charset(TRUE)
1035
/*************************************************
1037
Implementation of Mem_allocator class.
1039
*************************************************/
1041
/// All allocated memory segments are linked into a list using this structure.
1042
struct Mem_allocator::node
1178
int ret= BSTREAM_OK;
1180
ret= read_header(*this,s);
1182
if (!(m_valid= (ret != BSTREAM_ERROR)))
1185
ret= s.next_chunk();
1187
if (!(m_valid= (ret == BSTREAM_OK)))
1190
ret= read_catalog(*this,s);
1191
m_valid= (ret != BSTREAM_ERROR);
1194
Restore_info::~Restore_info()
1048
Mem_allocator::Mem_allocator() :first(NULL)
1198
Restore an object given its meta data.
1200
@param[in] it Item instance identifying the object to restore
1201
@param[in] sdata the serialization read from backup image
1202
@param[in] extra other meta data stored in the image (not used now)
1204
result_t Restore_info::restore_item(Item &it, String &sdata, String &extra)
1206
using namespace obs;
1208
Obj *obj= it.obj_ptr(0, sdata);
1213
return obj->execute(m_thd) ? ERROR : OK;
1051
/// Deletes all allocated segments which have not been freed explicitly.
1052
Mem_allocator::~Mem_allocator()
1065
Allocate memory segment of given size.
1067
Extra memory is allocated for @c node structure which holds pointers
1068
to previous and next segment in the segments list. This is used when
1069
deallocating allocated memory in the destructor.
1071
void* Mem_allocator::alloc(size_t howmuch)
1073
void *ptr= my_malloc(sizeof(node) + howmuch, MYF(0));
1078
node *n= (node*)ptr;
1091
Explicit deallocation of previously allocated segment.
1093
The @c ptr should contain an address which was obtained from
1094
@c Mem_allocator::alloc().
1096
The deallocated fragment is removed from the allocated fragments list.
1098
void Mem_allocator::free(void *ptr)
1103
node *n= ((node*)ptr) - 1;
1109
n->prev->next= n->next;
1112
n->next->prev= n->prev;
1216
1117
} // backup namespace
1218
1120
/*************************************************
1220
1122
CATALOGUE SERVICES
1499
1781
/*************************************************
1501
MEMORY ALLOCATOR FOR BACKUP STREAM LIBRARY
1503
*************************************************/
1508
This calss provides memory allocation services for backup stream library.
1510
An instance of this class must be created and pointer to it stored in the
1511
static @c instance variable during BACKUP/RESTORE operation. This assumes
1512
only one BACKUP/RESTORE operation is running at a time.
1521
void* alloc(size_t);
1524
static Mem_allocator *instance;
1528
/// All allocated memory segments are linked into a list using this structure.
1535
node *first; ///< Pointer to the first segment in the list.
1539
Mem_allocator::Mem_allocator(): first(NULL)
1542
/// Deletes all allocated segments which have not been freed.
1543
Mem_allocator::~Mem_allocator()
1556
Allocate memory segment of given size.
1558
Extra memory is allocated for a @c node structure which holds pointers
1559
to previous and next segment in the segments list. This is used when
1560
deallocating allocated memory in the destructor.
1562
void* Mem_allocator::alloc(size_t howmuch)
1564
void *ptr= my_malloc(sizeof(node)+howmuch, MYF(0));
1569
node *n= (node*)ptr;
1582
Explicit deallocation of previously allocated segment.
1584
The @c ptr should contain an address which was obtained from
1585
@c Mem_allocator::alloc().
1587
The deallocated fragment is removed from the allocated fragments list.
1589
void Mem_allocator::free(void *ptr)
1594
node *n= ((node*)ptr) - 1;
1600
n->prev->next= n->next;
1603
n->next->prev= n->prev;
1608
Mem_allocator *Mem_allocator::instance= NULL;
1611
This function must be called before @c bstream_alloc() can be used.
1613
void prepare_stream_memory()
1615
if (Mem_allocator::instance)
1616
delete Mem_allocator::instance;
1618
Mem_allocator::instance= new Mem_allocator();
1622
This function should be called when @c bstream_alloc()/ @c bstream_free()
1623
are no longer to be used.
1625
It destroys the Mem_allocator instance which frees all memory which was
1626
allocated but not explicitly freed.
1628
void free_stream_memory()
1630
delete Mem_allocator::instance;
1631
Mem_allocator::instance= NULL;
1639
Memory allocator for backup stream library.
1641
@pre @c prepare_stream_memory() has been called (i.e., the Mem_allocator
1642
instance is created.
1644
bstream_byte* bstream_alloc(unsigned long int size)
1646
using namespace backup;
1648
DBUG_ASSERT(Mem_allocator::instance);
1650
return (bstream_byte*)Mem_allocator::instance->alloc(size);
1654
Memory deallocator for backup stream library.
1656
void bstream_free(bstream_byte *ptr)
1658
using namespace backup;
1660
if (Mem_allocator::instance)
1661
Mem_allocator::instance->free(ptr);
1666
/*************************************************
1670
*************************************************/
1675
Specialization of @c Location class representing a file in the local
1678
struct File_loc: public Location
1682
enum_type type() const
1683
{ return SERVER_FILE; }
1685
File_loc(const char *p)
1691
On some systems certain file names are invalid. We use
1692
check_if_legal_filename() function from mysys to detect this.
1694
#if defined(__WIN__) || defined(__EMX__)
1696
if (check_if_legal_filename(path.c_ptr()))
1703
const char* describe()
1704
{ return path.c_ptr(); }
1708
int res= my_delete(path.c_ptr(),MYF(0));
1711
Ignore ENOENT error since it is ok if the file doesn't exist.
1713
if (my_errno == ENOENT)
1717
sql_print_error(ER(ER_CANT_DELETE_FILE),path.c_ptr(),my_errno);
1719
return res ? ERROR : OK;
1725
S* open_stream(const Location &loc)
1727
switch (loc.type()) {
1729
case Location::SERVER_FILE:
1731
const File_loc &f= static_cast<const File_loc&>(loc);
1732
S *s= new S(f.path);
1747
template IStream* open_stream<IStream>(const Location&);
1748
template OStream* open_stream<OStream>(const Location&);
1750
IStream* open_for_read(const Location &loc)
1752
return open_stream<IStream>(loc);
1755
OStream* open_for_write(const Location &loc)
1757
return open_stream<OStream>(loc);
1761
Find location described by a string.
1763
The string is taken from the "TO ..." clause of BACKUP/RESTORE commands.
1764
This function parses the string and creates instance of @c Location class
1765
describing the location or NULL if string doesn't describe any valid location.
1767
Currently the only supported type of location is a file on the server host.
1768
The string is supposed to contain a path to such file.
1770
@note No checks on the location are made at this stage. In particular the
1771
location might not physically exist. In the future methods performing such
1772
checks can be added to @Location class.
1775
Location::find(const LEX_STRING &where)
1777
return where.str && where.length ? new File_loc(where.str) : NULL;
1780
} // backup namespace
1783
/*************************************************
1787
*************************************************/
1789
TABLE *create_schema_table(THD *thd, TABLE_LIST *table_list); // defined in sql_show.cc
1796
Current implementation reports the last error saved in the logger if it exist.
1797
Otherwise it reports error given by @c error_code.
1799
int report_errors(THD *thd, Logger &log, int error_code, ...)
1801
MYSQL_ERROR *error= log.last_saved_error();
1803
if (error && !util::report_mysql_error(thd,error,error_code))
1806
error_code= error->code;
1808
else // there are no error information in the logger - report error_code
1810
char buf[ERRMSGSIZE + 20];
1812
va_start(args,error_code);
1814
my_vsnprintf(buf,sizeof(buf),ER_SAFE(error_code),args);
1815
my_printf_error(error_code,buf,MYF(0));
1824
int check_info(THD *thd, Backup_info &info)
1826
return info.is_valid() ? OK : report_errors(thd,info,ER_BACKUP_BACKUP_PREPARE);
1830
int check_info(THD *thd, Restore_info &info)
1832
return info.is_valid() ? OK : report_errors(thd,info,ER_BACKUP_RESTORE_PREPARE);
1836
Send a summary of the backup/restore operation to the client.
1838
The data about the operation is taken from filled @c Archive_info
1839
structure. Parameter @c backup determines if this was backup or
1843
bool send_summary(THD *thd, const Image_info &info, bool backup)
1845
Protocol *protocol= thd->protocol; // client comms
1846
List<Item> field_list; // list of fields to send
1847
String op_str("backup_id"); // operations string
1848
int ret= 0; // return value
1849
char buf[255]; // buffer for summary information
1852
DBUG_ENTER("backup::send_summary");
1854
DBUG_PRINT(backup?"backup":"restore", ("sending summary"));
1859
field_list.push_back(new Item_empty_string(op_str.c_ptr(), op_str.length()));
1860
protocol->send_fields(&field_list, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
1865
protocol->prepare_for_resend();
1866
llstr(info.backup_prog_id,buf);
1867
protocol->store(buf, system_charset_info);
1875
bool send_summary(THD *thd, const Backup_info &info)
1876
{ return send_summary(thd,info,TRUE); }
1879
bool send_summary(THD *thd, const Restore_info &info)
1880
{ return send_summary(thd,info,FALSE); }
1883
/// Open given table in @c INFORMATION_SCHEMA database.
1884
TABLE* get_schema_table(THD *thd, ST_SCHEMA_TABLE *st)
1888
my_bitmap_map *old_map;
1890
bzero( &arg, sizeof(TABLE_LIST) );
1892
// set context for create_schema_table call
1893
arg.schema_table= st;
1895
arg.select_lex= NULL;
1897
t= ::create_schema_table(thd,&arg); // Note: callers must free t.
1899
if( !t ) return NULL; // error!
1902
Temporarily set thd->lex->wild to NULL to keep st->fill_table
1905
::String *wild= thd->lex->wild;
1906
::enum_sql_command command= thd->lex->sql_command;
1908
thd->lex->wild = NULL;
1909
thd->lex->sql_command = enum_sql_command(0);
1911
// context for fill_table
1914
old_map= tmp_use_all_columns(t, t->read_set);
1916
st->fill_table(thd, &arg,
1917
obs::create_db_select_condition(thd, t, &thd->lex->db_list));
1919
tmp_restore_column_map(t->read_set, old_map);
1921
// undo changes to thd->lex
1922
thd->lex->wild= wild;
1923
thd->lex->sql_command= command;
1928
/// Build linked @c TABLE_LIST list from a list stored in @c Table_list object.
1783
Implementation of Table_ref class
1785
*************************************************/
1790
Produce string identifying the table in internal format (as used by
1793
const char* Table_ref::internal_name(char *buf, size_t len) const
1795
uint plen= build_table_filename(buf, len,
1796
db().name().ptr(), name().ptr(),
1797
"", /* no extension */
1798
0 /* not a temporary table - do conversions */);
1804
Produce human readable string identifying the table
1805
(e.g. for error reporting)
1807
const char* Table_ref::describe(char *buf, size_t len) const
1809
my_snprintf(buf, len, "`%s`.`%s`", db().name().ptr(), name().ptr());
1931
FIXME: build list with the same order as in input
1932
Actually, should work fine with reversed list as long as we use the reversed
1933
list both in table writing and reading.
1814
TODO: remove these functions. Currently they are only used by the myisam
1815
native backup engine.
1935
1817
TABLE_LIST *build_table_list(const Table_list &tables, thr_lock_type lock)
1937
1819
TABLE_LIST *tl= NULL;
1939
1821
for( uint tno=0; tno < tables.count() ; tno++ )
1941
TABLE_LIST *ptr= (TABLE_LIST*)my_malloc(sizeof(TABLE_LIST), MYF(MY_WME));
1942
DBUG_ASSERT(ptr); // FIXME: report error instead
1943
bzero(ptr,sizeof(TABLE_LIST));
1945
Table_ref tbl= tables[tno];
1947
ptr->alias= ptr->table_name= const_cast<char*>(tbl.name().ptr());
1948
ptr->db= const_cast<char*>(tbl.db().name().ptr());
1949
ptr->lock_type= lock;
1951
// and add it to the list
1953
ptr->next_global= ptr->next_local=
1954
ptr->next_name_resolution_table= tl;
1956
tl->table= ptr->table;
1823
TABLE_LIST *ptr = mk_table_list(tables[tno], lock, ::current_thd->mem_root);
1825
tl= link_table_list(*ptr,tl);
1963
/// Execute SQL query without sending anything to client.
1965
int silent_exec_query(THD *thd, ::String &query)
1967
Vio *save_vio= thd->net.vio;
1969
DBUG_PRINT("restore",("executing query %s",query.c_ptr()));
1972
Note: the change net.vio idea taken from execute_init_command in
1977
thd->query= query.c_ptr();
1978
thd->query_length= query.length();
1980
thd->set_time(time(NULL));
1981
pthread_mutex_lock(&::LOCK_thread_count);
1982
thd->query_id= ::next_query_id();
1983
pthread_mutex_unlock(&::LOCK_thread_count);
1986
@todo The following is a work around for online backup and the DDL blocker.
1987
It should be removed when the generalized solution is in place.
1988
This is needed to ensure the restore (which uses DDL) is not blocked
1989
when the DDL blocker is engaged.
1991
thd->DDL_exception= TRUE;
1994
::mysql_parse(thd,thd->query,thd->query_length,&ptr);
1996
thd->net.vio= save_vio;
1998
if (thd->is_error())
2000
DBUG_PRINT("restore",
2001
("error executing query %s!", thd->query));
2002
DBUG_PRINT("restore",("last error (%d): %s",thd->net.last_errno
2003
,thd->net.last_error));
2004
return thd->net.last_errno ? (int)thd->net.last_errno : -1;
1831
void free_table_list(TABLE_LIST*)
2010
1834
} // backup namespace
2012
extern pthread_mutex_t LOCK_backup;
1837
/*************************************************
1841
*************************************************/
2014
1843
namespace backup {
2016
static bool backup_or_restore_is_running= FALSE;
2019
Indicate that BACKUP/RESTORE operation has started.
2021
@returns 0 if it is OK to continue or non-zero if another BACKUP/RESTORE
2022
command is running and it is not possible to execute enather one now.
2024
int start_backup_or_restore()
2028
pthread_mutex_lock(&::LOCK_backup);
2029
running= backup_or_restore_is_running;
2031
backup_or_restore_is_running= TRUE;
2032
pthread_mutex_unlock(&::LOCK_backup);
2038
Indicate that BACKUP/RESTORE operation has finished.
2040
This function should be called only if an earlier call to
2041
start_backup_or_restore() was successful.
2043
void finish_backup_or_restore()
2045
pthread_mutex_lock(&::LOCK_backup);
2046
backup_or_restore_is_running= FALSE;
2047
pthread_mutex_unlock(&::LOCK_backup);
1846
Build linked @c TABLE_LIST list from a list stored in @c Table_list object.
1848
@note The order of tables in the returned list is different than in the
1849
input list (reversed).
1851
@todo Decide what to do if errors are detected. For example, how to react
1852
if memory for TABLE_LIST structure could not be allocated?
2050
1856
} // backup namespace